package gve.calc.formula;

public class OperatorPlus extends InfixBinaryOp {
	public String getName() { return "+"; }
	public int getPri() { return 1800; }

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

	public boolean same(Object o) {
		o = Brackets.unbracket(o);
		if (o instanceof OperatorPlus) {
			OperatorPlus plus = (OperatorPlus)o;
			return left.same(plus.left) && right.same(plus.right);
		}
		return false;
	}

	public Part evaluate(Evaluator ev) {
		Part l = left.evaluate(ev);
		Part 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(inter.low+left,inter.high+left,inter.openLow,inter.openHigh);
		} 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.low,left.high+right.high,
				left.openLow||right.openLow,left.openHigh||right.openHigh);
		}
		System.out.println("cannot evaluate "+l+" + "+r);
		return new OperatorPlus((Part)l.clone(),(Part)r.clone());
	}

	public static Part read(java.io.BufferedReader r) throws java.io.IOException,
			ClassNotFoundException,NoSuchMethodException,
			java.lang.reflect.InvocationTargetException,IllegalAccessException{
		Part left = Part.read(r);
		Part right = Part.read(r);
		return new OperatorPlus(left,right);
	}
}