From 112c5f2c1bd0d6bee604bc42f69f7af005e064e7 Mon Sep 17 00:00:00 2001 From: StefanZ3 Date: Wed, 19 Jun 2024 20:35:59 +0200 Subject: [PATCH] add instVar to ast --- .../Expression/InstVarExpression.java | 10 ++++++-- .../Expression/SubReceiver.java | 22 ++++++++++++++++ src/main/java/astGenerator/ASTGenerator.java | 25 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/main/java/abstractSyntaxTree/Expression/SubReceiver.java diff --git a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java index 2e65e76..95842e3 100644 --- a/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java +++ b/src/main/java/abstractSyntaxTree/Expression/InstVarExpression.java @@ -3,6 +3,7 @@ package abstractSyntaxTree.Expression; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Parameter.ParameterList; +import abstractSyntaxTree.StatementExpression.ReceivingMethod; import gen.DecafParser; import jdk.jshell.spi.ExecutionControl; import org.objectweb.asm.MethodVisitor; @@ -10,15 +11,20 @@ import org.objectweb.asm.Opcodes; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.List; import java.util.Objects; public class InstVarExpression implements IExpression{ public RefType classRef; + + public List receivers; ; + public List receivingMethods; public String fieldName; - public InstVarExpression(RefType classRef, String fieldName){ - this.classRef = classRef; + public InstVarExpression(List receivers, List receivingMethods, String fieldName){ + this.receivers = receivers; + this.receivingMethods = receivingMethods; this.fieldName = fieldName; } diff --git a/src/main/java/abstractSyntaxTree/Expression/SubReceiver.java b/src/main/java/abstractSyntaxTree/Expression/SubReceiver.java new file mode 100644 index 0000000..cfbce8a --- /dev/null +++ b/src/main/java/abstractSyntaxTree/Expression/SubReceiver.java @@ -0,0 +1,22 @@ +package abstractSyntaxTree.Expression; + +import abstractSyntaxTree.Node; +import abstractSyntaxTree.StatementExpression.NewStatementExpression; + +public class SubReceiver implements Node { + boolean thisExpression; + NewStatementExpression newStatementExpression; + String identifier; + + public SubReceiver(boolean thisExpression) { + this.thisExpression = thisExpression; + } + + public SubReceiver(NewStatementExpression newStatementExpression) { + this.newStatementExpression = newStatementExpression; + } + + public SubReceiver(String identifier) { + this.identifier = identifier; + } +} diff --git a/src/main/java/astGenerator/ASTGenerator.java b/src/main/java/astGenerator/ASTGenerator.java index 820ee03..fdcaf63 100644 --- a/src/main/java/astGenerator/ASTGenerator.java +++ b/src/main/java/astGenerator/ASTGenerator.java @@ -217,6 +217,18 @@ public class ASTGenerator extends DecafBaseVisitor { return null; } + @Override + public Node visitSubReceiver(DecafParser.SubReceiverContext ctx) { + if (ctx.This() != null) { + return new SubReceiver(true); + } else if (ctx.newDecl() != null) { + return new SubReceiver((NewStatementExpression) visit(ctx.newDecl())); + } else if (ctx.Identifier() != null) { + return new SubReceiver(ctx.Identifier().getText()); + } + return null; + } + @Override public Node visitNewDecl(DecafParser.NewDeclContext ctx) { String name = ctx.Identifier().getText(); @@ -316,13 +328,24 @@ public class ASTGenerator extends DecafBaseVisitor { public Node visitAssignableExpr(DecafParser.AssignableExprContext ctx) { if (ctx.Identifier() != null) { return new LocalVarIdentifier(ctx.Identifier().getText()); + } else if (ctx.instVar() != null) { + return visitInstVar(ctx.instVar()); } return null; } @Override public Node visitInstVar(DecafParser.InstVarContext ctx) { - return super.visitInstVar(ctx); + List receivers = new ArrayList<>(); + List receivingMethods = new ArrayList<>(); + String fieldName = ctx.Identifier().getText(); + for(DecafParser.SubReceiverContext subReceiver : ctx.subReceiver()) { + receivers.add((SubReceiver) visit(subReceiver)); + } + for(DecafParser.ReceivingMethodContext receivingMethod: ctx.receivingMethod()) { + receivingMethods.add((ReceivingMethod) visit(receivingMethod)); + } + return new InstVarExpression(receivers, receivingMethods, fieldName); } public List generateExpressions(DecafParser.ArgumentListContext ctx) {