Skip to content

Commit

Permalink
Add implicationStmt to name analyzer and fix forLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
MisaghM committed May 13, 2023
1 parent c734dd8 commit b4d67b6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
6 changes: 2 additions & 4 deletions Phase-2/src/symbolTable/symbolTableItems/ForLoopItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import ast.type.Type;
import symbolTable.SymbolTable;

import java.util.ArrayList;

public class ForLoopItem extends SymbolTableItem {

protected Type identifierType;
Expand Down Expand Up @@ -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
Expand Down
56 changes: 35 additions & 21 deletions Phase-2/src/visitor/nameAnalyzer/NameAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,15 +31,15 @@ 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);
}
}

return null;
}


@Override
public Void visit(FuncDeclaration funcDeclaration) {
var functionItem = new FunctionItem(funcDeclaration);
Expand All @@ -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);

Expand All @@ -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);
}
}
Expand All @@ -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() + "@");
}
}
Expand All @@ -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() + "@");
}
}
Expand All @@ -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() + "@");
}
}
Expand All @@ -133,31 +134,44 @@ 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);
}
}

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;
}
}

0 comments on commit b4d67b6

Please sign in to comment.