12
12
13
13
public class ConstantPropagation extends AJmmVisitor <Map <String , String >, Void > {
14
14
private final JmmSemanticsResult semanticsResult ;
15
- private final SymbolTable symbolTable ; //TODO: remove
16
- private String currentMethodName ; //TODO: remove
17
15
private boolean codeModified ;
18
16
19
17
public ConstantPropagation (JmmSemanticsResult semanticsResult ){
20
18
this .semanticsResult = semanticsResult ;
21
- this .symbolTable = semanticsResult .getSymbolTable ();
22
19
}
23
20
24
21
public boolean apply (){
@@ -32,10 +29,10 @@ public boolean apply(){
32
29
@ Override
33
30
protected void buildVisitor () {
34
31
setDefaultVisit (this ::setDefaultVisit );
35
- addVisit ("MethodDecl" , this ::changeCurrentMethodName );
36
- addVisit ("VoidMethodDecl" , this ::changeCurrentMethodName );
37
- addVisit ("MainMethodDecl" , this ::changeCurrentMethodName );
38
- addVisit ("Condition" , this ::checkIfElseCondition );
32
+ addVisit ("MethodDecl" , this ::clearConstants );
33
+ addVisit ("VoidMethodDecl" , this ::clearConstants );
34
+ addVisit ("MainMethodDecl" , this ::clearConstants );
35
+ addVisit ("Condition" , this ::dealWithCondition );
39
36
addVisit ("Cycle" , this ::dealWithCycle );
40
37
addVisit ("Assignment" , this ::dealWithAssignment );
41
38
addVisit ("Identifier" , this ::dealWithIdentifier );
@@ -47,16 +44,15 @@ private Void setDefaultVisit(JmmNode jmmNode, Map<String, String> constants) {
47
44
return null ;
48
45
}
49
46
50
- private Void changeCurrentMethodName (JmmNode jmmNode , Map <String , String > constants ) {
51
- this .currentMethodName = jmmNode .get ("methodname" ); //TODO: remove
47
+ private Void clearConstants (JmmNode jmmNode , Map <String , String > constants ) {
52
48
constants .clear ();
53
49
54
50
for (JmmNode child : jmmNode .getChildren ())
55
51
visit (child , constants ); //each statement modifies the map
56
52
return null ;
57
53
}
58
54
59
- private Void checkIfElseCondition (JmmNode jmmNode , Map <String , String > constants ) {
55
+ private Void dealWithCondition (JmmNode jmmNode , Map <String , String > constants ) {
60
56
JmmNode conditionNode = jmmNode .getJmmChild (0 );
61
57
JmmNode ifCode = jmmNode .getJmmChild (1 );
62
58
JmmNode elseCode = jmmNode .getJmmChild (2 );
0 commit comments