Esempio n. 1
0
        // Build prototype.

        LuaPrototype BuildPrototype(LuaAST LuaAST)
        {
            // Enter block.
            block = new BlockBuilder(block, LuaAST.Block, 0);

            // Enter function.
            function = new FunctionBuilder(function, LuaAST, block);

            for (int parameter = 0; parameter < LuaAST.Parameters.Count; ++parameter)
            {
                function.DeclareLocal(LuaAST.Parameters[parameter]);
            }

            BuildBlock(LuaAST.Block);

            for (int parameter = LuaAST.Parameters.Count - 1; parameter >= 0; --parameter)
            {
                function.RetireLocal(LuaAST.Parameters[parameter]);
            }

            // End function.
            LuaPrototype prototype = function.Build();

            function = function.Parent;

            // End of block.
            block = block.Parent;

            return(prototype);
        }
Esempio n. 2
0
            public LuaPrototype Build()
            {
                LuaPrototype prototype = new LuaPrototype();

                prototype.UpValCount     = Function.UpVals.Count;
                prototype.ParameterCount = Function.Parameters.Count;
                prototype.IsVararg       = Function.IsVararg;

                prototype.Constants  = new LuaValue[constants.Count];
                prototype.Prototypes = prototypes.ToArray();

                prototype.StackSize    = watermark;
                prototype.Instructions = instructions.ToArray();

                prototype.DebugName                   = Function.Name;
                prototype.DebugSourceSpan             = ConvertSourceSpan(Function.Block.SourceSpan);
                prototype.DebugInstructionSourceSpans = instructionSourceSpans.ToArray();
                prototype.DebugUpValNames             = new string[Function.UpVals.Count];
                prototype.DebugLocals                 = debugLocals.ToArray();

                for (int constant = 0; constant < constants.Count; ++constant)
                {
                    prototype.Constants[constant] = ObjectToValue(constants[constant]);
                }

                for (int upval = 0; upval < Function.UpVals.Count; ++upval)
                {
                    prototype.DebugUpValNames[upval] = Function.UpVals[upval].Name;
                }

                return(prototype);
            }
Esempio n. 3
0
            public int Prototype(LuaPrototype prototype)
            {
                int p = prototypes.IndexOf(prototype);

                if (p == -1)
                {
                    p = prototypes.Count;
                    prototypes.Add(prototype);
                }
                return(p);
            }
Esempio n. 4
0
        // Compiling.

        public static LuaPrototype Compile(TextWriter errorWriter, TextReader sourceReader, string sourceName)
        {
            // Parse the function.
            LuaAST LuaAST = LuaParser.Parse(errorWriter, sourceReader, sourceName);

            if (LuaAST == null)
            {
                return(null);
            }


            // Compile.
            BytecodeCompiler compiler  = new BytecodeCompiler(sourceName);
            LuaPrototype     prototype = compiler.BuildPrototype(LuaAST);

            return(prototype);
        }
Esempio n. 5
0
        public void Visit(FunctionClosure e)
        {
            // Compile function and reference it.
            LuaPrototype prototype = BuildPrototype(e.Function);

            function.InstructionABx(e.SourceSpan, Opcode.Closure, target, function.Prototype(prototype));

            // Initialize upvals.
            for (int upval = 0; upval < e.Function.UpVals.Count; ++upval)
            {
                Variable variable = e.Function.UpVals[upval];
                if (function.Function.UpVals.Contains(variable))
                {
                    function.InstructionABC(e.SourceSpan, Opcode.GetUpVal, upval, function.UpVal(variable), 0);
                }
                else
                {
                    function.InstructionABC(e.SourceSpan, Opcode.Move, upval, function.Local(variable), 0);
                }
            }
        }
Esempio n. 6
0
    public static int Main(string[] arguments)
    {
        if (arguments.Length < 1)
        {
            Console.Out.WriteLine("Usage: Lua <chunk>.luac");
            return(1);
        }


        try
        {
            // Get function.
            LuaPrototype prototype;
            string       sourceName = arguments[0];
            if (String.Equals(Path.GetExtension(sourceName), ".luac", StringComparison.InvariantCultureIgnoreCase))
            {
                // Load script.
                using (BinaryReader r = new BinaryReader(File.OpenRead(sourceName)))
                {
                    prototype = LuaPrototype.Load(r);
                }
            }
            else
            {
                using (TextReader r = File.OpenText(sourceName))
                {
                    Lua.Compiler.Parser.TestParser.Parse(Console.Error, r, sourceName);
                }


                // Compile script.
                using (TextReader r = File.OpenText(sourceName))
                {
                    prototype = LuaPrototype.Compile(Console.Error, r, sourceName);
                }
            }

            if (prototype == null)
            {
                return(1);
            }

            prototype.Disassemble(Console.Out);


            // Build arg table.
            LuaTable arg = new LuaTable();
            for (int argument = 0; argument < arguments.Length; ++argument)
            {
                arg[argument] = arguments[argument];
            }


            // Modify environment.
            LuaThread.CurrentThread.Environment["arg"] = arg;


            // Invoke script.
            LuaFunction function = new LuaFunction(prototype);
            Action      action   = function.MakeDelegate <Action>();
            action();
        }
        catch (Exception e)
        {
            Console.Error.WriteLine(e.ToString());
            return(1);
        }

        return(0);
    }