evalBinaryExpression method

  1. @protected
dynamic evalBinaryExpression(
  1. BinaryExpression expression,
  2. Map<String, dynamic> context, {
  3. void onValueAssigned(
    1. String key,
    2. dynamic value
    )?,
})
inherited

Implementation

@protected
dynamic evalBinaryExpression(
  BinaryExpression expression,
  Map<String, dynamic> context, {
  void Function(String key, dynamic value)? onValueAssigned,
}) {
  dynamic result;
  final left = eval(expression.left, context);
  dynamic right() => eval(expression.right, context);
  switch (expression.operator) {
    case '=':
      final leftVar = expression.left.toString();
      result = right();
      context[leftVar] = result;
      if (onValueAssigned != null) {
        onValueAssigned(leftVar, result);
      }
      break;

    case '||':
      result = left || right();
      break;

    case '&&':
      result = left && right();
      break;

    case '|':
      result = left | right();
      break;

    case '^':
      result = left ^ right();
      break;

    case '&':
      result = left & right();
      break;

    case '==':
      result = left == right();
      break;

    case '!=':
      result = left != right();
      break;

    case '<=':
      result = left <= right();
      break;

    case '>=':
      result = left >= right();
      break;

    case '<':
      result = left < right();
      break;

    case '>':
      result = left > right();
      break;

    case '<<':
      result = left << right();
      break;

    case '>>':
      result = left >> right();
      break;

    case '+':
      final r = right();
      result = left + r;
      break;

    case '-':
      result = left - right();
      break;

    case '*':
      result = left * right();
      break;

    case '/':
      result = left / right();
      break;

    case '%':
      result = left % right();
      break;

    case '??':
      result = left ?? right();
      break;

    default:
      throw ArgumentError(
        'Unknown operator ${expression.operator} in expression',
      );
  }

  return result;
}