예제 #1
0
 /// <summary>
 ///     Parses and executes the specified string
 /// </summary>
 public LuaArguments DoString(string Chunk)
 {
     var def = new FunctionDefinition();
     def.Arguments = new List<Argument>();
     def.Body = _parser.ParseString(Chunk);
     var function = LuaCompiler.CompileFunction(def, Expression.Constant(Context)).Compile();
     return function().Call(Return());
 }
예제 #2
0
        private Assignment ParseFunctionDecl(ParseTreeNode node)
        {
            if (node.Term.Name == "FunctionDecl")
            {
                var expr = ParseVariable(node.ChildNodes[1]);

                var argsNode = node.ChildNodes[3].ChildNodes[0];
                var chunkNode = node.ChildNodes[4];

                var block = ParseBlock(chunkNode);
                var def = new FunctionDefinition();
                def.Arguments = new List<Argument>();

                var nameNode = node.ChildNodes[2];
                if (nameNode.ChildNodes.Count > 0)
                {
                    def.Arguments.Add(new Argument { Name = "self" });
                    /*expr = new Ast.Variable()
                    {
                        Name = nameNode.ChildNodes[0].Token.ValueString,
                        Prefix = expr
                    };*/
                    var token = nameNode.ChildNodes[0].Token;
                    expr = new TableAccess
                    {
                        Expression = expr,
                        Index = new StringLiteral
                        {
                            Value = token.ValueString,
                            lineNumber = token.Location.Line,
                            columnNumber = token.Location.Column,
                            filename = curParseTree.FileName
                        }
                    };
                }
                def.Body = block;
                if (argsNode.ChildNodes.Count > 0)
                {
                    argsNode = argsNode.ChildNodes[0];
                    while (argsNode.ChildNodes.Count > 0)
                    {
                        if (argsNode.ChildNodes[0].Term.Name == "Varargs")
                        {
                            def.isVarargs = true;
                            break;
                        }
                        var ident = argsNode.ChildNodes[0].Token.ValueString;
                        def.Arguments.Add(new Argument { Name = ident });
                        if (argsNode.ChildNodes.Count == 1)
                            break;
                        argsNode = argsNode.ChildNodes[1];
                    }
                }
                var assign = new Assignment();
                assign.Variables.Add(expr);
                assign.Expressions.Add(def);
                return assign;
            }
            throw new Exception("Invalid FunctionDecl node");
        }
예제 #3
0
        private FunctionDefinition ParseFunctionDef(ParseTreeNode node)
        {
            if (node.Term.Name == "FunctionDef")
            {
                var argsNode = node.ChildNodes[1].ChildNodes[0];
                var chunkNode = node.ChildNodes[2];

                var block = ParseBlock(chunkNode);
                var def = new FunctionDefinition();
                def.Body = block;
                def.Arguments = new List<Argument>();

                if (argsNode.ChildNodes.Count == 0)
                    return def;
                if (argsNode.ChildNodes.Count > 0)
                {
                    argsNode = argsNode.ChildNodes[0];
                    while (argsNode.ChildNodes.Count > 0)
                    {
                        var ident = argsNode.ChildNodes[0].Token.ValueString;
                        def.Arguments.Add(new Argument { Name = ident });
                        if (argsNode.ChildNodes.Count == 1)
                            break;
                        argsNode = argsNode.ChildNodes[1];
                    }
                }
                return def;
            }
            throw new Exception("Invalid FunctionDef node");
        }
예제 #4
0
        private LocalAssignment ParseLocalAssign(ParseTreeNode node)
        {
            if (node.Term.Name == "LocalAssignment")
            {
                var assign = new LocalAssignment();


                var child = node.ChildNodes[1];

                if (child.ChildNodes[0].Term.Name == "LocalFunction")
                {
                    child = child.ChildNodes[0];

                    var argsNode = child.ChildNodes[2];
                    var blockNode = child.ChildNodes[3];

                    assign.Names.Add(child.ChildNodes[1].Token.ValueString);
                    var func = new FunctionDefinition();

                    if (argsNode.ChildNodes.Count > 0)
                    {
                        argsNode = argsNode.ChildNodes[0];
                        while (argsNode.ChildNodes.Count > 0)
                        {
                            var ident = argsNode.ChildNodes[0].Token.ValueString;
                            func.Arguments.Add(new Argument { Name = ident });
                            if (argsNode.ChildNodes.Count == 1)
                                break;
                            argsNode = argsNode.ChildNodes[1];
                        }
                    }

                    func.Body = ParseBlock(blockNode);

                    assign.Values.Add(func);
                    return assign;
                }

                var left = child.ChildNodes[0];
                var right = child.ChildNodes[1];

                assign.Names.Add(left.ChildNodes[0].Token.ValueString);

                left = left.ChildNodes[1];

                while (left.ChildNodes.Count > 0)
                {
                    left = left.ChildNodes[0];
                    assign.Names.Add(left.ChildNodes[0].Token.ValueString);
                    left = left.ChildNodes[1];
                }

                while (right.ChildNodes.Count > 0)
                {
                    right = right.ChildNodes[0];
                    assign.Values.Add(ParseExpression(right.ChildNodes[0]));
                    right = right.ChildNodes[1];
                }

                return assign;
            }
            throw new Exception("Invalid LocalAssignment node");
        }