From b4d67b6c6e350c43e0c0f2e39b22b2c5e8dc8889 Mon Sep 17 00:00:00 2001 From: MisaghM Date: Sat, 13 May 2023 23:13:25 +0330 Subject: [PATCH] Add implicationStmt to name analyzer and fix forLoop --- .../symbolTableItems/ForLoopItem.java | 6 +- .../visitor/nameAnalyzer/NameAnalyzer.java | 56 ++++++++++++------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Phase-2/src/symbolTable/symbolTableItems/ForLoopItem.java b/Phase-2/src/symbolTable/symbolTableItems/ForLoopItem.java index 2481f68..9d3e2a1 100644 --- a/Phase-2/src/symbolTable/symbolTableItems/ForLoopItem.java +++ b/Phase-2/src/symbolTable/symbolTableItems/ForLoopItem.java @@ -4,8 +4,6 @@ import ast.type.Type; import symbolTable.SymbolTable; -import java.util.ArrayList; - public class ForLoopItem extends SymbolTableItem { protected Type identifierType; @@ -45,11 +43,11 @@ public String getName() { public void setName(String name) { this.name = name; - forLoopStmt.getIterator().setName(name); + this.forLoopStmt.getIterator().setName(name); } public ForloopStmt getHandlerStmt() { - return forLoopStmt; + return this.forLoopStmt; } @Override diff --git a/Phase-2/src/visitor/nameAnalyzer/NameAnalyzer.java b/Phase-2/src/visitor/nameAnalyzer/NameAnalyzer.java index 49b6971..3ce2c5f 100644 --- a/Phase-2/src/visitor/nameAnalyzer/NameAnalyzer.java +++ b/Phase-2/src/visitor/nameAnalyzer/NameAnalyzer.java @@ -5,12 +5,12 @@ import ast.node.statement.ArrayDecStmt; import ast.node.statement.VarDecStmt; import ast.node.statement.ForloopStmt; +import ast.node.statement.ImplicationStmt; import compileError.*; import compileError.Name.*; import symbolTable.SymbolTable; import symbolTable.symbolTableItems.*; import symbolTable.itemException.ItemAlreadyExistsException; -import symbolTable.symbolTableItems.VariableItem; import visitor.Visitor; import java.util.ArrayList; @@ -31,7 +31,8 @@ public Void visit(Program program) { for (var stmt : program.getMain().getMainStatements()) { if (stmt instanceof VarDecStmt || stmt instanceof ArrayDecStmt - || stmt instanceof ForloopStmt) { + || stmt instanceof ForloopStmt + || stmt instanceof ImplicationStmt) { stmt.accept(this); } } @@ -39,7 +40,6 @@ public Void visit(Program program) { return null; } - @Override public Void visit(FuncDeclaration funcDeclaration) { var functionItem = new FunctionItem(funcDeclaration); @@ -50,12 +50,12 @@ public Void visit(FuncDeclaration funcDeclaration) { break; } catch (ItemAlreadyExistsException e) { if (!functionItem.getName().endsWith("@")) - nameErrors.add(new FunctionRedefinition(funcDeclaration.getName().getLine(), funcDeclaration.getName().getName())); + nameErrors.add(new FunctionRedefinition(funcDeclaration.getLine(), functionItem.getName())); functionItem.setName(functionItem.getName() + "@"); } } - var functionSymbolTable = new SymbolTable(SymbolTable.top, funcDeclaration.getName().getName()); + var functionSymbolTable = new SymbolTable(SymbolTable.top, functionItem.getName()); functionItem.setFunctionSymbolTable(functionSymbolTable); SymbolTable.push(functionSymbolTable); @@ -66,7 +66,8 @@ public Void visit(FuncDeclaration funcDeclaration) { for (var stmt : funcDeclaration.getStatements()) { if (stmt instanceof VarDecStmt || stmt instanceof ArrayDecStmt - || stmt instanceof ForloopStmt) { + || stmt instanceof ForloopStmt + || stmt instanceof ImplicationStmt) { stmt.accept(this); } } @@ -85,7 +86,7 @@ public Void visit(VarDecStmt varDeclaration) { break; } catch (ItemAlreadyExistsException e) { if (!variableItem.getName().endsWith("@")) - nameErrors.add(new VariableRedefinition(varDeclaration.getIdentifier().getLine(), varDeclaration.getIdentifier().getName())); + nameErrors.add(new VariableRedefinition(varDeclaration.getLine(), variableItem.getName())); variableItem.setName(variableItem.getName() + "@"); } } @@ -103,7 +104,7 @@ public Void visit(ArrayDecStmt arrDeclaration) { break; } catch (ItemAlreadyExistsException e) { if (!arrayItem.getName().endsWith("@")) - nameErrors.add(new VariableRedefinition(arrDeclaration.getIdentifier().getLine(), arrDeclaration.getIdentifier().getName())); + nameErrors.add(new VariableRedefinition(arrDeclaration.getLine(), arrayItem.getName())); arrayItem.setName(arrayItem.getName() + "@"); } } @@ -121,7 +122,7 @@ public Void visit(ArgDeclaration argDeclaration) { break; } catch (ItemAlreadyExistsException e) { if (!argItem.getName().endsWith("@")) - nameErrors.add(new VariableRedefinition(argDeclaration.getIdentifier().getLine(), argDeclaration.getIdentifier().getName())); + nameErrors.add(new VariableRedefinition(argDeclaration.getLine(), argItem.getName())); argItem.setName(argItem.getName() + "@"); } } @@ -133,25 +134,21 @@ public Void visit(ArgDeclaration argDeclaration) { public Void visit(ForloopStmt forloopStmt) { var forLoopItem = new ForLoopItem(forloopStmt); - var forLoopSymbolTable = new SymbolTable(SymbolTable.top, forloopStmt.getIterator().getName()); + var forLoopSymbolTable = new SymbolTable(SymbolTable.top, forLoopItem.getName()); forLoopItem.setForLoopSymbolTable(forLoopSymbolTable); SymbolTable.push(forLoopSymbolTable); - while (true) { - try { - SymbolTable.top.put(forLoopItem); - break; - } catch (ItemAlreadyExistsException e) { - if (!forLoopItem.getName().endsWith("@")) - nameErrors.add(new VariableRedefinition(forloopStmt.getIterator().getLine(), forloopStmt.getIterator().getName())); - forLoopItem.setName(forLoopItem.getName() + "@"); - } + try { + SymbolTable.top.put(forLoopItem); + } catch (ItemAlreadyExistsException e) { + // Will not reach here because SymbolTable.top is empty. } for (var stmt : forloopStmt.getStatements()) { if (stmt instanceof VarDecStmt || stmt instanceof ArrayDecStmt - || stmt instanceof ForloopStmt) { + || stmt instanceof ForloopStmt + || stmt instanceof ImplicationStmt) { stmt.accept(this); } } @@ -159,5 +156,22 @@ public Void visit(ForloopStmt forloopStmt) { SymbolTable.pop(); return null; } -} + @Override + public Void visit(ImplicationStmt implStmt) { + var implSymbolTable = new SymbolTable(SymbolTable.top, "ImplicationStmt"); + SymbolTable.push(implSymbolTable); + + for (var stmt : implStmt.getStatements()) { + if (stmt instanceof VarDecStmt + || stmt instanceof ArrayDecStmt + || stmt instanceof ForloopStmt + || stmt instanceof ImplicationStmt) { + stmt.accept(this); + } + } + + SymbolTable.pop(); + return null; + } +}