/** Our internal representation of a BinExpr * in the O0 language. * See http://www.radford.edu/itec380/2009fall-ibarland/Hw06/hw06.html * * @author Ian Barland * @version 2008.Nov.30 */ public class BinExpr extends Expr { Expr left, right; String op; BinExpr( Expr _left, String _op, Expr _right ) { this.left = _left; this.op = _op; this.right = _right; } public String toString() { return "(" + this.left.toString() + " " + this.op + " " + this.right.toString() + ")" ; } public Value eval() { double leftValue = ((Num)(this.left .eval())).doubleValue(); double rightValue = ((Num)(this.right.eval())).doubleValue(); if (this.op.equals("mul")) { return new Num(leftValue * rightValue); } else if (this.op.equals("add")) { return new Num(leftValue + rightValue); } else if (this.op.equals("sub")) { return new Num(leftValue - rightValue); } else { throw new RuntimeException("BinExpr.eval(): unknown operator `" + this.op + "`"); } } }