Skip to content

Commit

Permalink
Complete phase-3 code
Browse files Browse the repository at this point in the history
  • Loading branch information
MisaghM committed Jun 11, 2023
1 parent cc42258 commit a84891d
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 72 deletions.
35 changes: 17 additions & 18 deletions Phase-3/src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,26 @@
import visitor.typeAnalyzer.TypeAnalyzer;

public class Main {
public static void main(String[] args) throws java.io.IOException {
public static void main(String[] args) throws java.io.IOException {

CharStream reader = CharStreams.fromFileName(args[0]);
LogicPLLexer lexer = new LogicPLLexer(reader);
CommonTokenStream tokens = new CommonTokenStream(lexer);
LogicPLParser parser = new LogicPLParser(tokens);
Program program = parser.program().p;
CharStream reader = CharStreams.fromFileName(args[0]);
LogicPLLexer lexer = new LogicPLLexer(reader);
CommonTokenStream tokens = new CommonTokenStream(lexer);
LogicPLParser parser = new LogicPLParser(tokens);
Program program = parser.program().p;

NameAnalyzer nameAnalyzer = new NameAnalyzer();
nameAnalyzer.visit(program);

NameAnalyzer nameAnalyzer = new NameAnalyzer();
nameAnalyzer.visit(program);
TypeAnalyzer typeAnalyzer = new TypeAnalyzer();
typeAnalyzer.visit(program);

TypeAnalyzer typeAnalyzer = new TypeAnalyzer();
typeAnalyzer.visit(program);
if (typeAnalyzer.typeErrors.size() > 0){
for(CompileError compileError: typeAnalyzer.typeErrors)
System.out.println(compileError.getMessage());
return;
}

System.out.println("Compilation was Successful!!");
if (typeAnalyzer.typeErrors.size() > 0) {
for (CompileError compileError : typeAnalyzer.typeErrors)
System.out.println(compileError.getMessage());
return;
}

}
System.out.println("Compilation was Successful!!");
}
}
9 changes: 9 additions & 0 deletions Phase-3/src/ast/node/statement/ForloopStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

