Generating code for expressions
The easiest code to generate is straight-line code consisting of statements and expressions that execute in sequence with no control flow. As described earlier in this chapter, there are two attributes to compute for each node: the attribute for where to find an expression's value is called addr
, while the intermediate code necessary to compute its value is called icode
. The values to be computed for these attributes for a subset of the Jzero expression grammar are shown in the following table. The |||
operator refers to list concatenation:
The main intermediate code generation algorithm is a bottom-up post-order traversal of the syntax tree. To present it in small chunks, the traversal is broken into the main method, gencode()
, and helper methods for each non-terminal. In Unicon, the gencode()
method in tree.icn
looks as follows:
method gencode() every (...