Java Reference
In-Depth Information
}
//Declaretheparameters
for(JFormalParameterparam:params){
this.context.addEntry(param.line(),param.name(),
newLocalVariableDefn(param.type(),this.context
.nextOffset(),null));
}
if(body!=null){
body=body.analyze(this.context);
}
returnthis;
}
The code for analyzing a
JBlock
is even simpler; it performs just two steps:
1. It creates a new
LocalContext
, whose
surroundingContext
points back to the pre-
vious
MethodContext
(or
LocalContext
in the case of nested blocks). Its
nextOffset
value is copied from the previous context.
2. It analyzes each of the body's statements. Any
JVariableDeclaration
s declare their
variables in the
LocalContext
created in step 1. Any nested
JBlock
simply invokes
this two-step process recursively, creating yet another
LocalContext
for the nested
block.
Again, the code is straightforward:
publicJBlockanalyze(Contextcontext){
//{...}definesanewlevelofscope.
this.context=newLocalContext(context);
for(inti=0;i<statements.size();i++){
statements.set(i,(JStatement)statements.get(i).analyze(
this.context));
}
returnthis;
}
For example, the steps in adding the contexts to the symbol table for the analysis of method
foo()
are illustrated in Figure 4.7. In the contexts, the names should map to objects of
type
LocalVariableDefn
, which define the variables and their stack frame offsets. In the
figures, the arrows point to the offsets in parentheses.
Search WWH ::
Custom Search