return value;
}
private Expression simplify() {
Expression evaluated = operand.evaluate();
if (evaluated instanceof Addition) { // -(a + b) = -a - b : needed by Sum.evaluation
Addition sum = (Addition) evaluated;
return new Subtraction(new Negation(sum.lhs()), sum.rhs()).evaluate();
} else if (evaluated instanceof Subtraction) {
Subtraction diff = (Subtraction) evaluated;
return new Subtraction(diff.rhs(), diff.lhs()).evaluate();
} else if (evaluated instanceof Sum) {
return ((Sum) evaluated).negate().evaluate();
} else if (evaluated instanceof Division) {
Expression n = ((Division) evaluated).lhs();
Expression d = ((Division) evaluated).rhs();
return new Division(new Negation(n), d).evaluate();
}
return new Negation(evaluated);