moved some logic from methodcall to instvar

This commit is contained in:
Krauß, Josefine 2024-07-03 14:46:24 +02:00
parent f6358867f0
commit ba9760fe43
4 changed files with 72 additions and 17 deletions

View File

@ -10,4 +10,56 @@ class FourClasses {
}
}
class Test {
public int x;
public int y;
public Test3 test3;
public Test(int i) {
this.x = i;
this.y = 10;
this.test3 = new Test3(i * 2);
}
public Test3 getTest3() {
return this.test3;
}
public int getX() {
return this.x;
}
}
class Test2 {
public Test test;
public Test2(int i) {
this.test = new Test(i);
}
}
class Test3 {
public int x;
public int y;
public Test3(int i) {
this.x = i;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
}

View File

@ -31,11 +31,25 @@ public class InstVarExpression extends AbstractType implements IExpression{
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
if(this.receivers.get(0).identifier != null){
thisClass = localVars.get(this.receivers.get(0).identifier);
String typeOfSubreceiver = "";
if(receivers.get(0).identifier != null) {
String subreceiver = receivers.get(0).identifier;
typeOfSubreceiver = localVars.get(subreceiver);
if (typeOfSubreceiver == null)
typeContext.get(thisClass).get(subreceiver);
if (receivers.size() > 1) {
for (int i = 1; i < receivers.size(); i++) {
subreceiver = receivers.get(i).identifier;
typeOfSubreceiver = typeContext.get(typeOfSubreceiver).get(subreceiver);
}
}
}else {
typeOfSubreceiver = thisClass;
}
String varType = typeContext.get(thisClass).get(fieldName);
String varType = typeContext.get(typeOfSubreceiver).get(fieldName);
if (varType == null) {
throw new TypeCheckException("Field " + fieldName + " was not found in class " + thisClass + ".");
}

View File

@ -48,23 +48,11 @@ 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;
String typeOfSubreceiver = localVars.get(subreceiver);
if(typeOfSubreceiver == null)
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);
}
}
receiver.instVarExpression.thisClass = this.thisClass;
String typeOfSubreceiver = receiver.instVarExpression.typeCheck(methodContext, typeContext, localVars).type;
String lastField = receiver.instVarExpression.fieldName;
typeOfSubreceiver = typeContext.get(typeOfSubreceiver).get(lastField);
currentType = typeOfSubreceiver;
//currentType = typeContext.get(receiver.instVarExpression.getTypeCheckResult().type).get(mostLeftField);

View File

@ -32,6 +32,7 @@ public class NewStatementExpression extends AbstractType implements IExpression,
throw new TypeCheckException("An instance of " + className + " is created, but the type does not exist.");
}
TypeCheckResult result = new TypeCheckResult();
result.type = className;
setTypeCheckResult(result);
return result;