
Build Your Own Programming Language
By :

Within the syntax tree, the nodes associated with actual code expressions in the method bodies have a type associated with the value that the expression computes. For example, if a tree node corresponds to the sum of adding two numbers, the tree node's type is determined by the types of the operands and the rules of the language for the addition operator. Our goal for this section is to spell out how this type information can be calculated.
As you saw in the Type representation in the compiler section, the class for syntax tree nodes has an attribute to store that node's type, if there is one. The type
attribute is calculated bottom-up, during a post-order tree traversal. There is a similarity here to checking for undeclared variables, which we did in the previous chapter, in that type checking expressions only occur in the bodies of functions. The call to invoke this type checking tree traversal, starting at the root of the...