simplify method

  1. @override
Expression simplify()
override

Possible simplifications:

  1. -a / b = - (a / b)
  2. a / -b = - (a / b)
  3. -a / -b = a / b
  4. 0 / a = 0
  5. 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?)!
}