Exemplo n.º 1
0
    public object VisitCallExpr(Expr.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 LoxCallable))
        {
            throw new RuntimeException(
                      expr.Paren, "Can only call functions and classes.");
        }

        LoxCallable function = (LoxCallable)callee;
        int         arity    = function.Arity();

        if (arguments.Count != arity)
        {
            throw new RuntimeException(expr.Paren,
                                       $"Expected {arity} arguments but got {arguments.Count}.");
        }

        return(function.Call(this, arguments));
    }
Exemplo n.º 2
0
        public object visitCallExpr(Expr.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 LoxCallable))
            {
                throw new RuntimeError(expr.paren, "Can only call functions and classes.");
            }

            LoxCallable function = (LoxCallable)callee;

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


            return(function.call(this, arguments));
        }
Exemplo n.º 3
0
        public object VisitCallExpr(Expr.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 LoxCallable))
            {
                throw new RuntimeError(expr.paren, "Can only call functions and classes.");
            }

            LoxCallable function = (LoxCallable)callee;

            //check number of arguments before calling
            if (arguments.Count != function.Arity())
            {
                throw new RuntimeError(expr.paren, $"Expected {function.Arity()} but got {arguments.Count}");
            }

            return(function.Call(this, arguments));
        }
Exemplo n.º 4
0
        public object visitCallExpr(Expr.Call expr)
        {
            object        callee = evaluate(expr.Callee);
            List <object> args   = expr.Arguments.ConvertAll(
                new Converter <Expr, object>(x => evaluate(x))
                );

            if (!(callee is LoxCallable))
            {
                throw new RuntimeError(expr.Paren,
                                       "Can only call functions and classes");
            }
            LoxCallable function = (LoxCallable)callee;

            if (args.Count != function.arity())
            {
                throw new RuntimeError(expr.Paren,
                                       $"Expected {function.arity()} but got {args.Count}");
            }
            return(function.call(this, args));
        }
Exemplo n.º 5
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));
        }
Exemplo n.º 6
0
        private object EvaluateCallExpression(CallExpression expr)
        {
            object        callee    = Evaluate(expr.Callee);
            List <object> arguments = new List <object>();

            foreach (SyntaxNode argument in expr.Arguments)
            {
                arguments.Add(Evaluate(argument));
            }
            LoxCallable function = callee as LoxCallable;

            if (function is null)
            {
                throw new RuntimeError(expr.Paren, "Can olny call functions and classes.");
            }

            // check arity
            if (arguments.Count != function.Arity)
            {
                throw new RuntimeError(expr.Paren, $"Expected {function.Arity} arguments but got {arguments.Count}.");
            }
            return(function.Call(this, arguments));
        }
Exemplo n.º 7
0
        public object visit_Call_Expr(GExpr.Call expr)
        {
            Object callee = evaluate(expr.callee);

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

            foreach (GExpr.Expr argument in expr.Arguments)
            {
                arguments.Add(evaluate(argument));
            }

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

            LoxCallable function = (LoxCallable)callee;

            if (arguments.Count != function.arity())
            {
                throw new RuntimeError(expr.paren, $"Expected {function.arity()} arguments but got {arguments.Count}.");
            }
            return(function.call(this, arguments));
        }
Exemplo n.º 8
0
        public object Visit_CallExpr(CallExpr expr)
        {
            Object callee = Evaluate(expr.callee);

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

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

            if (!(callee is LoxCallable))
            {
                throw new RuntimeException(expr.paren.line, expr.paren.column, "Can only call functions and classes.");
            }
            LoxCallable function = (LoxCallable)callee;

            if (arguments.Count != function.Arity)
            {
                throw new RuntimeException(expr.paren.line, expr.paren.column, "Expected "
                                           + function.Arity + " arguments but got " + arguments.Count + ".");
            }
            return(function.Call(this, arguments));
        }