Merge branch 'NewParser' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
commit
9edee73705
@ -2,6 +2,7 @@ package ast.block;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.statement.IStatementNode;
|
import ast.statement.IStatementNode;
|
||||||
|
import ast.statement.ReturnStatementNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
@ -12,6 +13,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class BlockNode implements ASTNode, Visitable {
|
public class BlockNode implements ASTNode, Visitable {
|
||||||
public List<IStatementNode> statements = new ArrayList<>();
|
public List<IStatementNode> statements = new ArrayList<>();
|
||||||
|
public Boolean hasReturnStatement = false;
|
||||||
|
|
||||||
public BlockNode() {}
|
public BlockNode() {}
|
||||||
|
|
||||||
|
@ -6,12 +6,12 @@ import typechecker.TypeCheckResult;
|
|||||||
|
|
||||||
public class CalculationExpressionNode extends BinaryExpressionNode {
|
public class CalculationExpressionNode extends BinaryExpressionNode {
|
||||||
public CalculationExpressionNode calculationExpression;
|
public CalculationExpressionNode calculationExpression;
|
||||||
public String operator;
|
public EnumLineOperator operator;
|
||||||
public DotExpressionNode dotExpression;
|
public DotExpressionNode dotExpression;
|
||||||
|
|
||||||
public CalculationExpressionNode(CalculationExpressionNode calculationExpression, String operator, DotExpressionNode dotExpression) {
|
public CalculationExpressionNode(CalculationExpressionNode calculationExpression, String operator, DotExpressionNode dotExpression) {
|
||||||
this.calculationExpression = calculationExpression;
|
this.calculationExpression = calculationExpression;
|
||||||
this.operator = operator;
|
setOperator(operator);
|
||||||
this.dotExpression = dotExpression;
|
this.dotExpression = dotExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,6 +19,14 @@ public class CalculationExpressionNode extends BinaryExpressionNode {
|
|||||||
this.dotExpression = dotExpression;
|
this.dotExpression = dotExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setOperator(String operator) {
|
||||||
|
if(operator.equals("+")) {
|
||||||
|
this.operator = EnumLineOperator.PLUS;
|
||||||
|
} else if(operator.equals("-")) {
|
||||||
|
this.operator = EnumLineOperator.MINUS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
|
@ -6,12 +6,12 @@ import typechecker.TypeCheckResult;
|
|||||||
|
|
||||||
public class DotExpressionNode extends BinaryExpressionNode {
|
public class DotExpressionNode extends BinaryExpressionNode {
|
||||||
public DotExpressionNode dotExpression;
|
public DotExpressionNode dotExpression;
|
||||||
public String operator;
|
public EnumDotOperator operator;
|
||||||
public DotSubstractionExpressionNode dotSubstractionExpression;
|
public DotSubstractionExpressionNode dotSubstractionExpression;
|
||||||
|
|
||||||
public DotExpressionNode(DotExpressionNode dotExpression, String operator, DotSubstractionExpressionNode dotSubstractionExpression) {
|
public DotExpressionNode(DotExpressionNode dotExpression, String operator, DotSubstractionExpressionNode dotSubstractionExpression) {
|
||||||
this.dotExpression = dotExpression;
|
this.dotExpression = dotExpression;
|
||||||
this.operator = operator;
|
setOperator(operator);
|
||||||
this.dotSubstractionExpression = dotSubstractionExpression;
|
this.dotSubstractionExpression = dotSubstractionExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,6 +19,16 @@ public class DotExpressionNode extends BinaryExpressionNode {
|
|||||||
this.dotSubstractionExpression = dotSubstractionExpression;
|
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
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
package ast.expression.binaryexpression;
|
||||||
|
|
||||||
|
public enum EnumDotOperator {
|
||||||
|
MULT, DIV, MOD
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package ast.expression.binaryexpression;
|
||||||
|
|
||||||
|
public enum EnumLineOperator {
|
||||||
|
PLUS, MINUS
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package ast.expression.binaryexpression;
|
||||||
|
|
||||||
|
public enum EnumNonCalculationOperator {
|
||||||
|
AND, OR, GREATER, LESS, GREATER_EQUAL, LESS_EQUAL, EQUAL, NOT_EQUAL
|
||||||
|
}
|
@ -8,15 +8,35 @@ import typechecker.TypeCheckResult;
|
|||||||
|
|
||||||
public class NonCalculationExpressionNode extends BinaryExpressionNode {
|
public class NonCalculationExpressionNode extends BinaryExpressionNode {
|
||||||
public UnaryExpressionNode unaryExpression;
|
public UnaryExpressionNode unaryExpression;
|
||||||
public String operator;
|
public EnumNonCalculationOperator operator;
|
||||||
public IExpressionNode expression;
|
public IExpressionNode expression;
|
||||||
|
|
||||||
public NonCalculationExpressionNode(UnaryExpressionNode unaryExpression, String operator, IExpressionNode expression) {
|
public NonCalculationExpressionNode(UnaryExpressionNode unaryExpression, String operator, IExpressionNode expression) {
|
||||||
this.unaryExpression = unaryExpression;
|
this.unaryExpression = unaryExpression;
|
||||||
this.operator = operator;
|
setOperator(operator);
|
||||||
this.expression = expression;
|
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
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
|
11
src/main/java/ast/member/MainMethodNode.java
Normal file
11
src/main/java/ast/member/MainMethodNode.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -23,10 +23,6 @@ public class MethodNode implements MemberNode, Visitable {
|
|||||||
|
|
||||||
public MethodNode() {}
|
public MethodNode() {}
|
||||||
|
|
||||||
public MethodNode(BlockNode block){
|
|
||||||
this.block = block;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MethodNode(String accessModifier, ITypeNode type, Boolean voidType, String identifier, BlockNode block){
|
public MethodNode(String accessModifier, ITypeNode type, Boolean voidType, String identifier, BlockNode block){
|
||||||
this.accesModifier = new AccessModifierNode(accessModifier);
|
this.accesModifier = new AccessModifierNode(accessModifier);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
@ -1,15 +1,19 @@
|
|||||||
package ast.statement;
|
package ast.statement;
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.expression.IExpressionNode;
|
import ast.expression.IExpressionNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class ReturnStatementNode implements IStatementNode {
|
public class ReturnStatementNode implements IStatementNode {
|
||||||
public IExpressionNode expression;
|
public IExpressionNode expression;
|
||||||
|
public Boolean voidReturn = false;
|
||||||
|
|
||||||
public ReturnStatementNode(IExpressionNode expression) {
|
public ReturnStatementNode(IExpressionNode expression) {
|
||||||
|
if(expression != null) {
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
|
} else {
|
||||||
|
voidReturn = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package ast.type;
|
|
||||||
|
|
||||||
public enum EnumTypeNode {
|
|
||||||
INT, BOOLEAN, CHAR, IDENTIFIER
|
|
||||||
}
|
|
@ -10,12 +10,9 @@ import ast.expression.binaryexpression.NonCalculationExpressionNode;
|
|||||||
import ast.expression.unaryexpression.MemberAccessNode;
|
import ast.expression.unaryexpression.MemberAccessNode;
|
||||||
import ast.expression.unaryexpression.NotExpressionNode;
|
import ast.expression.unaryexpression.NotExpressionNode;
|
||||||
import ast.expression.unaryexpression.UnaryExpressionNode;
|
import ast.expression.unaryexpression.UnaryExpressionNode;
|
||||||
import ast.member.FieldNode;
|
import ast.member.*;
|
||||||
import ast.member.MethodNode;
|
|
||||||
import ast.statement.ifstatement.ElseStatementNode;
|
import ast.statement.ifstatement.ElseStatementNode;
|
||||||
import ast.statement.ifstatement.IfElseStatementNode;
|
import ast.statement.ifstatement.IfElseStatementNode;
|
||||||
import ast.member.ConstructorNode;
|
|
||||||
import ast.member.MemberNode;
|
|
||||||
import ast.parameter.ParameterNode;
|
import ast.parameter.ParameterNode;
|
||||||
import ast.statement.*;
|
import ast.statement.*;
|
||||||
import ast.statement.ifstatement.IfStatementNode;
|
import ast.statement.ifstatement.IfStatementNode;
|
||||||
@ -65,7 +62,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
@Override
|
@Override
|
||||||
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
|
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
|
||||||
if(ctx.MainMethodDeclaration() != null) {
|
if(ctx.MainMethodDeclaration() != null) {
|
||||||
return new MethodNode((BlockNode) visit(ctx.block()));
|
return new MainMethodNode((BlockNode) visit(ctx.block()));
|
||||||
} else {
|
} else {
|
||||||
if(ctx.type() != null) {
|
if(ctx.type() != null) {
|
||||||
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.block()));
|
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<ASTNode> {
|
|||||||
for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
|
for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
|
||||||
blockNode.addStatement((IStatementNode) visit(statement));
|
blockNode.addStatement((IStatementNode) visit(statement));
|
||||||
}
|
}
|
||||||
|
if(!blockNode.hasReturnStatement) {
|
||||||
|
blockNode.addStatement(new ReturnStatementNode(null));
|
||||||
|
}
|
||||||
return blockNode;
|
return blockNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user