Exemple #1
0
        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 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));
        }
Exemple #3
0
        public object VisitCallExpr(Call expr)
        {
            object callee = Evaluate(expr.callee);

            List <object> arguments = new List <object>();

            foreach (Expr a in expr.arguments)
            {
                arguments.Add(Evaluate(a));
            }

            if (!(callee.GetType() != typeof(ILoxCallable)))
            {
                throw new RuntimeError(expr.paren, "Can only call functions and clases.");
            }

            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));
        }
Exemple #4
0
        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));
        }