Java Reference
In-Depth Information
method for a literal node is trivial, since the type is immediately
available. The pseudocode is also included in Figure 8.33, at Marker 60 .There
is no corresponding
The
visit
method in LHSSemanticVisitor because we expect
the parser to forbid interger literals incontextswhereanL-valueisexpected.
The case where literals may incorrectly appear as L-values is explored in
Exercise 26.
We develop the type checking visitors for more complex names, such as
record field and array element references, in Section 8.8.4. The processing that
is done for an assignment is designed to completely ignore the complexity
of the subtree that specifies the target of the assignment, just as it need not
consider the complexity of the computation that specifies the source value.
visit
8.8.2 Assignment Statements
/
Visitor code for Marker 61 on page 325
/
procedure
visit
(Assigning assign )
lhsVisitor
new LHSSemanticsVisitor()
call assign . targetName . accept( lhsVisitor )
69
call assign . valueExpr . accept( this )
70
if
( assign . valueExpr . type , assign . targetName . type )
then assign . type assign . targetName . type
else
assignable
71
call
error
(” Right hand side expression not assignable to le f t hand side name at
, assign )
assign . type errorType
end
method for an Assigningnode involves obtain-
ing the types of the components of the assignment statement and checking
whether they are compatible according to the rules of the language. We cre-
ateanduseanLHSSemanticsVisitor at Marker 69 to get the target's type
and verify that it may be the target of an assignment. We use the normal
SemanticsVisitor at Marker 70 to check the type of the right-hand side. The
test at Marker 71 determines whether the type of the value on the right-hand
side of the assignment can be assigned to the variable on the left. The type
field of the Assigningnode itself is then set appropriately, with errorType being
used if the assignability test fails.
The major task of the
visit
8.8.3 Checking Expressions
At the beginning of our discussion of type checking, we saw two simple
examples of the general concept of an expression, namely, a single identifier
and a literal constant. More complex expressions are constructed using unary
 
 
 
Search WWH ::




Custom Search