Skip to content

Commit 402bd58

Browse files
committed
Refactor ConstantPropagation: remove some fields/rename methods
1 parent 5d40c8f commit 402bd58

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/main/pt/up/fe/comp2023/optimization/ConstantPropagation.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212

1313
public class ConstantPropagation extends AJmmVisitor<Map<String, String>, Void> {
1414
private final JmmSemanticsResult semanticsResult;
15-
private final SymbolTable symbolTable; //TODO: remove
16-
private String currentMethodName; //TODO: remove
1715
private boolean codeModified;
1816

1917
public ConstantPropagation (JmmSemanticsResult semanticsResult){
2018
this.semanticsResult = semanticsResult;
21-
this.symbolTable = semanticsResult.getSymbolTable();
2219
}
2320

2421
public boolean apply(){
@@ -32,10 +29,10 @@ public boolean apply(){
3229
@Override
3330
protected void buildVisitor() {
3431
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);
3936
addVisit("Cycle", this::dealWithCycle);
4037
addVisit("Assignment", this::dealWithAssignment);
4138
addVisit("Identifier", this::dealWithIdentifier);
@@ -47,16 +44,15 @@ private Void setDefaultVisit(JmmNode jmmNode, Map<String, String> constants) {
4744
return null;
4845
}
4946

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) {
5248
constants.clear();
5349

5450
for (JmmNode child: jmmNode.getChildren())
5551
visit(child, constants); //each statement modifies the map
5652
return null;
5753
}
5854

59-
private Void checkIfElseCondition(JmmNode jmmNode, Map<String, String> constants) {
55+
private Void dealWithCondition(JmmNode jmmNode, Map<String, String> constants) {
6056
JmmNode conditionNode = jmmNode.getJmmChild(0);
6157
JmmNode ifCode = jmmNode.getJmmChild(1);
6258
JmmNode elseCode = jmmNode.getJmmChild(2);

0 commit comments

Comments
 (0)