Exemple #1
0
        object Expr.IVisitor <object> .Visit(Expr.Call _call)
        {
            object callee = Evaluate(_call.callee);

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

            foreach (Expr argument in _call.arguments)
            {
                arguments.Add(Evaluate(argument));
            }

            if (!(callee is ICallable))
            {
                throw new RuntimeError(_call.paren, "Can only call functions and classes.");
            }

            ICallable function = (ICallable)callee;

            if (arguments.Count != function.Arity)
            {
                throw new RuntimeError(_call.paren, "Expected " + function.Arity + " arguments, but got " + arguments.Count + ".");
            }



            return(function.Call(this, arguments));
        }
Exemple #2
0
        public Void VisitCallExpr(Expr.Call expr)
        {
            Resolve(expr.Callee);

            foreach (var argument in expr.Arguments)
            {
                Resolve(argument);
            }

            return(null);
        }
Exemple #3
0
        public object Visit(Expr.Call _call)
        {
            Resolve(_call.callee);

            foreach (Expr argument in _call.arguments)
            {
                Resolve(argument);
            }

            return(null);
        }
Exemple #4
0
        public object visitCallExpr(Expr.Call call)
        {
            resolve(call.callee);

            foreach (Object arg in call.expressionArguments)
            {
                if (arg is Expr)
                {
                    resolve((Expr)arg);
                }
                else if (arg is Statement.function)
                {
                    resolve((Statement.function)arg);
                }
            }
            return(null);
        }
Exemple #5
0
 public string visitCallExpr(Expr.Call call)
 {
     Expr[] expressions = new Expr[call.expressionArguments.Count];
     for (int i = 0; i < call.expressionArguments.Count; i++)
     {
         if (call.expressionArguments[i] is Expr)
         {
             expressions[i] = (Expr)call.expressionArguments[i];
         }
         else if (call.expressionArguments[i] is Statement.function)
         {
             Statement.function lambda = (Statement.function)call.expressionArguments[i];
             expressions[i] = new Expr.Lambda(lambda.name, lambda._params, lambda.body);
         }
     }
     return(parenthesize(call.callee.ToString(), expressions));
 }
        public object VisitCallExpr(Expr.Call expr)
        {
            object callee = Evaluate(expr.callee);

            var arguments = expr.arguments.Select(Evaluate).ToList();

            if (callee is ICallable func)
            {
                if (arguments.Count != func.Arity())
                {
                    throw new RuntimeError(expr.paren,
                                           $"Expected {func.Arity()} arguments but got {arguments.Count}.");
                }

                return(func.Call(this, arguments));
            }
            throw new RuntimeError(expr.paren, "Can only call functions and classes.");
        }
Exemple #7
0
        public object VisitCallExpr(Expr.Call expr)
        {
            var callee    = Evaluate(expr.Callee);
            var arguments = new List <object>();

            foreach (var argument in expr.Arguments)
            {
                arguments.Add(Evaluate(argument));
            }

            if (callee is ILoxCallable function)
            {
                if (arguments.Count != function.Arity())
                {
                    throw new RuntimeError(expr.Paren, $"Expected {function.Arity()} arguments but got {arguments.Count}.");
                }
                return(function.Call(this, arguments));
            }

            throw new RuntimeError(expr.Paren, "Can only call functions and classes");
        }
Exemple #8
0
        public object visitCallExpr(Expr.Call call)
        {
            Object callee = evaluate(call.callee);

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

            foreach (Object argument in call.expressionArguments)
            {
                if (argument is Expr)
                {
                    arguments.Add(evaluate((Expr)argument));
                }
                else if (argument is Statement.function)
                {
                    arguments.Add((Statement.function)argument);
                }
            }

            if (!(callee is LoxCallable) && !(callee is Statement.function))
            {
                throw new Exceptions.RuntimeError(call.paren, "Can only call functions and classes.");
            }
            else if (callee is Statement.function)
            {
                Statement.function staticFunction      = (Statement.function)callee;
                LoxFunction        classStaticFunction = new LoxFunction(staticFunction, globals, false);
                return(classStaticFunction.call(this, arguments));
            }
            LoxCallable function = (LoxCallable)callee;

            if (arguments.Count != function.arity())
            {
                throw new Exceptions.RuntimeError(call.paren, "Expected " +
                                                  function.arity() + " arguments but got " +
                                                  arguments.Count + ".");
            }
            return(function.call(this, arguments));
        }
 string Expr.IVisitor <string> .Visit(Expr.Call _call)
 {
     throw new NotImplementedException();
 }
Exemple #10
0
 public object VisitCallExpr(Expr.Call expr)
 {
     Resolve(expr.callee);
     expr.arguments.ForEach(Resolve);
     return(null);
 }
Exemple #11
0
 public string VisitCallExpr(Expr.Call expr)
 {
     return($"({expr.callee.Accept(this)}()");
 }