December 2009
Intermediate to advanced
380 pages
9h 2m
English
To build a translator, we need to look at the mechanics of matching an input construct and creating the right output object. Then, we have to figure out how to organize all those translated pieces into a nested hierarchy.
Translating an individual input phrase means creating the appropriate output model object and injecting it with elements from the input phrase. Here’s what the assignment node example from the previous section looks like using an AST visitor and a target-specific generator object:
| | Statement gen(AssignNode n) { |
| | Expression e = gen(n.valueExpr); // walk right-hand-side expression |
| | return new AssignmentStatement(n.id, e); |
| | } |
Notice ...