KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Update accepted Ira Baxter's answer since it pointed me into the right direction: I first figured out what I actually needed by starting the implementation of the compiling stage, and it became obvious pretty soon that traversal within the nodes made thie an impossible approach. Not all nodes should be visited, and some of them in reverse order (for example, first the rhs of an assignment so the compiler can check if the type matches with the rhs/operator). Putting traversal in the visitor makes this all very easy. I'm playing around with ASTs and the likes before deciding a major refactory of the handling of a mini-language used in an applicaiton. I've built a Lexer/Parser and can get the AST just fine. There's also a Visitor and as concrete implementation I made an ASTToOriginal which just recreates the original source file. Eventually there's goin to be some sort of compiler that also implements the Vsisitor and creates the actual C++ code at runtime so I want to make sure everything is right from the start. While everything works fine now, there is some similar/duplicate code since the traversal order is implemented in the Visitor itself. When looking up more information, it seems that some implementations prefer keeping the traversal order in the visited objects themselves instead, in order not to repeat this in each concrete visitor. Even the GoF only talks briefly about this, in the same way. So I wanted to give this approach a try as well but got stuck pretty soon.. Let me explain. Sample source line and corresponding AST nodes: if(t>100?x=1;sety(20,true):x=2) Conditional BinaryOp left=Variable [name=t], operator=[>], right=Integer [value=100] IfTrue Assignment left=Variable [name=x], operator=[=], right=Integer [value=1] Method MethodName [name=sety], Arguments( Integer [value=20], Boolean [value=true] ) IfFalse Assignment left=Variable [name=x], opera
Tags (comma-separated)
Save Edits
Cancel