Merge branch 'NewParser' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Bruder John 2024-06-21 18:07:18 +02:00
commit 9edee73705
12 changed files with 83 additions and 22 deletions

View File

@ -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<IStatementNode> statements = new ArrayList<>();
public Boolean hasReturnStatement = false;
public BlockNode() {}

View File

@ -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);

View File

@ -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);

View File

@ -0,0 +1,5 @@
package ast.expression.binaryexpression;
public enum EnumDotOperator {
MULT, DIV, MOD
}

View File

@ -0,0 +1,5 @@
package ast.expression.binaryexpression;
public enum EnumLineOperator {
PLUS, MINUS
}

View File

@ -0,0 +1,5 @@
package ast.expression.binaryexpression;
public enum EnumNonCalculationOperator {
AND, OR, GREATER, LESS, GREATER_EQUAL, LESS_EQUAL, EQUAL, NOT_EQUAL
}

View File

@ -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);

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

View File

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

View File

@ -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

View File

@ -1,5 +0,0 @@
package ast.type;
public enum EnumTypeNode {
INT, BOOLEAN, CHAR, IDENTIFIER
}

View File

@ -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<ASTNode> {
@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<ASTNode> {
for(SimpleJavaParser.StatementContext statement : ctx.statement()) {
blockNode.addStatement((IStatementNode) visit(statement));
}
if(!blockNode.hasReturnStatement) {
blockNode.addStatement(new ReturnStatementNode(null));
}
return blockNode;
}