Java Reference
In-Depth Information
class
NodeVisitor
procedure
(
n
)
foreach
c
∈
n
.
getChildren()
do call
c
.
accept(
this
)
visit
C
hildren
end
end
class
SemanticsVisitor
extends
NodeVisitor
/
This extends the class definition of Figure 9.1
/
procedure
(Switching
sn
)
call
sn
.
control
.
accept(
this
)
if
sn
.
control
.
type
errorType
and not
visit
28
assignable
(
int
,
sn
.
control
.
type
)
then
call
error
(”
Illegal type f or control expression
”)
call
set
S
witch
T
ype
(
errorType
)
else call
set
(
sn
.
control
.
type
)
call
sn
.
cases
.
accept(
this
)
labelList
← sort
S
witch
T
ype
(
gather
L
abels
(
sn
.
cases
))
call
check
F
or
D
uplicates
(
labelList
)
if
count
D
efaults
(
sn
.
cases
)
>
1
then call
error
(”
More than one de f ault case label
”)
end
procedure
visit
(CaseItem
cn
)
call
visit
C
hildren
(
cn
)
end
procedure
visit
( LabelList
lln
)
call
visit
C
hildren
(
lln
)
lln
.
caseLabel
←
null
if
lln
.
caseExp
.
type
errorType
then
if not
assignable
(
get
S
witch
T
ype
()
,
lln
.
caseExp
.
type
)
then call
error
(”
Invalid case label type
”)
else
constExprVisitor
←
new
ConstExprVisitor()
call
lln
.
caseExp
.
accept(
constExprVisitor
)
labelValue
←
lln
.
caseExp
.
exprValue
if
labelValue
=
null
then call
error
(”
Case label must be a constant expression
”)
end
end
Figure 9.21: Semantic Analysis Visitors (Part 2)