This commit is contained in:
Julian Murek 2024-07-03 14:48:46 +02:00
commit d7016df1ba
4 changed files with 73 additions and 19 deletions

View File

@ -1,4 +1,3 @@
/*
class FourClasses { class FourClasses {
public int notmain(int i) { public int notmain(int i) {
@ -11,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 @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException { 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){ String typeOfSubreceiver = "";
thisClass = localVars.get(this.receivers.get(0).identifier); 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) { if (varType == null) {
throw new TypeCheckException("Field " + fieldName + " was not found in class " + thisClass + "."); 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 // receiver is instvar
if (receiver != null) { if (receiver != null) {
if (receiver.instVarExpression != null) { if (receiver.instVarExpression != null) {
String subreceiver = receiver.instVarExpression.receivers.get(0).identifier; receiver.instVarExpression.thisClass = this.thisClass;
String typeOfSubreceiver = localVars.get(subreceiver); String typeOfSubreceiver = receiver.instVarExpression.typeCheck(methodContext, typeContext, localVars).type;
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);
}
}
String lastField = receiver.instVarExpression.fieldName; String lastField = receiver.instVarExpression.fieldName;
typeOfSubreceiver = typeContext.get(typeOfSubreceiver).get(lastField);
currentType = typeOfSubreceiver; currentType = typeOfSubreceiver;
//currentType = typeContext.get(receiver.instVarExpression.getTypeCheckResult().type).get(mostLeftField); //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."); throw new TypeCheckException("An instance of " + className + " is created, but the type does not exist.");
} }
TypeCheckResult result = new TypeCheckResult(); TypeCheckResult result = new TypeCheckResult();
result.type = className; result.type = className;
setTypeCheckResult(result); setTypeCheckResult(result);
return result; return result;