Java Reference
In-Depth Information
BinaryExpr
UnaryExpr
type
type
operator
operator
leftExpr
rightExpr
subExpr
Figure 8.35: Abstract Syntax Tree Representations for Unary and
Binary Expressions
and binary operators, as represented by at ASTs shown in Figure 8.35. The
nodes referenced by le f tExpr , rightExpr ,and subExpr in the figure can reference
any kind of expression subtree. Whether simple or complex, they are analyzed
by the appropriate semantic visitor.
visit
methods for binary and unary operators are defined in Figure 8.33
at Marker 62 and Marker 63 . Each of the methods starts by propagating the
visitor traversal to the operand expressions, after which only the types they
produce are of interest.
Thetypecheckingdonebythese
methods depends on the operator
and the types of the operands. The pseudocode for the visitors uses the func-
tions
visit
to examine the operator and
operands to determine whether they describe a legal operation according to
the definition of the language being compiled. If the specified operation is
meaningful, then the type of the result of the operation is returned; otherwise,
the result of the call must be errorType . Consider a few examples. Adding
integers is allowed in all programming languages, with the result being an
integer. Addition of an integer and a float will produce a float in most lan-
guages, but in a language that does not allow implicit type conversions, such
an expression is erroneous. Typically, comparision of two arithmetic values
yields a Boolean result.
binary
R
esult
T
ype
and
unary
R
esult
T
ype
8.8.4 Checking Complex Names
We now examine the type checking that must be done when references to
elements of arrays or fields of structs are analyzed. The AST representations
of array and struct references are illustrated in Figure 8.36. These trees are
actually simplified a bit, since objectName in a StructReferencing node and
arrayName in an ArrayReferencing node need not always be Identifier nodes.
 
 
Search WWH ::




Custom Search