missing thisclass and subreceiver

This commit is contained in:
Krauß, Josefine 2024-07-03 12:32:29 +02:00
parent 1100cef668
commit 0711028dc3
6 changed files with 36 additions and 34 deletions

2
.idea/misc.xml generated
View File

@ -24,7 +24,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/target" />
</component>
</project>

View File

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

View File

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

View File

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

View File

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