Added the implementationf of typeCheck in typedAssignment

This commit is contained in:
Ahmad 2024-05-08 10:51:58 +02:00
parent b61af71076
commit 201d6e03b9

View File

@ -13,7 +13,6 @@ import java.util.Map;
@Data
public class TypedAssignment implements TypedStatement {
private TypedId loc;
private AssignSign assignSign;
private TypedExpression value;
@Override
@ -21,15 +20,14 @@ public class TypedAssignment implements TypedStatement {
if (!localVar.containsKey(loc.getName())) {
throw new RuntimeException("Variable not declared");
}
//ASSIGN : '=';ADD_ASSIGN : '+='; SUB_ASSIGN : '-='; MUL_ASSIGN : '*=';
if (assignSign == AssignSign.ASSIGN || assignSign == AssignSign.ADD_ASSIGN ||
assignSign == AssignSign.SUB_ASSIGN || assignSign == AssignSign.MUL_ASSIGN) {
if (localVar.get(loc.getName()) != Type.INT || value.typeCheck(localVar, classes) != Type.INT) {
throw new RuntimeException("Type mismatch");
}
}
return localVar.get(loc.getName());
Type typeLeft = localVar.get(loc.getName());
Type typeRight = value.typeCheck(localVar, classes);
if (typeLeft.equals(typeRight) ) {
return typeLeft;
}
throw new RuntimeException("type of left not equals with type right");
}
@Override