Updated the TypeCheck of TypedAssignement

This commit is contained in:
ahmad 2024-05-11 11:45:47 +02:00
parent cdbb3ae55f
commit 34a20668b6

View File

@ -23,6 +23,7 @@ public class TypedAssignment implements TypedStatement {
public TypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) { public TypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) {
convertToTypedAssignment(localVar, clas, untyped); convertToTypedAssignment(localVar, clas, untyped);
} }
public void convertToTypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) { public void convertToTypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) {
varName = untyped.location().id(); varName = untyped.location().id();
value = getKindOfExpression(localVar, clas, untyped.value()); value = getKindOfExpression(localVar, clas, untyped.value());
@ -30,13 +31,20 @@ public class TypedAssignment implements TypedStatement {
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
Type typeLeft = null;
if (!localVar.containsKey(varName)) { if (!localVar.containsKey(varName)) {
throw new RuntimeException("Variable not declared"); if(clas.isThereField(varName)){
typeLeft = clas.getFieldType(varName);
}else {
throw new RuntimeException("Variable not declared");
}
} else {
typeLeft = localVar.get(varName);
} }
Type typeLeft = localVar.get(varName);
Type typeRight = value.typeCheck(localVar, clas); Type typeRight = value.typeCheck(localVar, clas);
if (typeLeft.equals(typeRight) ) { if (typeLeft.equals(typeRight)) {
type = typeLeft; type = typeLeft;
return typeLeft; return typeLeft;
} }