/** Our internal representation of a BinOp * in the S0 language. * See http://www.radford.edu/itec380/2017fall-ibarland/Homeworks/Project/ * * @author Ian Barland * @version 2017.Nov.28 */ public class BinOp extends Expr { Expr left, right; String op; public static final java.util.List OPS = java.util.Arrays.asList( "ad", "su", "mu" ); BinOp( Expr _left, Expr _right, String _op ) { this.left = _left; this.right = _right; this.op = _op; } public String toString( /* BinOp this */) { return "?" + " " + this.op +" " + this.left.toString() + " " + this.right.toString() ; } public Value eval( /* BinOp this */) { String theOp = this.op; double leftVal = ((Num)(this.left .eval())).doubleValue(); double rightVal = ((Num)(this.right.eval())).doubleValue(); if (this.op.equals("ad")) { return new Num(leftVal + rightVal); } else if (this.op.equals("su")) { return new Num(leftVal - rightVal); } else if (this.op.equals("mu")) { return new Num(leftVal * rightVal); } 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)0x314d2ef361bcd159L; // 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; }