1abstract
2class BinaryExpr extends Expr {
3
4
5
6 protected
7 Expr left;
8
9 protected
10 Expr right;
11
12
13
14
15
16
17 protected
18 BinaryExpr(Expr left, Expr right) {
19 this.left = left;
20 this.right = right;
21 }
22
23 public Object eval() {
24 Object v1 = left.eval();
25 Object v2 = right.eval();
26 return
27 op2(v1, v2);
28 }
29
30 public
31 String toString() {
32 return
33 "(" +
34 left.toString() +
35 " " + op2ToString() + " " +
36 right.toString() +
37 ")";
38 }
39
40
41
42 protected abstract Object op2(Object v1, Object v2);
43 protected abstract String op2ToString();
44}
45