Exemplo n.º 1
0
        public object VisitCallExpression(Expression.Call expression)
        {
            object callee = Evaluate(expression.callee);

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

            foreach (Expression argument in expression.arguments)
            {
                arguments.Add(Evaluate(argument));
            }

            if (!(callee is Callable))
            {
                throw new RuntimeError(expression.parenthesis, "Can only call functions and classes.");
            }

            Callable function = (Callable)callee;

            if (arguments.Count != function.Arity())
            {
                throw new RuntimeError(expression.parenthesis, $"Expected {function.Arity()} arguments but got {arguments.Count}.");
            }

            return(function.Call(this, arguments));
        }
Exemplo n.º 2
0
 protected override Node?Visit(Expression.Call call)
 {
     if (dependencyMap.CallDesugar.TryGetValue(call, out var alt))
     {
         return(Transform(alt));
     }
     return(base.Visit(call));
 }
Exemplo n.º 3
0
        public object VisitCallExpression(Expression.Call expression)
        {
            Resolve(expression.callee);

            foreach (Expression argument in expression.arguments)
            {
                Resolve(argument);
            }
            return(null);
        }
Exemplo n.º 4
0
        public object VisitCallExpr(Expression.Call expr)
        {
            Resolve(expr.callee);

            foreach (Expression arg in expr.arguments)
            {
                Resolve(arg);
            }

            return(new Stmt.DefaultStatement());
        }
Exemplo n.º 5
0
        protected override object?Visit(Expression.Call call)
        {
            base.Visit(call);
            var calledType = System.TypeOf(call.Procedure);

            if (calledType is Type.Proc procType)
            {
                var dependency = procType.GetDependency();
                if (dependency != null)
                {
                    /*
                     * Desugar
                     *  f(dependee args..., independent args..., dependent args...)
                     * Into
                     *  f(dependee args...).f(independent args..., dependent args...)
                     */

                    Expression callProcedure;
                    if (procType.IsIntrinsic)
                    {
                        var intrinsic = (Intrinsic)((Value.User)System.Evaluate(call.Procedure)).Payload;
                        callProcedure = new Expression.Identifier(null, intrinsic.NonDependentDesugar());
                    }
                    else
                    {
                        callProcedure = call.Procedure;
                    }

                    var result = new Expression.Call(
                        null,
                        new Expression.DotPath(
                            null,
                            new Expression.Call(
                                null,
                                callProcedure,
                                dependency.DependeeIndices
                                .Select(i => call.Arguments[i])
                                .ToArray()),
                            "f"),
                        dependency.DependentIndices
                        .Concat(dependency.IndependentIndices)
                        .Select(i => call.Arguments[i])
                        .ToArray());

                    DependencyMap.CallDesugar.Add(call, result);
                }
            }
            return(null);
        }
Exemplo n.º 6
0
        protected override object?Visit(Expression.Call call)
        {
            base.Visit(call);
            // Check if the called thing is even a procedure
            var calledType = System.TypeOf(call.Procedure);

            if (!(calledType is Type.Proc procType))
            {
                // TODO
                throw new NotImplementedException("Can't call non-procedure!");
            }
            // Check if arguments match parameters
            if (procType.Parameters.Count == call.Arguments.Count)
            {
                foreach (var(param, arg) in procType.Parameters.Zip(call.Arguments))
                {
                    var paramType = param.Type;
                    var argType   = System.TypeOf(arg);
                    if (!paramType.Equals(argType))
                    {
                        System.Report(new TypeMismatchError(paramType, argType)
                        {
                            Context = "procedure call",
                            Defined = param.Symbol.Definition?.ParseTreeNode,
                            Wrong   = arg.ParseTreeNode,
                        });
                    }
                }
            }
            else
            {
                // Argument count mismatch
                System.Report(new ArgCountMismatchError(procType.Parameters.Count, call.Arguments.Count)
                {
                    // TODO: Add definition
                    Wrong = call.ParseTreeNode,
                });
            }
            return(null);
        }
Exemplo n.º 7
0
        public object VisitCallExpr(Expression.Call expr)
        {
            object        callee    = Evaluate(expr.callee);
            List <object> arguments = new();

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

            if (!(callee is IJukaCallable))
            {
                throw new ArgumentException("not a class or function");
            }

            IJukaCallable function = (IJukaCallable)callee;

            if (arguments.Count != function.Arity())
            {
                throw new ArgumentException("Wrong number of arguments");
            }

            return(function.Call(this, arguments));
        }
Exemplo n.º 8
0
 public string VisitCallExpression(Expression.Call expression)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 9
0
        private Expression ParsePostfixExpression(ExprState state)
        {
            var result = ParseAtomicExpression(state);

            while (true)
            {
                var peek = Peek();
                if (PostfixOperators.Contains(peek.Type))
                {
                    var op = Next();
                    result = new Expression.Postfix(result, op);
                }
                else if (peek.Type == TokenType.OpenParen)
                {
                    var openParen = Expect(null, null, TokenType.OpenParen);
                    // Call expression
                    var args = new List <WithComma <Expression> >();
                    while (Peek().Type != TokenType.CloseParen)
                    {
                        var arg      = ParseExpression(ExprState.None);
                        var hasComma = Match(TokenType.Comma, out var comma);
                        args.Add(new WithComma <Expression>(arg, comma));
                        if (!hasComma)
                        {
                            break;
                        }
                    }
                    var closeParen = Expect("call expression", openParen, TokenType.CloseParen);
                    result = new Expression.Call(result, openParen, args, closeParen);
                }
                else if (peek.Type == TokenType.OpenBracket)
                {
                    var openBracket = Expect(null, null, TokenType.OpenBracket);
                    // Subscript expression
                    var index        = ParseExpression(ExprState.None);
                    var closeBracket = Expect("subscript expression", openBracket, TokenType.CloseBracket);
                    result = new Expression.Subscript(result, openBracket, index, closeBracket);
                }
                else if (!state.HasFlag(ExprState.TypeOnly) &&
                         !state.HasFlag(ExprState.NoBraced) &&
                         peek.Type == TokenType.OpenBrace)
                {
                    // Struct instantiation
                    var openBrace = Expect(null, null, TokenType.OpenBrace);
                    var fields    = new List <Expression.StructValue.Field>();
                    while (Peek().Type != TokenType.CloseBrace)
                    {
                        fields.Add(ParseStructValueField());
                    }
                    var closeBrace = Expect("struct instantiation", openBrace, TokenType.CloseBrace);
                    result = new Expression.StructValue(result, openBrace, fields, closeBrace);
                }
                else if (peek.Type == TokenType.Dot)
                {
                    // Dot path
                    var dot   = Expect(null, null, TokenType.Dot);
                    var ident = Expect("dot path expression", null, TokenType.Identifier);
                    result = new Expression.DotPath(result, dot, ident);
                }
                else
                {
                    break;
                }
            }
            return(result);
        }