Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

1 Answer

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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