diff --git a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java index 72af1be..8ddd6bf 100644 --- a/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/BinaryExpression.java @@ -14,6 +14,12 @@ public class BinaryExpression extends AbstractType implements IExpression{ public IExpression left; public IExpression right; + public BinaryExpression(String operator, IExpression left, IExpression right) { + this.operator = operator; + this.left = left; + this.right = right; + } + @Override public TypeCheckResult typeCheck() throws Exception { TypeCheckHelper helper = new TypeCheckHelper(); diff --git a/src/main/java/abstractSyntaxTree/Expression/IExpression.java b/src/main/java/abstractSyntaxTree/Expression/IExpression.java index a679711..f4cdf75 100644 --- a/src/main/java/abstractSyntaxTree/Expression/IExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/IExpression.java @@ -1,9 +1,10 @@ package abstractSyntaxTree.Expression; import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Node; import org.objectweb.asm.MethodVisitor; -public interface IExpression { +public interface IExpression extends Node { // typeCheck method TypeCheckResult typeCheck() throws Exception; diff --git a/src/main/java/astGenerator/ASTGenerator.java b/src/main/java/astGenerator/ASTGenerator.java index 24e582f..c31af91 100644 --- a/src/main/java/astGenerator/ASTGenerator.java +++ b/src/main/java/astGenerator/ASTGenerator.java @@ -4,6 +4,7 @@ import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Expression.BinaryExpression; +import abstractSyntaxTree.Expression.IExpression; import abstractSyntaxTree.Node; import abstractSyntaxTree.Parameter.Parameter; import abstractSyntaxTree.Parameter.ParameterList; @@ -114,7 +115,8 @@ public class ASTGenerator extends DecafBaseVisitor { @Override public Node visitReturnStmt(DecafParser.ReturnStmtContext ctx) { - return new ReturnStatement(new BinaryExpression()); + Node expression = visitExpression(ctx.expression()); + return new ReturnStatement((IExpression) expression); } @Override @@ -132,22 +134,25 @@ public class ASTGenerator extends DecafBaseVisitor { if (ctx.elseStmt() != null) { return visitIfStmt(ctx.ifStmt()); } else { + Node expression = visitExpression(ctx.ifStmt().expression()); Node ifStatement = visitStatement(ctx.ifStmt().statement()); Node elseStatement = visitStatement(ctx.elseStmt().statement()); - return new IfElseStatement(new BinaryExpression(), (IStatement) ifStatement, (IStatement) elseStatement); + return new IfElseStatement((IExpression) expression, (IStatement) ifStatement, (IStatement) elseStatement); } } @Override public Node visitIfStmt(DecafParser.IfStmtContext ctx) { + Node expression = visitExpression(ctx.expression()); Node statement = visitStatement(ctx.statement()); - return new IfStatement(new BinaryExpression(), (IStatement) statement); + return new IfStatement((IExpression) expression, (IStatement) statement); } @Override public Node visitWhileStmt(DecafParser.WhileStmtContext ctx) { + Node expression = visitExpression(ctx.expression()); Node statement = visitStatement(ctx.statement()); - return new WhileStatement(new BinaryExpression(), (IStatement) statement); + return new WhileStatement((IExpression) expression, (IStatement) statement); } @Override @@ -167,7 +172,6 @@ public class ASTGenerator extends DecafBaseVisitor { return null; } - @Override public Node visitAssign(DecafParser.AssignContext ctx) { return new AssignStatementExpression(); @@ -182,4 +186,79 @@ public class ASTGenerator extends DecafBaseVisitor { public Node visitNewDecl(DecafParser.NewDeclContext ctx) { return super.visitNewDecl(ctx); } + + @Override + public Node visitExpression(DecafParser.ExpressionContext ctx) { + if (ctx.subExpression() != null) { + + } else if (ctx.binaryExpr() != null) { + return visitBinaryExpr(ctx.binaryExpr()); + } + return null; + } + + @Override + public Node visitBinaryExpr(DecafParser.BinaryExprContext ctx) { + if (ctx.calcExpr() != null) { + return visitCalcExpr(ctx.calcExpr()); + } else if (ctx.nonCalcExpr() != null) { + return visitNonCalcExpr(ctx.nonCalcExpr()); + } else if (ctx.value() != null) { + //todo + } else if (ctx.binaryExpr() != null) { + //todo + } + return null; + } + + @Override + public Node visitCalcExpr(DecafParser.CalcExprContext ctx) { + if (ctx.calcExpr() != null) { + Node left = visitCalcExpr(ctx.calcExpr()); + Node right = visitDotExpr(ctx.dotExpr()); + return new BinaryExpression(ctx.LineOperator().getText(), (IExpression) left, (IExpression) right); + } else { + visitDotExpr(ctx.dotExpr()); + } + return null; + } + + @Override + public Node visitDotExpr(DecafParser.DotExprContext ctx) { + if (ctx.dotExpr() != null) { + Node left = visitDotExpr(ctx.dotExpr()); + Node right = visitDotSubExpr(ctx.dotSubExpr()); + return new BinaryExpression(ctx.DotOperator().getText(), (IExpression) left, (IExpression) right); + } else { + visitDotSubExpr(ctx.dotSubExpr()); + } + return null; + } + + //todo + @Override + public Node visitDotSubExpr(DecafParser.DotSubExprContext ctx) { + return super.visitDotSubExpr(ctx); + } + + @Override + public Node visitNonCalcExpr(DecafParser.NonCalcExprContext ctx) { + String operator; + if(ctx.nonCalcOperator().LogicalOpertor() != null) { + operator = ctx.nonCalcOperator().LogicalOpertor().getText(); + } else { + operator = ctx.nonCalcOperator().ComparisonOperator().getText(); + } + Node left = visitSubExpression(ctx.subExpression()); + Node right = visitExpression(ctx.expression()); + return new BinaryExpression(operator, (IExpression) left, (IExpression) right); + } + + @Override + public Node visitSubExpression(DecafParser.SubExpressionContext ctx) { + if (ctx.subExpression() != null) { + visitSubExpression(ctx.subExpression()); + } + return null; + } }