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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.