add instVar to ast

This commit is contained in:
StefanZ3 2024-06-19 20:35:59 +02:00
parent 46ac49576e
commit 112c5f2c1b
3 changed files with 54 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package abstractSyntaxTree.Expression;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.ParameterList; import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.StatementExpression.ReceivingMethod;
import gen.DecafParser; import gen.DecafParser;
import jdk.jshell.spi.ExecutionControl; import jdk.jshell.spi.ExecutionControl;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
@ -10,15 +11,20 @@ import org.objectweb.asm.Opcodes;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class InstVarExpression implements IExpression{ public class InstVarExpression implements IExpression{
public RefType classRef; public RefType classRef;
public List<SubReceiver> receivers; ;
public List<ReceivingMethod> receivingMethods;
public String fieldName; public String fieldName;
public InstVarExpression(RefType classRef, String fieldName){ public InstVarExpression(List<SubReceiver> receivers, List<ReceivingMethod> receivingMethods, String fieldName){
this.classRef = classRef; this.receivers = receivers;
this.receivingMethods = receivingMethods;
this.fieldName = fieldName; this.fieldName = fieldName;
} }

View File

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

View File

@ -217,6 +217,18 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
return null; 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 @Override
public Node visitNewDecl(DecafParser.NewDeclContext ctx) { public Node visitNewDecl(DecafParser.NewDeclContext ctx) {
String name = ctx.Identifier().getText(); String name = ctx.Identifier().getText();
@ -316,13 +328,24 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
public Node visitAssignableExpr(DecafParser.AssignableExprContext ctx) { public Node visitAssignableExpr(DecafParser.AssignableExprContext ctx) {
if (ctx.Identifier() != null) { if (ctx.Identifier() != null) {
return new LocalVarIdentifier(ctx.Identifier().getText()); return new LocalVarIdentifier(ctx.Identifier().getText());
} else if (ctx.instVar() != null) {
return visitInstVar(ctx.instVar());
} }
return null; return null;
} }
@Override @Override
public Node visitInstVar(DecafParser.InstVarContext ctx) { public Node visitInstVar(DecafParser.InstVarContext ctx) {
return super.visitInstVar(ctx); List<SubReceiver> receivers = new ArrayList<>();
List<ReceivingMethod> 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<IExpression> generateExpressions(DecafParser.ArgumentListContext ctx) { public List<IExpression> generateExpressions(DecafParser.ArgumentListContext ctx) {