package calc;

/** Binary formulas (like a+b).
 * @author Gary T. Leavens
 */
public abstract class BinaryFormula implements FormulaType {
    /** The left register */
    private /*@ spec_public @*/ int left;
    /** The right register */
    private /*@ spec_public @*/ int right;
    
    /**
     * Initialize this Binary Formula object to contain the
     * the given registers.
     * @param left the left register's number.
     * @param right the right register's number
     */
    //@ requires 0 <= left && left < Registers.NUM_REGS;
    //@ requires 0 <= right && right < Registers.NUM_REGS;
    //@ ensures this.left == left && this.right == right;
    public BinaryFormula(int left, int right) {
        this.left = left;
        this.right=right;
    }

    /**
     * Return the left register's value.
     */
    protected double leftValue() {
        return Registers.get(left);
    }

    /**
     * Return the right register's value.
     */
    protected double rightValue() {
        return Registers.get(right);
    }
    
    /**
     * Does this formula have a positive value?
     */
    public boolean isPositive() {
        return this.evaluate() > 0;    
    }
    
    public boolean equals(Object o) {
        if (o == null || !(o instanceof BinaryFormula)) {
            return false;
        }
        BinaryFormula bf = (BinaryFormula) o;
        return bf.getClass() == this.getClass()
            && bf.left == this.left
            && bf.right == this.right;
    }
    
    public int hashCode() {
        return left + right;
    }

    public String toString() {
        return "r" + left + " " + this.opString()
             + " r" + right;
    }
    
    /** A string that represents the operation.
     * This is used in decoding.
     * @see #toString() */
    //@ ensures \result != null;
    protected abstract String opString();
}
