package gve.calc.formula;

public class OperatorEquality extends InfixBinaryOp {
	/* = != */
	private String opname;

	public OperatorEquality(String nam,Part l,Part r) {
		super(l,r);
		opname = nam;
	}

	public String getName() { return opname; }
	public int getPri() { return 1600; }

	public boolean same(Object o) {
		o = Brackets.unbracket(o);
		if (o instanceof OperatorEquality) {
			OperatorEquality eq = (OperatorEquality)o;
			return opname.equals(eq.opname) && left.same(eq.left) && right.same(eq.right);
		}
		return false;
	}

	public Part evaluate(Evaluator ev) {
		boolean result = opname.equals("=");
		// Some tricks: "x+y>0 = subst(x+y,x,x>0)" must return true
		// if we evaluate() left, we get "x+y>0.0"
		// and the right side evaluates to "x+y>0" ...
		if (left.same(right)) return new Boolean(result);
		Part rightEval = right.evaluate(ev);
		if (left.same(rightEval)) return new Boolean(result);
		Part leftEval = left.evaluate(ev);
		if (leftEval.same(right)) return new Boolean(result);
		if (leftEval.same(rightEval)) return new Boolean(result);
//		boolean gelijk = left.evaluate(ev).same(right.evaluate(ev));
//		if (opname.equals("!=")) gelijk = !gelijk;
		return new Boolean(!result);
	}

	public void write(java.io.Writer w) throws java.io.IOException {
		super.write(w);
		w.write(opname+"\n");
	}

	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 OperatorEquality(r.readLine(),left,right);
	}
}