Constructing a Nested Output Model

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.

Creating Output Objects from Input Phrase Components

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 ...

Get Language Implementation Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.