-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinExpr.java
More file actions
43 lines (38 loc) · 1.15 KB
/
Copy pathBinExpr.java
File metadata and controls
43 lines (38 loc) · 1.15 KB
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
class BinExpr extends Expr {
Expr lhs;
String operator;
Expr rhs;
public BinExpr(Expr e1, String op, Expr e2) {
lhs = e1;
operator = op;
rhs = e2;
}
public String toString(int t) {
return getTabs(t) + "(" + lhs.toString(0) + " " + operator + " " + rhs.toString(0) + ")";
}
public String typeCheck() throws UTDLangException {
String lhsType = lhs.typeCheck();
String rhsType = rhs.typeCheck();
if (!lhsType.equals(rhsType)) {
throw new UTDLangException("Error: tried to operate on different types" + " : " + toString(0));
}
if (lhsType.equals("number")) {
if (operator.equals("?")) {
return "flag";
}
return "number";
}
else if (lhsType.equals("flag")) {
if (operator.equals("+") || operator.equals("*")) {
return "flag";
}
throw new UTDLangException("Error: tried to use " + operator + " on a flag " + " : " + toString(0));
}
else { //String
if (operator.equals("+")) {
return "string";
}
throw new UTDLangException("Error: tried to use " + operator + " on a String " + " : " + toString(0));
}
}
}