/** Our internal representation of a BinOp * in the R0 language. * See http://www.radford.edu/itec380/2016fall-ibarland/Homeworks/Project/ * * @author Ian Barland * @version 2014.Nov.04 */ public class BinOp extends Expr { Expr left, right; String op; BinOp( Expr _left, Expr _right, String _op ) { this.left = _left; this.right = _right; this.op = _op; } public String toString( /* BinOp this */) { return "|" + " " + this.left.toString() + " " + this.right.toString() + " " + op +" " + "|"; } public Value eval( /* BinOp this */) { double leftValue = ((Num)(this.left .eval())).doubleValue(); double rightValue = ((Num)(this.right.eval())).doubleValue(); if (this.op.equals("xD")) { return new Num(leftValue * rightValue); } else if (this.op.equals(";)")) { return new Num(leftValue + rightValue); } else if (this.op.equals(";(")) { return new Num(leftValue - rightValue); } else { throw new RuntimeException("BinOp.eval(): unknown binary operator `" + this.op + "`"); } } @Override public boolean equals( /* BinOp this, */ Object that) { if (this==that) { return true; } else if (that==null) { return false; } else if (this.getClass() != that.getClass()) { return false; } else { BinOp thatt = (BinOp) that; return this.left.equals(thatt.left) && this.op.equals(thatt.op) && this.right.equals(thatt.right); } } @Override public int hashCode() { if (cachedHash == null) { int hashSoFar = (int)0x31476096bdd65159L; // fingerprint hashSoFar += this.left.hashCode(); hashSoFar *= 31; hashSoFar += this.op.hashCode(); hashSoFar *= 31; hashSoFar += this.right.hashCode(); cachedHash = hashSoFar; } return cachedHash; } private Integer cachedHash = null; }