示例#1
0
文件: Program.cs 项目: acaly/FastLua
        public static void Main()
        {
            var codeReader = new StringReader(_code);
            var rawTokens  = new LuaRawTokenizer();
            var luaTokens  = new LuaTokenizer();

            rawTokens.Reset(codeReader);
            luaTokens.Reset(rawTokens);

            var parser  = new LuaParser();
            var builder = new SyntaxTreeBuilder();

            builder.Start();
            luaTokens.EnsureMoveNext();
            parser.Reset(luaTokens, builder);
            parser.Parse();

            var ast = builder.Finish();

            var env = new Table();

            env.SetRaw(TypedValue.MakeString("print"), TypedValue.MakeNClosure(Print));

            var codeGen = new CodeGenerator();
            var closure = codeGen.Compile(ast, env);

            var thread = new Thread();
            var stack  = thread.AllocateRootCSharpStack(1);

            var arg = TypedValue.MakeString("C#");

            stack.Write(0, in arg);
            LuaInterpreter.Execute(stack, closure, 0, 1);
            stack.Read(0, out var ret);
        }
示例#2
0
        public static void DoString(string str, Table env, Span <TypedValue> args, Span <TypedValue> results)
        {
            var closure = Compile(str, env);

            var thread    = new Thread();
            var stackSize = Math.Max(args.Length, results.Length);
            var stack     = thread.AllocateRootCSharpStack(stackSize);

            if (args.Length > 0)
            {
                throw new NotImplementedException();
            }
            stack.Write(0, args);

            var retCount = LuaInterpreter.Execute(thread, closure, stack, 0, 0);

            stack.Read(0, results);
            for (int i = retCount; i < results.Length; ++i)
            {
                results[i] = TypedValue.Nil;
            }
        }
示例#3
0
        public void TestAdd()
        {
            var closure = TestHelper.Compile("return function(a, b) return a + b end", null);

            var thread = new Thread();
            var stack  = thread.AllocateRootCSharpStack(3);

            var retCount = LuaInterpreter.Execute(stack, closure, 0, 0);

            Assert.AreEqual(1, retCount);
            stack.Read(0, out var retVal);

            Assert.AreEqual(LuaValueType.LClosure, retVal.ValueType);
            var retClosure = retVal.LClosureVal;

            //First call at offset 1: add(1, 2).

            stack.Write(1, TypedValue.MakeDouble(1));
            stack.Write(2, TypedValue.MakeDouble(2));
            retCount = LuaInterpreter.Execute(stack, retClosure, 1, 2);
            Assert.AreEqual(1, retCount);
            stack.Read(1, out retVal);

            Assert.AreEqual(LuaValueType.Number, retVal.ValueType);
            Assert.AreEqual(3, retVal.NumberVal);

            //Second call at offset 0: add(3, ret).

            stack.Write(0, TypedValue.MakeDouble(3));
            retCount = LuaInterpreter.Execute(stack, retClosure, 0, 2);
            Assert.AreEqual(1, retCount);
            stack.Read(0, out retVal);

            Assert.AreEqual(LuaValueType.Number, retVal.ValueType);
            Assert.AreEqual(6, retVal.NumberVal);
        }