comparison method

Expression comparison(
  1. ExpressionToken op,
  2. Expression left,
  3. Expression right
)

A comparison of two numbers of one dimension, or == and != of two bools.

Implementation

Expression comparison(ExpressionToken op, Expression left, Expression right) {
  final equality = op.text == '==' || op.text == '!=';
  final bothBool = left.type.isBoolean && right.type.isBoolean;
  if (!(equality && bothBool)) {
    for (final side in <Expression>[left, right]) {
      if (!side.type.isNumeric || side.type.components != 1) {
        fail(
          '"${op.text}" compares two numbers, not a ${side.type}.',
          where: op,
        );
      }
    }
    if (left.type.dimension != right.type.dimension) {
      fail(
        'a ${left.type} and a ${right.type} cannot be compared: they '
        'measure different things.',
        where: op,
      );
    }
  }
  return ExpressionBinary(op.text, left, right, ExpressionType.boolean);
}