johns-branch #12

Merged
i22005 merged 23 commits from johns-branch into main 2024-06-21 16:30:56 +00:00
21 changed files with 462 additions and 35 deletions
Showing only changes of commit bf7a642233 - Show all commits

View File

@ -1,4 +0,0 @@
package ast.expression;
public class AssignableExpressionNode {
}

View File

@ -0,0 +1,4 @@
package ast.expression.binaryexpression;
public class BinaryExpression {
}

View File

@ -0,0 +1,19 @@
package ast.expression.binaryexpression;
import ast.ASTNode;
public class CalculationExpressionNode implements ASTNode {
CalculationExpressionNode calculationExpression;
String operator;
DotExpressionNode dotExpression;
public CalculationExpressionNode(CalculationExpressionNode calculationExpression, String operator, DotExpressionNode dotExpression) {
this.calculationExpression = calculationExpression;
this.operator = operator;
this.dotExpression = dotExpression;
}
public CalculationExpressionNode(DotExpressionNode dotExpression) {
this.dotExpression = dotExpression;
}
}

View File

@ -0,0 +1,19 @@
package ast.expression.binaryexpression;
import ast.ASTNode;
public class DotExpressionNode implements ASTNode {
DotExpressionNode dotExpression;
String operator;
DotSubstractionExpressionNode dotSubstractionExpression;
public DotExpressionNode(DotExpressionNode dotExpression, String operator, DotSubstractionExpressionNode dotSubstractionExpression) {
this.dotExpression = dotExpression;
this.operator = operator;
this.dotSubstractionExpression = dotSubstractionExpression;
}
public DotExpressionNode(DotSubstractionExpressionNode dotSubstractionExpression) {
this.dotSubstractionExpression = dotSubstractionExpression;
}
}

View File

@ -0,0 +1,31 @@
package ast.expression.binaryexpression;
import ast.ASTNode;
import ast.expression.unaryexpression.MemberAccessNode;
import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode;
import ast.type.ValueNode;
public class DotSubstractionExpressionNode implements ASTNode {
ValueNode value;
String identifier;
MemberAccessNode memberAccess;
MethodCallStatementExpressionNode methodCall;
CalculationExpressionNode calculationExpression;
public DotSubstractionExpressionNode(ValueNode value) {
this.value = value;
}
public DotSubstractionExpressionNode(String identifier) {
this.identifier = identifier;
}
public DotSubstractionExpressionNode(MemberAccessNode memberAccess) {
this.memberAccess = memberAccess;
}
public DotSubstractionExpressionNode(MethodCallStatementExpressionNode methodCall, CalculationExpressionNode calculationExpression) {
this.methodCall = methodCall;
this.calculationExpression = calculationExpression;
}
}

View File

@ -0,0 +1,17 @@
package ast.expression.binaryexpression;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import ast.expression.unaryexpression.UnaryExpressionNode;
public class NonCalculationExpressionNode implements ASTNode {
UnaryExpressionNode unaryExpression;
String operator;
ExpressionNode expression;
public NonCalculationExpressionNode(UnaryExpressionNode unaryExpression, String operator, ExpressionNode expression) {
this.unaryExpression = unaryExpression;
this.operator = operator;
this.expression = expression;
}
}

View File

@ -0,0 +1,19 @@
package ast.expression.unaryexpression;
import ast.ASTNode;
import java.util.ArrayList;
import java.util.List;
public class MemberAccessNode implements ASTNode {
Boolean thisExpr;
List<String> identifiers = new ArrayList<>();
public MemberAccessNode(Boolean thisExpr) {
this.thisExpr = thisExpr;
}
public void addIdentifier(String identifier) {
identifiers.add(identifier);
}
}

View File

@ -0,0 +1,12 @@
package ast.expression.unaryexpression;
import ast.ASTNode;
import ast.expression.ExpressionNode;
public class NotExpressionNode implements ASTNode {
ExpressionNode expression;
public NotExpressionNode(ExpressionNode expression) {
this.expression = expression;
}
}

View File

