parseConditional method

Expression parseConditional()

c ? a : b, right to left.

Implementation

Expression parseConditional() {
  final condition = parseOr();
  if (current.kind != '?') return condition;
  final question = expectToken('?', '"?"');
  final then = parseExpression();
  expectToken(':', '":" and the value when the condition is false');
  final otherwise = parseExpression();
  if (!condition.type.isBoolean) {
    fail(
      'a condition is a bool, and this is a ${condition.type}.',
      where: question,
    );
  }
  if (then.type != otherwise.type) {
    fail(
      'the two values of "?" are a ${then.type} and a ${otherwise.type}; '
      'they have to be one type.',
      where: question,
    );
  }
  return ExpressionConditional(condition, then, otherwise);
}