-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryOp.java
76 lines (61 loc) · 2.85 KB
/
BinaryOp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class BinaryOp extends SuperToken implements Token{
Expr lhs;
Expr rhs;
String operator;
public BinaryOp(Expr l, Expr r, String op){
this.lhs = l;
this.rhs = r;
this.operator = op;
}
public String toString(int t){
return "(" + lhs.toString(t) + " " + operator + " " + rhs.toString(t) + ")";
}
public VarType typeCheck() throws Exception{
if (operator.equals("+") || operator.equals("-") || operator.equals("*") || operator.equals("/")){
VarType lhsType = lhs.typeCheck();
VarType rhsType = rhs.typeCheck();
// System.out.println(lhsType);
// System.out.println(rhsType);
if (lhsType.equals(VarType.String) && rhsType.equals(VarType.String) && operator.equals("+")) {
return VarType.String;
}
if (! lhsType.equals(VarType.Float) && !lhs.equals(VarType.Int)){
System.out.println("❌ Fatal error: Cant apply arithmetic operator " + operator + " on expression of type: " + lhsType + " and " + rhsType);
System.exit(0);
}
if (! rhsType.equals(VarType.Float) && !rhsType.equals(VarType.Int)){
System.out.println("❌ Fatal error: Cant apply arithmetic operator " + operator + " on expression of type: " + rhsType + " and " + rhsType);
System.exit(0);
}
if (lhsType.equals(VarType.Int) && rhsType.equals(VarType.Int))
return VarType.Int;
return VarType.Float;
}
else if (operator.equals("||") || operator.equals("&&")){
VarType lhsType = lhs.typeCheck();
VarType rhsType = rhs.typeCheck();
if (!lhsType.equals(VarType.Bool) && !lhsType.equals(VarType.Int)){
System.out.println("❌ Fatal error: Incompatible types: " + lhsType + " cannot be converted to Boolean");
System.exit(0);
}
if (!rhsType.equals(VarType.Bool) && !rhsType.equals(VarType.Int)){
System.out.println("❌ Fatal error: Incompatible types: " + rhsType + " cannot be converted to Boolean");
System.exit(0);
}
return VarType.Bool;
}
else{
VarType lhsType = lhs.typeCheck();
VarType rhsType = rhs.typeCheck();
if (!lhsType.equals(VarType.Int) && !lhsType.equals(VarType.Float)){
System.out.println("❌ Fatal error: Incompatible types: " + operator + " and " + lhsType);
System.exit(0);
}
if (!rhsType.equals(VarType.Int) && !rhsType.equals(VarType.Float)){
System.out.println("❌ Fatal error: Incompatible types: " + operator + " and " + rhsType);
System.exit(0);
}
return VarType.Bool;
}
}
}