@ -0,0 +1,46 @@
package ast.expression.unaryexpression;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import ast.statement.StatementNode;
import ast.type.ValueNode;
import java.util.Objects;
public class UnaryExpressionNode implements ASTNode {
String thisExp;
String identifier;
MemberAccessNode memberAccess;
ValueNode value;
NotExpressionNode notExpression;
StatementNode statement;
ExpressionNode expression;
public UnaryExpressionNode(String value) {
if(Objects.equals(value, "this")) {
this.thisExp = "this";
} else {
this.identifier = value;
}
}
public UnaryExpressionNode(MemberAccessNode memberAccess) {
this.memberAccess = memberAccess;
}
public UnaryExpressionNode(ValueNode value) {
this.value = value;
}
public UnaryExpressionNode(NotExpressionNode notExpression) {
this.notExpression = notExpression;
}
public UnaryExpressionNode(StatementNode statement) {
this.statement = statement;
}
public UnaryExpressionNode(ExpressionNode expression) {
this.expression = expression;
}
}

View File

@ -1,7 +1,6 @@
package ast.statement.statementexpression; package ast.statement.statementexpression;
import ast.ASTNode; import ast.ASTNode;
import ast.expression.AssignableExpressionNode;
import ast.expression.ExpressionNode; import ast.expression.ExpressionNode;
public class AssignStatementExpressionNode implements ASTNode { public class AssignStatementExpressionNode implements ASTNode {

View File

@ -0,0 +1,17 @@
package ast.statement.statementexpression;
import ast.ASTNode;
import ast.expression.unaryexpression.MemberAccessNode;
public class AssignableExpressionNode implements ASTNode {
String identifier;
MemberAccessNode memberAccess;
public AssignableExpressionNode(String identifier) {
this.identifier = identifier;
}
public AssignableExpressionNode(MemberAccessNode memberAccess) {
this.memberAccess = memberAccess;
}
}

View File

@ -1,20 +0,0 @@
package ast.statement.statementexpression;
import ast.ASTNode;
import ast.expression.ExpressionNode;
import java.util.ArrayList;
import java.util.List;
public class CrementExpressionStatementExpressionNode implements ASTNode {
String identifier;
List<ExpressionNode> expressions = new ArrayList<>();
public CrementExpressionStatementExpressionNode(String identifier) {
this.identifier = identifier;
}
public void addExpression(ExpressionNode expression) {
expressions.add(expression);
}
}

View File

@ -0,0 +1,5 @@
package ast.statement.statementexpression;
public class IncrementExpression {
}

View File

@ -0,0 +1,5 @@
package ast.statement.statementexpression.crementExpression;
public enum CrementType {
PREFIX, SUFFIX
}

View File

@ -0,0 +1,13 @@
package ast.statement.statementexpression.crementExpression;
import ast.ASTNode;
import ast.statement.statementexpression.AssignableExpressionNode;
public class DecrementExpressionNode implements ASTNode {
CrementType crementType;
AssignableExpressionNode assignableExpression;
public DecrementExpressionNode(CrementType crementType, AssignableExpressionNode assignableExpression) {
this.assignableExpression = assignableExpression;
}
}

View File

@ -0,0 +1,13 @@
package ast.statement.statementexpression.crementExpression;
import ast.ASTNode;
import ast.statement.statementexpression.AssignableExpressionNode;
public class IncrementExpressionNode implements ASTNode {
CrementType crementType;
AssignableExpressionNode assignableExpression;
public IncrementExpressionNode(CrementType crementType, AssignableExpressionNode assignableExpression) {
this.assignableExpression = assignableExpression;
}
}

View File

@ -1,7 +1,28 @@
package ast.statement.statementexpression.methodcallstatementnexpression; package ast.statement.statementexpression.methodcallstatementnexpression;
public class TargetNode { import ast.ASTNode;
String thisTarget; import ast.expression.unaryexpression.MemberAccessNode;
import ast.statement.statementexpression.NewDeclarationStatementExpressionNode;
public class TargetNode implements ASTNode {
Boolean thisTar;
MemberAccessNode memberAccess;
NewDeclarationStatementExpressionNode newDeclaration;
String identifier;
public TargetNode(Boolean thisTar) {
this.thisTar = thisTar;
}
public TargetNode(MemberAccessNode memberAccess) {
this.memberAccess = memberAccess;
}
public TargetNode(NewDeclarationStatementExpressionNode newDeclaration) {
this.newDeclaration = newDeclaration;
}
public TargetNode(String identifier) {
this.identifier = identifier;
}
} }

View File

@ -0,0 +1,5 @@
package ast.type;
public enum EnumValueNode {
INT_VALUE, BOOLEAN_VALUE, CHAR_VALUE, NULL_VALUE
}

View File

@ -0,0 +1,13 @@
package ast.type;
import ast.ASTNode;
public class ValueNode implements ASTNode {
EnumValueNode valueType;
String value;
public ValueNode(EnumValueNode valueType, String value) {
this.valueType = valueType;
this.value = value;
}
}

View File

@ -2,8 +2,14 @@ package parser.astBuilder;
import ast.*; import ast.*;
import ast.block.BlockNode; import ast.block.BlockNode;
import ast.expression.AssignableExpressionNode; import ast.expression.*;
import ast.expression.ExpressionNode; import ast.expression.binaryexpression.CalculationExpressionNode;
import ast.expression.binaryexpression.DotExpressionNode;
import ast.expression.binaryexpression.DotSubstractionExpressionNode;
import ast.expression.binaryexpression.NonCalculationExpressionNode;
import ast.expression.unaryexpression.MemberAccessNode;
import ast.expression.unaryexpression.NotExpressionNode;
import ast.expression.unaryexpression.UnaryExpressionNode;
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.ConstructorNode;
@ -12,11 +18,16 @@ import ast.parameter.ParameterNode;
import ast.statement.*; import ast.statement.*;
import ast.statement.ifstatement.IfStatementNode; import ast.statement.ifstatement.IfStatementNode;
import ast.statement.statementexpression.AssignStatementExpressionNode; import ast.statement.statementexpression.AssignStatementExpressionNode;
import ast.statement.statementexpression.CrementExpressionStatementExpressionNode; import ast.statement.statementexpression.AssignableExpressionNode;
import ast.statement.statementexpression.NewDeclarationStatementExpressionNode;
import ast.statement.statementexpression.crementExpression.CrementType;
import ast.statement.statementexpression.crementExpression.DecrementExpressionNode;
import ast.statement.statementexpression.crementExpression.IncrementExpressionNode;
import ast.statement.statementexpression.methodcallstatementnexpression.ChainedMethodNode; import ast.statement.statementexpression.methodcallstatementnexpression.ChainedMethodNode;
import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode; import ast.statement.statementexpression.methodcallstatementnexpression.MethodCallStatementExpressionNode;
import ast.statement.statementexpression.methodcallstatementnexpression.TargetNode; import ast.statement.statementexpression.methodcallstatementnexpression.TargetNode;
import ast.type.*; import ast.type.*;
import org.antlr.v4.runtime.tree.TerminalNode;
import parser.generated.*; import parser.generated.*;
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> { public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@ -147,11 +158,11 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) { public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) {
CrementExpressionStatementExpressionNode crementExpressionStatementNode = new CrementExpressionStatementExpressionNode(ctx.Identifier().getText()); NewDeclarationStatementExpressionNode newDeclarationStatementExpressionNode = new NewDeclarationStatementExpressionNode(ctx.Identifier().getText());
for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) { for(SimpleJavaParser.ExpressionContext expression : ctx.argumentList().expression()) {
crementExpressionStatementNode.addExpression((ExpressionNode) visit(expression)); newDeclarationStatementExpressionNode.addExpression((ExpressionNode) visit(expression));
} }
return crementExpressionStatementNode; return newDeclarationStatementExpressionNode;
} }
@Override @Override
@ -168,7 +179,16 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitTarget(SimpleJavaParser.TargetContext ctx) { public ASTNode visitTarget(SimpleJavaParser.TargetContext ctx) {
if(ctx.This() != null) {
return new TargetNode(true);
} else if(ctx.memberAccess() != null) {
return new TargetNode((MemberAccessNode) visit(ctx.memberAccess()));
} else if(ctx.newDeclaration() != null) {
return new TargetNode((NewDeclarationStatementExpressionNode) visit(ctx.newDeclaration()));
} else if(ctx.Identifier() != null) {
return new TargetNode(ctx.Identifier().getText());
}
return null;
} }
@Override @Override
@ -179,4 +199,177 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
return chainedMethodNode; return chainedMethodNode;
} }
@Override
public ASTNode visitCrementExpression(SimpleJavaParser.CrementExpressionContext ctx) {
if(ctx.incrementExpression() != null) {
return visitIncrementExpression(ctx.incrementExpression());
} else if(ctx.decrementExpression() != null) {
return visitDecrementExpression(ctx.decrementExpression());
}
return null;
}
@Override
public ASTNode visitIncrementExpression(SimpleJavaParser.IncrementExpressionContext ctx) {
if(ctx.prefixIncrementExpression() != null) {
return visitPrefixIncrementExpression(ctx.prefixIncrementExpression());
} else if(ctx.suffixIncrementExpression() != null) {
return visitSuffixIncrementExpression(ctx.suffixIncrementExpression());
}
return null;
}
@Override
public ASTNode visitPrefixIncrementExpression(SimpleJavaParser.PrefixIncrementExpressionContext ctx) {
return new IncrementExpressionNode(CrementType.PREFIX, (AssignableExpressionNode) visit(ctx.assignableExpression()));
}
@Override
public ASTNode visitSuffixIncrementExpression(SimpleJavaParser.SuffixIncrementExpressionContext ctx) {
return new IncrementExpressionNode(CrementType.SUFFIX, (AssignableExpressionNode) visit(ctx.assignableExpression()));
}
@Override
public ASTNode visitDecrementExpression(SimpleJavaParser.DecrementExpressionContext ctx) {
if(ctx.prefixDecrementExpression() != null) {
return visitPrefixDecrementExpression(ctx.prefixDecrementExpression());
} else if(ctx.suffixDecrementExpression() != null) {
return visitSuffixDecrementExpression(ctx.suffixDecrementExpression());
}
return null;
}
@Override
public ASTNode visitPrefixDecrementExpression(SimpleJavaParser.PrefixDecrementExpressionContext ctx) {
return new DecrementExpressionNode(CrementType.PREFIX, (AssignableExpressionNode) visit(ctx.assignableExpression()));
}
@Override
public ASTNode visitSuffixDecrementExpression(SimpleJavaParser.SuffixDecrementExpressionContext ctx) {
return new DecrementExpressionNode(CrementType.SUFFIX, (AssignableExpressionNode) visit(ctx.assignableExpression()));
}
@Override
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
if(ctx.unaryExpression() != null) {
return visit(ctx.unaryExpression());
} else if(ctx.binaryExpression() != null) {
return visit(ctx.binaryExpression());
}
return null;
}
@Override
public ASTNode visitUnaryExpression(SimpleJavaParser.UnaryExpressionContext ctx) {
if(ctx.This() != null) {
return new UnaryExpressionNode(ctx.This().getText());
} else if(ctx.Identifier() != null) {
return new UnaryExpressionNode(ctx.Identifier().getText());
} else if(ctx.memberAccess() != null) {
return new UnaryExpressionNode((MemberAccessNode) visitMemberAccess(ctx.memberAccess()));
} else if(ctx.value() != null) {
return new UnaryExpressionNode((ValueNode) visitValue(ctx.value()));
} else if(ctx.notExpression() != null) {
return new UnaryExpressionNode((NotExpressionNode) visitNotExpression(ctx.notExpression()));
} else if(ctx.statementExpression() != null) {
return new UnaryExpressionNode((StatementNode) visitStatementExpression(ctx.statementExpression()));
} else if(ctx.expression() != null) {
return new UnaryExpressionNode((ExpressionNode) visitExpression(ctx.expression()));
}
return null;
}
@Override
public ASTNode visitMemberAccess(SimpleJavaParser.MemberAccessContext ctx) {
MemberAccessNode memberAccessNode;
if(ctx.This() != null) {
memberAccessNode = new MemberAccessNode(true);
} else {
memberAccessNode = new MemberAccessNode(false);
}
for (TerminalNode identifierNode : ctx.Identifier()) {
memberAccessNode.addIdentifier(identifierNode.getText());
}
return memberAccessNode;
}
@Override
public ASTNode visitValue(SimpleJavaParser.ValueContext ctx) {
if(ctx.IntValue() != null) {
return new ValueNode(EnumValueNode.INT_VALUE, ctx.IntValue().getText());
} else if(ctx.BooleanValue() != null) {
return new ValueNode(EnumValueNode.BOOLEAN_VALUE, ctx.BooleanValue().getText());
} else if(ctx.CharValue() != null) {
return new ValueNode(EnumValueNode.CHAR_VALUE, ctx.CharValue().getText());
} else if(ctx.NullValue() != null) {
return new ValueNode(EnumValueNode.NULL_VALUE, ctx.NullValue().getText());
}
return null;
}
@Override
public ASTNode visitNotExpression(SimpleJavaParser.NotExpressionContext ctx) {
return new NotExpressionNode((ExpressionNode) visitExpression(ctx.expression()));
}
@Override
public ASTNode visitBinaryExpression(SimpleJavaParser.BinaryExpressionContext ctx) {
if(ctx.calculationExpression() != null) {
return visit(ctx.calculationExpression());
} else if(ctx.nonCalculationExpression() != null) {
return visit(ctx.nonCalculationExpression());
}
return null;
}
@Override
public ASTNode visitCalculationExpression(SimpleJavaParser.CalculationExpressionContext ctx) {
if(ctx.calculationExpression() != null) {
return new CalculationExpressionNode((CalculationExpressionNode) visit(ctx.calculationExpression()), ctx.LineOperator().getText(), (DotExpressionNode) visit(ctx.dotExpression()));
} else if(ctx.dotExpression() != null) {
return new CalculationExpressionNode((DotExpressionNode) visit(ctx.dotExpression()));
}
return null;
}
@Override
public ASTNode visitDotExpression(SimpleJavaParser.DotExpressionContext ctx) {
if(ctx.dotExpression() != null) {
return new DotExpressionNode((DotExpressionNode) visit(ctx.dotExpression()), ctx.DotOperator().getText(), (DotSubstractionExpressionNode) visit(ctx.dotSubtractionExpression()));
} else if(ctx.dotSubtractionExpression() != null) {
return new DotExpressionNode((DotSubstractionExpressionNode) visit(ctx.dotSubtractionExpression()));
}
return null;
}
@Override
public ASTNode visitDotSubtractionExpression(SimpleJavaParser.DotSubtractionExpressionContext ctx) {
if(ctx.IntValue() != null) {
return new DotSubstractionExpressionNode(new ValueNode(EnumValueNode.INT_VALUE, ctx.IntValue().getText()));
} else if(ctx.Identifier() != null) {
return new DotSubstractionExpressionNode(ctx.Identifier().getText());
} else if(ctx.memberAccess() != null) {
return new DotSubstractionExpressionNode((MemberAccessNode) visit(ctx.memberAccess()));
} else if(ctx.methodCall() != null && ctx.calculationExpression() != null) {
return new DotSubstractionExpressionNode((MethodCallStatementExpressionNode) visit(ctx.methodCall()), (CalculationExpressionNode) visit(ctx.calculationExpression()));
}
return null;
}
@Override
public ASTNode visitNonCalculationExpression(SimpleJavaParser.NonCalculationExpressionContext ctx) {
return new NonCalculationExpressionNode((UnaryExpressionNode) visit(ctx.unaryExpression()), ctx.nonCalculationOperator().getText(), (ExpressionNode) visit(ctx.expression()));
}
@Override
public ASTNode visitAssignableExpression(SimpleJavaParser.AssignableExpressionContext ctx) {
if(ctx.Identifier() != null) {
return new AssignableExpressionNode(ctx.Identifier().getText());
} else if(ctx.memberAccess() != null) {
return new AssignableExpressionNode((MemberAccessNode) visit(ctx.memberAccess()));
}
return null;
}
} }

View File

@ -7,7 +7,7 @@ import ast.ProgramNode;
import ast.expression.BinaryExpressionNode; import ast.expression.BinaryExpressionNode;
import ast.expression.IdentifierExpressionNode; import ast.expression.IdentifierExpressionNode;
import ast.expression.InstVar; import ast.expression.InstVar;
import ast.expression.UnaryExpressionNode; import ast.expression.unaryexpression.UnaryExpressionNode;
import ast.member.FieldNode; import ast.member.FieldNode;
import ast.member.MethodNode; import ast.member.MethodNode;
import ast.statement.*; import ast.statement.*;