From 0711028dc325b7e1344296d30241329f9293be98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Wed, 3 Jul 2024 12:32:29 +0200 Subject: [PATCH] missing thisclass and subreceiver --- .idea/misc.xml | 2 +- src/main/java/TestClass.java | 33 ++++--------------- .../Statement/BlockStatement.java | 3 ++ .../Statement/ReturnStatement.java | 7 ++++ .../AssignStatementExpression.java | 2 +- .../MethodCallStatementExpression.java | 23 +++++++++---- 6 files changed, 36 insertions(+), 34 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 32ca2da..aec221f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -24,7 +24,7 @@ - + \ No newline at end of file diff --git a/src/main/java/TestClass.java b/src/main/java/TestClass.java index 375caa8..6310ef2 100644 --- a/src/main/java/TestClass.java +++ b/src/main/java/TestClass.java @@ -1,32 +1,13 @@ -class TestClass { - int i; - int a = 12345; - Example e; - public TestClass(){i = 3456;} - int meth(int n){ - Example e = new Example(); - e.example1 = new TestClass(); - int abc = e.example1.m1(2).m2().m3(); +class FourClasses { - return 0; - } - Example m1(int n){ - return new Example(); + public int notmain(int i) { + Test t = new Test(i); + Test2 t2 = new Test2(t.y); + return t2.test.test3.getX(); } + public static void main(String[] args){ - TestClass t2 = new TestClass(); - t2.meth(2); - } -} -class Example { - int i; - boolean b; - char c; - TestClass example1; - TestClass m(int a){ - return new TestClass(); } - Example m2(){return new Example();} - int m3(){return 1;} + } \ No newline at end of file diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index 7ab66d9..bffe5c2 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -63,6 +63,9 @@ public class BlockStatement extends AbstractType implements IStatement { if (statement instanceof PrintStatement printStatement) { printStatement.thisClass = thisClass; } + if (statement instanceof ReturnStatement returnStatement) { + returnStatement.thisClass = thisClass; + } TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); if(statement instanceof MethodCallStatementExpression methodCall){ diff --git a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java index 1f39162..48e79fa 100644 --- a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java @@ -4,7 +4,9 @@ import TypeCheck.TypeCheckException; import TypeCheck.TypeCheckResult; import TypeCheck.AbstractType; import abstractSyntaxTree.Expression.IExpression; +import abstractSyntaxTree.Expression.InstVarExpression; import abstractSyntaxTree.Parameter.ParameterList; +import abstractSyntaxTree.StatementExpression.MethodCallStatementExpression; import org.objectweb.asm.*; import java.util.HashMap; @@ -14,6 +16,7 @@ import java.util.Objects; public class ReturnStatement extends AbstractType implements IStatement{ IExpression expression; + public String thisClass; public ReturnStatement(IExpression expression) { this.expression = expression; @@ -26,6 +29,10 @@ public class ReturnStatement extends AbstractType implements IStatement{ if (expression == null) { result.type = "void"; } else { + if(expression instanceof MethodCallStatementExpression methodCallStatementExpression) + methodCallStatementExpression.thisClass = this.thisClass; + else if(expression instanceof InstVarExpression instVarExpression) + instVarExpression.thisClass = this.thisClass; TypeCheckResult typedExpression = expression.typeCheck(methodContext, typeContext, localVars); result.type = typedExpression.type; } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index ffabadb..448d87a 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -44,7 +44,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi if(leftType.type == null){ leftType.type = typeContext.get(thisClass).get(identifier); } - }else{ + }else { if(left instanceof InstVarExpression instVarExpression) instVarExpression.thisClass = this.thisClass; leftType = left.typeCheck(methodContext, typeContext, localVars); diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java index 1107172..7ef11db 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -48,14 +48,25 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr // receiver is instvar if (receiver != null) { if (receiver.instVarExpression != null) { - String Subreceiver = receiver.instVarExpression.receivers.get(0).identifier; // e - String mostLeftField = receiver.instVarExpression.fieldName; // example1 + String subreceiver = receiver.instVarExpression.receivers.get(0).identifier; + String typeOfSubreceiver = localVars.get(subreceiver); + if(typeOfSubreceiver == null) + typeContext.get(thisClass).get(subreceiver); - String typeOfSubreceiver = typeContext.get(thisClass).get(Subreceiver); + if(receiver.instVarExpression.receivers.size() > 1){ + for(int i = 1; i < receiver.instVarExpression.receivers.size(); i++) { + subreceiver = receiver.instVarExpression.receivers.get(i).identifier; + typeOfSubreceiver = typeContext.get(typeOfSubreceiver).get(subreceiver); + + } + } + + String lastField = receiver.instVarExpression.fieldName; + + typeOfSubreceiver = typeContext.get(typeOfSubreceiver).get(lastField); + + currentType = typeOfSubreceiver; - receiver.instVarExpression.thisClass = typeOfSubreceiver; - receiver.instVarExpression.typeCheck(methodContext, typeContext, localVars); - currentType = typeContext.get(typeOfSubreceiver).get(mostLeftField); //currentType = typeContext.get(receiver.instVarExpression.getTypeCheckResult().type).get(mostLeftField); } else { currentType = classToSearchMethodIn;