Beispiel #1
0
        AnonymousFunctionExpr ParseExprFunctionArgsAndBody(Scope scope)
        {
            AnonymousFunctionExpr func = new AnonymousFunctionExpr();

            func.Scope = new Scope(scope);

            if (reader.ConsumeSymbol('(') == false)
            {
                error("'(' expected");
            }

            // arg list
            List <Variable> arglist  = new List <Variable>();
            bool            isVarArg = false;

            while (reader.ConsumeSymbol(')') == false)
            {
                if (reader.Is(TokenType.Ident))
                {
                    Variable arg = new Variable();
                    arg.Name = reader.Get().Data;
                    func.Scope.AddLocal(arg);
                    arglist.Add(arg);
                    if (!reader.ConsumeSymbol(','))
                    {
                        if (reader.ConsumeSymbol(')'))
                        {
                            break;
                        }
                        else
                        {
                            error("')' expected");
                        }
                        break;
                    }
                }
                else if (reader.ConsumeSymbol("..."))
                {
                    isVarArg = true;
                    if (!reader.ConsumeSymbol(')'))
                    {
                        error("'...' must be the last argument of a function");
                    }
                    break;
                }
                else
                {
                    error("Argument name or '...' expected");
                    break;
                }
            }

            // body
            List <Statement> body = ParseStatementList(func.Scope);

            // end
            if (!reader.ConsumeKeyword("end"))
            {
                error("'end' expected after function body");
            }

            //nodeFunc.AstType = AstType.Function;
            func.Arguments = arglist;
            func.Body      = body;
            func.IsVararg  = isVarArg;

            return(func);
        }