waterbearlang / waterbear

Visual block syntax for programming languages
http://waterbearlang.com/
358 stars 88 forks source link

Parse Javascript code into blocks #193

Open dethe opened 11 years ago

dethe commented 11 years ago

This would allow for round-tripping code, so generated code could be editable, but also allow loading arbitrary Javascript code into Waterbear for viewing as blocks, which could help with editing, debugging, and refactoring.

If this were done really well, we might not even need the JSON intermediate format?

kumavis commented 11 years ago

I would recommend using esprima https://github.com/ariya/esprima

dethe commented 11 years ago

Yes, Esprima is a good candidate, as is Acorn (https://github.com/marijnh/acorn). The problem isn't parsing so much, as detecting the patterns in the resulting AST which match code templates in Waterbear blocks. Alternatively, I could make blocks for every possible Javascript pattern (which would be hidden an unavailable to users by default, but could show in parsed code). Even if we parse the code and match it to existing Waterbear blocks, there will be code which does not match, so I will need to add one or more blocks to handle that, probably that just contain raw code.

WebRTCGame commented 11 years ago

This is a huge feature and something that would promote the transition from blocks to hand coding. Really it's something I would love for this project. Would it be possible to have a 'un-parsed' block where it would just contain the unparseable region of code, even containing other blocks. I came here and looked through most of the issues just to find the waterbear --> js --> waterbear loop, and if there were any plans on working on it. I'm glad to see it's being addressed to any extent.

kumavis commented 11 years ago

I got this working in node-blockly-js-editor, though its kind of a mess.

  1. Parse with esprima
  2. pattern match with pattern-match
  3. construct block representation (in Blockly's case xml)

if-statement example: https://github.com/kumavis/node-blockly-js-editor/blob/master/language/jslang/jslang_if.js#L73-L118 relevant util code: https://github.com/kumavis/node-blockly-js-editor/blob/master/util.js#L24-L71

node-blockly-js-editor is not under active development, but I'd be happy to answer questions

hit me up: on freenode kumavis twitter email aaron@kumavis.me

dethe commented 11 years ago

Right now we're in the process of adding hidden blocks (blocks that can be in scripts, but don't show by default in menus), which will help with this. Blocks that can contain arbitrary Javascript are also on the todo-list, although they will require some care in handling inputs and returns to play nicely with other blocks.

Thanks for the examples!

CelticMinstrel commented 11 years ago

Alternatively, I could make blocks for every possible Javascript pattern

Um, this sounds awfully like an impossible task. I think a hidden "arbitrary code" block would be the better solution.

kumavis commented 11 years ago

@CelticMinstrel If you implement these basic language structures you have full coverage, and then you can start building higher level blocks as desired.

CelticMinstrel commented 11 years ago

Eh, true enough, I guess.