mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-27 10:18:04 +00:00
Implemented typeCheck of Binary Record
This commit is contained in:
parent
d812208712
commit
2b1b11b448
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user