visitCallExpr method

  1. @override
Object? visitCallExpr(
  1. Call expr
)
override

Implementation

@override
Object? visitCallExpr(Expr.Call expr) {
  Object? callee = evaluate(expr.callee);
  List<Object?> arguments = [];
  Map<Symbol, Object?> namedArguments = {};
  for (Expr.Expr argument in expr.arguments) {
    var arg = evaluate(argument);
    if (argument is Expr.NamedArgs) {
      Map args = arg as Map;
      namedArguments.addAll({Symbol(args.keys.first): args.values.first});
    } else {
      arguments.add(arg);
    }
  }
  if (callee is Function) {
    return Function.apply(callee, arguments, namedArguments);
  }
  if (callee is DaxCallable) {
    return callee.call(this, arguments, namedArguments);
  }
  if (callee is! LoxCallable) {
    throw RuntimeError(expr.paren, "Can only call functions and classes.");
  }
  LoxCallable function = callee;
  if (arguments.length != function.arity()) {
    throw RuntimeError(expr.paren,
        "Expected ${function.arity()} arguments but got ${arguments.length}.");
  }
  if (function is LoxClass &&
      function.superclass != null &&
      (function.superclass!.name == "StatefulWidget" ||
          function.superclass!.name == "StatelessWidget")) {
    namedArguments[Symbol('_klass_')] = function;
    return (top.getAt(0, "Dax${function.superclass!.name}") as DaxCallable)
        .call(this, arguments, namedArguments);
  }
  return function.call(this, arguments, namedArguments);
}