public object Visit(Expr.Call expr) { object callee = Evaluate(expr.callee); List <object> arguments = new List <object>(); foreach (Expr arg in expr.arguments) { arguments.Add(Evaluate(arg)); } if (!(callee is ILoxCallable)) { throw new RuntimeError(expr.paren, "Can only call functions and classes."); } ILoxCallable function = (ILoxCallable)callee; if (arguments.Count != function.Arity) { throw new RuntimeError(expr.paren, "Expected " + function.Arity + " arguments but got " + arguments.Count + "."); } return(function.Call(this, arguments)); }
public object visitExprCallExpr(ExprCall expr) { object callee = evaluate(expr.callee); List <object> arguments = new List <object>(); foreach (Expr argument in expr.arguments) { arguments.Add(evaluate(argument)); } if (!callee.GetType().GetInterfaces().Contains(typeof(ILoxCallable))) { throw new LoxRuntimeException(expr.paren, "Can only call function and classes."); } ILoxCallable function = (ILoxCallable)callee; if (function.arity() != arguments.Count) { throw new LoxRuntimeException(expr.paren, "Expected " + function.arity() + " arguments but got " + arguments.Count + '.'); } return(function.call(this, arguments)); }
public object VisitCallExpr(Expr <object> .Call expr) { Object callee = Evaluate(expr.callee); var args = new List <object>(); foreach (Expr <object> arg in expr.arguments) { args.Add(Evaluate(arg)); } if (!(typeof(ILoxCallable).IsInstanceOfType(callee))) { throw new RuntimeError(expr.paren, "Can only call functions and classes."); } ILoxCallable function = (ILoxCallable)callee; if (args.Count != function.Arity()) { throw new RuntimeError(expr.paren, $"Expected {function.Arity()} arguments but got {args.Count}."); } return(function.Call(this, args)); }
public object VisitCallExpr(Call expr) { object callee = Evaluate(expr.Callee); List <object> arguments = new List <object>(); foreach (Expr argument in expr.Arguments) { arguments.Add(Evaluate(argument)); } if (!(callee is ILoxCallable)) { throw new RuntimeError(expr.Paren, "Can only call functions and classes."); } ILoxCallable function = (ILoxCallable)callee; if (arguments.Count != function.Arity()) { throw new RuntimeError(expr.Paren, $"Expected {function.Arity()} arguments but got {arguments.Count}."); } return(function.Call(this, arguments)); }
public object Visit(Expr.Call expr) { object callee = Evaluate(expr.Callee); List <object> arguments = new List <object>(); foreach (Expr arg in expr.Arguments) { arguments.Add(Evaluate(arg)); } // Make sure we the callee is actually callable if (!(callee is ILoxCallable)) { throw new RuntimeErrorException(expr.Paren, "Can only call functions and classes."); } ILoxCallable function = (ILoxCallable)callee; // Make sure we are passing the correct number of arguments if (arguments.Count() != function.Arity) { throw new RuntimeErrorException(expr.Paren, $"Expected {function.Arity} argumuments, but got {arguments.Count()}."); } return(function.Call(this, arguments)); }
public object VisitCallExpr(Expr.Call expr) { object callee = Evaluate(expr.Callee); List <object> arguments = expr.Arguments.Select(Evaluate).ToList(); if (!(callee is ILoxCallable)) { throw new RuntimeError(expr.Paren, "Can only call functions and classes."); } ILoxCallable function = (ILoxCallable)callee; if (arguments.Count() != function.Arity()) { throw new RuntimeError(expr.Paren, $"Expect {function.Arity()} arguments but got {arguments.Count()} instead."); } return(function.Call(this, arguments)); }
public object Visit(Expr.Call _call) { object callee = Evaluate(_call.callee); object[] arguments = new object[_call.arguments.Count]; for (int i = 0; i < _call.arguments.Count; i++) { arguments[i] = Evaluate(_call.arguments[i]); } ILoxCallable function = callee as ILoxCallable; if (callee == null) { throw new RuntimeError(_call.paren, "Can only call functions on classes"); } if (arguments.Length != function.arity) { throw new RuntimeError(_call.paren, "Expected " + function.arity + " arguments but got " + arguments.Length + "."); } return(function.Call(this, arguments)); }