//Line -> FOR
public class ForloopStmt extends Statement {
private int id;
private Identifier iterator;
private Identifier arrayName;
private ArrayList<Statement> bodyStmts = new ArrayList<>();
Expand Down Expand Up @@ -43,6 +44,14 @@ public void setArrayName(Identifier identifier) {
this.arrayName = identifier;
}

public int getForloopId() {
return id;
}

public void setForloopId(int id) {
this.id = id;
}

@Override
public String toString() {
return "ForloopStmt";
Expand Down
11 changes: 9 additions & 2 deletions Phase-3/src/ast/node/statement/ImplicationStmt.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package ast.node.statement;

import ast.node.expression.Expression;
import ast.node.expression.Identifier;
import ast.node.expression.Variable;
import visitor.IVisitor;

import java.util.ArrayList;

//Line -> ARROW
public class ImplicationStmt extends Statement {
private int id;
private Expression condition;
private ArrayList<Statement> resaultStmts = new ArrayList<>();

Expand All @@ -35,6 +34,14 @@ public void setCondition(Expression condition) {
this.condition = condition;
}

public int getImplicationId() {
return id;
}

public void setImplicationId(int id) {
this.id = id;
}

@Override
public String toString() {
return "ImplicationStmt";
Expand Down
32 changes: 15 additions & 17 deletions Phase-3/src/symbolTable/symbolTableItems/ForLoopItem.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
package symbolTable.symbolTableItems;

import ast.node.declaration.ArgDeclaration;
import ast.node.declaration.FuncDeclaration;
import ast.node.statement.ForloopStmt;
import ast.type.Type;
import symbolTable.SymbolTable;

import java.util.ArrayList;

public class ForLoopItem extends SymbolTableItem{
protected SymbolTable ForLoopSymbolTable;
public class ForLoopItem extends SymbolTableItem {
protected SymbolTable forLoopSymbolTable;
protected ForloopStmt forloopStmt;
public static final String STARTKEY = "ForLoop_";

public ForLoopItem(String name) {
this.name = name;
}
public static final String STARTKEY = "ForLoop_";
private static int counter = 0;
private int id;

public ForLoopItem(ForloopStmt forloopStmt)
{
public ForLoopItem(ForloopStmt forloopStmt) {
this.id = counter++;
forloopStmt.setForloopId(id);
this.name = forloopStmt.toString();
this.forloopStmt = forloopStmt;
}

public SymbolTable getForLoopSymbolTable()
{
return this.ForLoopSymbolTable;
public SymbolTable getForLoopSymbolTable() {
return this.forLoopSymbolTable;
}

public void setForLoopSymbolTable(SymbolTable symbolTable) {
this.forLoopSymbolTable = symbolTable;
}

@Override
public String getKey() {
return FunctionItem.STARTKEY + this.name;
return ForLoopItem.STARTKEY + this.name + this.id;
}
}
33 changes: 33 additions & 0 deletions Phase-3/src/symbolTable/symbolTableItems/ImplicationItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package symbolTable.symbolTableItems;

import ast.node.statement.ImplicationStmt;
import symbolTable.SymbolTable;

public class ImplicationItem extends SymbolTableItem {
protected SymbolTable implicationSymbolTable;
protected ImplicationStmt implicationStmt;

public static final String STARTKEY = "Implication_";
private static int counter = 0;
private int id;

public ImplicationItem(ImplicationStmt implicationStmt) {
this.id = counter++;
implicationStmt.setImplicationId(id);
this.name = implicationStmt.toString();
this.implicationStmt = implicationStmt;
}

public SymbolTable getImplicationSymbolTable() {
return this.implicationSymbolTable;
}

public void setImplicationSymbolTable(SymbolTable symbolTable) {
this.implicationSymbolTable = symbolTable;
}

@Override
public String getKey() {
return ImplicationItem.STARTKEY + this.name + this.id;
}
}
4 changes: 2 additions & 2 deletions Phase-3/src/visitor/astPrinter/ASTPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public Void visit(ArrayAccess arrayAccess) {
@Override
public Void visit(FunctionCall functionCall) {
messagePrinter(functionCall.getLine(), functionCall.toString());
if (functionCall.getUFuncName() != null)
functionCall.getUFuncName().accept(this);
if (functionCall.getFuncName() != null)
functionCall.getFuncName().accept(this);
for (Expression expression: functionCall.getArgs())
expression.accept(this);
return null;
Expand Down
63 changes: 62 additions & 1 deletion Phase-3/src/visitor/nameAnalyzer/NameAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import ast.node.Program;
import ast.node.declaration.*;
import ast.node.statement.*;
import ast.type.NoType;
import compileError.*;
import compileError.Name.*;
import symbolTable.SymbolTable;
import symbolTable.symbolTableItems.*;
import symbolTable.itemException.ItemAlreadyExistsException;
import symbolTable.symbolTableItems.VariableItem;
import symbolTable.itemException.ItemNotFoundException;
import visitor.Visitor;

import java.util.ArrayList;
Expand All @@ -27,6 +28,7 @@ public Void visit(Program program) {
functionDeclaration.accept(this);
}

// program.getMain().accept(this);
for (var stmt : program.getMain().getMainStatements()) {
if(stmt instanceof VarDecStmt) {
stmt.accept(this);
Expand All @@ -45,6 +47,30 @@ public Void visit(Program program) {
return null;
}

// @Override
// public Void visit(MainDeclaration mainDeclaration) {
// var mainItem = new MainItem(mainDeclaration);
// var mainSymbolTable = new SymbolTable(SymbolTable.top, "main");
// mainItem.setMainItemSymbolTable(mainSymbolTable);
// SymbolTable.push(mainSymbolTable);

// for (var stmt : mainDeclaration.getMainStatements()) {
// if (stmt instanceof VarDecStmt) {
// stmt.accept(this);
// }
// if (stmt instanceof ArrayDecStmt) {
// stmt.accept(this);
// }
// if (stmt instanceof ForloopStmt) {
// stmt.accept(this);
// }
// if (stmt instanceof ImplicationStmt) {
// stmt.accept(this);
// }
// }

// return null;
// }

@Override
public Void visit(FuncDeclaration funcDeclaration) {
Expand Down Expand Up @@ -94,9 +120,34 @@ public Void visit(FuncDeclaration funcDeclaration) {

@Override
public Void visit(ForloopStmt forloopStmt) {
var forloopItem = new ForLoopItem(forloopStmt);
var forLoopSymbolTable = new SymbolTable(SymbolTable.top, forloopStmt.toString());
forloopItem.setForLoopSymbolTable(forLoopSymbolTable);

try {
SymbolTable.top.put(forloopItem);
}
catch (ItemAlreadyExistsException e) {
// unreachable
}

VariableItem forVarItem;
try {
var x = (VariableItem) SymbolTable.top.get(VariableItem.STARTKEY + forloopStmt.getArrayName().getName());
forVarItem = new VariableItem(forloopStmt.getIterator().getName(), x.getType());
}
catch (ItemNotFoundException e) {
forVarItem = new VariableItem(forloopStmt.getIterator().getName(), new NoType());
}

SymbolTable.push(forLoopSymbolTable);
try {
SymbolTable.top.put(forVarItem);
}
catch (ItemAlreadyExistsException e) {
// unreachable
}

for(Statement stmt: forloopStmt.getStatements()) {
if(stmt instanceof VarDecStmt) {
stmt.accept(this);
Expand All @@ -121,7 +172,16 @@ public Void visit(ForloopStmt forloopStmt) {

@Override
public Void visit(ImplicationStmt implicationStmt) {
var implicationItem = new ImplicationItem(implicationStmt);
var implicationSymbolTable = new SymbolTable(SymbolTable.top, implicationStmt.toString());
implicationItem.setImplicationSymbolTable(implicationSymbolTable);

try {
SymbolTable.top.put(implicationItem);
}
catch (ItemAlreadyExistsException e) {
// unreachable
}

SymbolTable.push(implicationSymbolTable);
for(Statement stmt: implicationStmt.getStatements()) {
Expand Down Expand Up @@ -161,6 +221,7 @@ public Void visit(VarDecStmt varDeclaration) {

@Override
public Void visit(ArrayDecStmt arrayDecStmt) {
// var variableItem = new ArrayItem(arrayDecStmt);
var variableItem = new VariableItem(arrayDecStmt.getIdentifier().getName(), arrayDecStmt.getType());
try {
SymbolTable.top.put(variableItem);
Expand Down
Loading

0 comments on commit a84891d

Please sign in to comment.