Implemented typeCheck of Binary Record

This commit is contained in:
Ahmad 2024-05-02 11:27:06 +02:00
parent d812208712
commit 2b1b11b448

View File

@ -10,10 +10,25 @@ import java.util.Map;
public record Binary(Expression left, Operator op, Expression right) implements Expression { public record Binary(Expression left, Operator op, Expression right) implements Expression {
@Override @Override
public Type typeCheck(Map<String, Type> localVars, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVars, Map<String, Class> classes) {
Type type = null;
// x = y + z -> y, z : int // x = y + z -> y, z : int
if(left.typeCheck(localVars, classes) != Type.INT && right.typeCheck(localVars, classes) != Type.INT) { if(left.typeCheck(localVars, classes) == Type.INT && right.typeCheck(localVars, classes) == Type.INT) {
throw new RuntimeException("Binary operation on non-integers"); type = Type.INT;
} }
return Type.INT;
if(left.typeCheck(localVars, classes) == Type.BOOL && right.typeCheck(localVars, classes) == Type.BOOL) {
type = Type.BOOL;
}
if(left.typeCheck(localVars, classes) == Type.CHAR && right.typeCheck(localVars, classes) == Type.CHAR) {
type = Type.CHAR;
}
if(type != null){
return type;
}else{
throw new RuntimeException("Type mismatch");
}
} }
} }