package gve.calc.formula;

public class OperatorMinus extends InfixBinaryOp {
	public String getName() { return "-"; }
	public int getPri() { return 1800; }
	public int getLeftPri() { return 1790; }
	public int getRightPri() { return 1810; }

	public OperatorMinus(Part l,Part r) {
		super(l,r);
	}

	public Part evaluate(Evaluator ev) {
		Object l = left.evaluate(ev);
		Object r = right.evaluate(ev);
		if (l instanceof Real && r instanceof Real) { 
			return new Real(((Real)l).doubleValue
				- ((Real)r).doubleValue);
		} else if (l instanceof Real && r instanceof Interval) {
			Interval inter = (Interval)r;
			double left = ((Real)l).doubleValue;
			return new Interval(left-inter.high,left-inter.low,inter.openHigh,inter.openLow);
		} else if (l instanceof Interval && r instanceof Real) {
			Interval inter = (Interval)l;
			double right = ((Real)r).doubleValue;
			return new Interval(inter.low-right,inter.high-right,inter.openLow,inter.openHigh);
		} else if (l instanceof Interval && r instanceof Interval) {
			Interval left = (Interval)l;
			Interval right = (Interval)r;
			return new Interval(left.low-right.high,left.high-right.low,
				left.openLow||right.openHigh,left.openHigh||right.openLow);
		}
		System.out.println("cannot evaluate "+l+" - "+r);
		return null;
	}
}