simplify method
Possible simplifications:
- -a / b = - (a / b)
- a / -b = - (a / b)
- -a / -b = a / b
- 0 / a = 0
- a / 1 = a
Implementation
@override
Expression simplify() {
Expression firstOp = first.simplify();
Expression secondOp = second.simplify();
Expression tempResult;
bool negative = false;
if (firstOp is UnaryMinus) {
firstOp = (firstOp).exp;
negative = !negative;
}
if (secondOp is UnaryMinus) {
secondOp = (secondOp).exp;
negative = !negative;
}
if (_isNumber(firstOp, 0)) {
return firstOp; // = 0
}
if (_isNumber(secondOp, 1)) {
tempResult = firstOp;
} else {
tempResult = Divide(firstOp, secondOp);
}
return negative ? UnaryMinus(tempResult) : tempResult;
// TODO cancel down/out? - needs equals on literals (and expressions?)!
}