diff --git a/src/main/java/ast/block/BlockNode.java b/src/main/java/ast/block/BlockNode.java index 9987cf9..ab2d70b 100644 --- a/src/main/java/ast/block/BlockNode.java +++ b/src/main/java/ast/block/BlockNode.java @@ -2,6 +2,7 @@ package ast.block; import ast.ASTNode; import ast.statement.IStatementNode; +import ast.statement.ReturnStatementNode; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; import visitor.Visitable; @@ -12,6 +13,7 @@ import java.util.List; public class BlockNode implements ASTNode, Visitable { public List statements = new ArrayList<>(); + public Boolean hasReturnStatement = false; public BlockNode() {} diff --git a/src/main/java/ast/expression/binaryexpression/CalculationExpressionNode.java b/src/main/java/ast/expression/binaryexpression/CalculationExpressionNode.java index 70451d8..b594502 100644 --- a/src/main/java/ast/expression/binaryexpression/CalculationExpressionNode.java +++ b/src/main/java/ast/expression/binaryexpression/CalculationExpressionNode.java @@ -6,12 +6,12 @@ import typechecker.TypeCheckResult; public class CalculationExpressionNode extends BinaryExpressionNode { public CalculationExpressionNode calculationExpression; - public String operator; + public EnumLineOperator operator; public DotExpressionNode dotExpression; public CalculationExpressionNode(CalculationExpressionNode calculationExpression, String operator, DotExpressionNode dotExpression) { this.calculationExpression = calculationExpression; - this.operator = operator; + setOperator(operator); this.dotExpression = dotExpression; } @@ -19,6 +19,14 @@ public class CalculationExpressionNode extends BinaryExpressionNode { this.dotExpression = dotExpression; } + private void setOperator(String operator) { + if(operator.equals("+")) { + this.operator = EnumLineOperator.PLUS; + } else if(operator.equals("-")) { + this.operator = EnumLineOperator.MINUS; + } + } + @Override public TypeCheckResult accept(SemanticVisitor visitor) { return visitor.analyze(this); diff --git a/src/main/java/ast/expression/binaryexpression/DotExpressionNode.java b/src/main/java/ast/expression/binaryexpression/DotExpressionNode.java index cc5f2c6..2d56221 100644 --- a/src/main/java/ast/expression/binaryexpression/DotExpressionNode.java +++ b/src/main/java/ast/expression/binaryexpression/DotExpressionNode.java @@ -6,12 +6,12 @@ import typechecker.TypeCheckResult; public class DotExpressionNode extends BinaryExpressionNode { public DotExpressionNode dotExpression; - public String operator; + public EnumDotOperator operator; public DotSubstractionExpressionNode dotSubstractionExpression; public DotExpressionNode(DotExpressionNode dotExpression, String operator, DotSubstractionExpressionNode dotSubstractionExpression) { this.dotExpression = dotExpression; - this.operator = operator; + setOperator(operator); this.dotSubstractionExpression = dotSubstractionExpression; } @@ -19,6 +19,16 @@ public class DotExpressionNode extends BinaryExpressionNode { this.dotSubstractionExpression = dotSubstractionExpression; } + private void setOperator(String operator) { + if(operator.equals("*")) { + this.operator = EnumDotOperator.MULT; + } else if(operator.equals("/")) { + this.operator = EnumDotOperator.DIV; + } else if(operator.equals("%")) { + this.operator = EnumDotOperator.MOD; + } + } + @Override public TypeCheckResult accept(SemanticVisitor visitor) { return visitor.analyze(this); diff --git a/src/main/java/ast/expression/binaryexpression/EnumDotOperator.java b/src/main/java/ast/expression/binaryexpression/EnumDotOperator.java new file mode 100644 index 0000000..3525266 --- /dev/null +++ b/src/main/java/ast/expression/binaryexpression/EnumDotOperator.java @@ -0,0 +1,5 @@ +package ast.expression.binaryexpression; + +public enum EnumDotOperator { + MULT, DIV, MOD +} diff --git a/src/main/java/ast/expression/binaryexpression/EnumLineOperator.java b/src/main/java/ast/expression/binaryexpression/EnumLineOperator.java new file mode 100644 index 0000000..27389ec --- /dev/null +++ b/src/main/java/ast/expression/binaryexpression/EnumLineOperator.java @@ -0,0 +1,5 @@ +package ast.expression.binaryexpression; + +public enum EnumLineOperator { + PLUS, MINUS +} diff --git a/src/main/java/ast/expression/binaryexpression/EnumNonCalculationOperator.java b/src/main/java/ast/expression/binaryexpression/EnumNonCalculationOperator.java new file mode 100644 index 0000000..40c3dfd --- /dev/null +++ b/src/main/java/ast/expression/binaryexpression/EnumNonCalculationOperator.java @@ -0,0 +1,5 @@ +package ast.expression.binaryexpression; + +public enum EnumNonCalculationOperator { + AND, OR, GREATER, LESS, GREATER_EQUAL, LESS_EQUAL, EQUAL, NOT_EQUAL +} diff --git a/src/main/java/ast/expression/binaryexpression/NonCalculationExpressionNode.java b/src/main/java/ast/expression/binaryexpression/NonCalculationExpressionNode.java index 5cdebc2..f89fdf8 100644 --- a/src/main/java/ast/expression/binaryexpression/NonCalculationExpressionNode.java +++ b/src/main/java/ast/expression/binaryexpression/NonCalculationExpressionNode.java @@ -8,15 +8,35 @@ import typechecker.TypeCheckResult; public class NonCalculationExpressionNode extends BinaryExpressionNode { public UnaryExpressionNode unaryExpression; - public String operator; + public EnumNonCalculationOperator operator; public IExpressionNode expression; public NonCalculationExpressionNode(UnaryExpressionNode unaryExpression, String operator, IExpressionNode expression) { this.unaryExpression = unaryExpression; - this.operator = operator; + setOperator(operator); this.expression = expression; } + private void setOperator(String operator) { + if(operator.equals("&&")) { + this.operator = EnumNonCalculationOperator.AND; + } else if(operator.equals("||")) { + this.operator = EnumNonCalculationOperator.OR; + } else if(operator.equals(">")) { + this.operator = EnumNonCalculationOperator.GREATER; + } else if(operator.equals("<")) { + this.operator = EnumNonCalculationOperator.LESS; + } else if(operator.equals(">=")) { + this.operator = EnumNonCalculationOperator.GREATER_EQUAL; + } else if(operator.equals("<=")) { + this.operator = EnumNonCalculationOperator.LESS_EQUAL; + } else if(operator.equals("==")) { + this.operator = EnumNonCalculationOperator.EQUAL; + } else if(operator.equals("!=")) { + this.operator = EnumNonCalculationOperator.NOT_EQUAL; + } + } + @Override public TypeCheckResult accept(SemanticVisitor visitor) { return visitor.analyze(this); diff --git a/src/main/java/ast/member/MainMethodNode.java b/src/main/java/ast/member/MainMethodNode.java new file mode 100644 index 0000000..e50cc4f --- /dev/null +++ b/src/main/java/ast/member/MainMethodNode.java @@ -0,0 +1,11 @@ +package ast.member; + +import ast.block.BlockNode; + +public class MainMethodNode extends MethodNode { + public BlockNode block; + + public MainMethodNode(BlockNode block) { + this.block = block; + } +} diff --git a/src/main/java/ast/member/MethodNode.java b/src/main/java/ast/member/MethodNode.java index ecfd598..2aecf57 100644 --- a/src/main/java/ast/member/MethodNode.java +++ b/src/main/java/ast/member/MethodNode.java @@ -23,10 +23,6 @@ public class MethodNode implements MemberNode, Visitable { public MethodNode() {} - public MethodNode(BlockNode block){ - this.block = block; - } - public MethodNode(String accessModifier, ITypeNode type, Boolean voidType, String identifier, BlockNode block){ this.accesModifier = new AccessModifierNode(accessModifier); this.type = type; diff --git a/src/main/java/ast/statement/ReturnStatementNode.java b/src/main/java/ast/statement/ReturnStatementNode.java index 66d3846..e525c3f 100644 --- a/src/main/java/ast/statement/ReturnStatementNode.java +++ b/src/main/java/ast/statement/ReturnStatementNode.java @@ -1,15 +1,19 @@ package ast.statement; -import ast.ASTNode; import ast.expression.IExpressionNode; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; public class ReturnStatementNode implements IStatementNode { public IExpressionNode expression; + public Boolean voidReturn = false; public ReturnStatementNode(IExpressionNode expression) { - this.expression = expression; + if(expression != null) { + this.expression = expression; + } else { + voidReturn = true; + } } @Override diff --git a/src/main/java/ast/type/EnumTypeNode.java b/src/main/java/ast/type/EnumTypeNode.java deleted file mode 100644 index 98c38ea..0000000 --- a/src/main/java/ast/type/EnumTypeNode.java +++ /dev/null @@ -1,5 +0,0 @@ -package ast.type; - -public enum EnumTypeNode { - INT, BOOLEAN, CHAR, IDENTIFIER -} diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index 31a8b82..95ac93c 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -10,12 +10,9 @@ import ast.expression.binaryexpression.NonCalculationExpressionNode; import ast.expression.unaryexpression.MemberAccessNode; import ast.expression.unaryexpression.NotExpressionNode; import ast.expression.unaryexpression.UnaryExpressionNode; -import ast.member.FieldNode; -import ast.member.MethodNode; +import ast.member.*; import ast.statement.ifstatement.ElseStatementNode; import ast.statement.ifstatement.IfElseStatementNode; -import ast.member.ConstructorNode; -import ast.member.MemberNode; import ast.parameter.ParameterNode; import ast.statement.*; import ast.statement.ifstatement.IfStatementNode; @@ -65,7 +62,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { if(ctx.MainMethodDeclaration() != null) { - return new MethodNode((BlockNode) visit(ctx.block())); + return new MainMethodNode((BlockNode) visit(ctx.block())); } else { if(ctx.type() != null) { MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.block())); @@ -129,6 +126,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { for(SimpleJavaParser.StatementContext statement : ctx.statement()) { blockNode.addStatement((IStatementNode) visit(statement)); } + if(!blockNode.hasReturnStatement) { + blockNode.addStatement(new ReturnStatementNode(null)); + } return blockNode; }