I have started a project where I need to design an application that will work as a code-block editor, much like lego mindstorms and this google project. http://code.google.com/p/blockly/

What are the possible approaches for designing such an application in Java?

More specifically, assuming I have one block A, how can I define which other blocks can be connected with it? What I think of is using subclasses and checking the hierarchy, or another possible solution could be some kind of 2D table that will verify a connection is valid(although I think it might get too big?).

p.s: I need to force correct structure of code, and not check it with parser at the end. So only blocks that fit together should be connected.

  • Things like plugins are quite popular approach. But plugins are too burdensome to create, so maybe something more lightweight is appropriate. – tp1 Feb 9 '13 at 17:01

Like any programming language, it's just a representation of an Abstract Syntax Tree.

Any approach that you can use to represent a tree can be used to represent a program. Interconnecting blocks like you see at Blockly are simply a syntax a human-readable view of an AST. The way the blocks connect make it easier for you to visualise the possible flows for any given item in the tree, and easier to validate on the fly.

I think once you look at it in those terms, the rest is fairly obvious, no? Just code it like you would any tree of classes that can only fit together in particular ways.

  • I was thinking the following approach: each block stores a string which represents it in EBNF. When connecting A to B I check language to see if B can take A. Maybe in cases B has many blocks connected, check each to see if there is room for more. – latusaki Feb 10 '13 at 9:04
  • Another approach I though was using a parser generated from a tool like javaCC. Although I am thinking that since the program needs to be validated while being constructed, checking its complete syntax tree will most likely return errors unless its complete. – latusaki Feb 10 '13 at 9:31
  • 1
    @latusaki: I sincerely suggest you get a book programming-language design and a book about DSLs. This is way too broad a subject to be covered in a simple Q&A format. – pdr Feb 10 '13 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.