Exemple #1
0
        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);
        }
Exemple #2
0
        private static TypedValue[] MakeCollisionKeys(int sameCount, int diffCount)
        {
            List <TypedValue> retSame = new(), retDiff = new();

            var bucketSize = GetBucketSize(sameCount + diffCount);

            var zero      = TypedValue.MakeString((0).ToString());
            var zeroIndex = zero.GetHashCode() % bucketSize;

            retSame.Add(zero);

            int sameAdded = 1, diffAdded = 0;
            int i = 1;

            while (sameAdded < sameCount || diffAdded < diffCount)
            {
                var key    = TypedValue.MakeString((i++).ToString());
                var isSame = (key.GetHashCode() % bucketSize) == zeroIndex;
                if (isSame && sameAdded < sameCount)
                {
                    sameAdded += 1;
                    retSame.Add(key);
                }
                else if (!isSame && diffAdded < diffCount)
                {
                    diffAdded += 1;
                    retDiff.Add(key);
                }
            }
            Debug.Assert(retSame.Count == sameCount);
            Debug.Assert(retDiff.Count == diffCount);
            return(retSame.Concat(retDiff).ToArray());
        }
Exemple #3
0
        public void ForGeneric()
        {
            var args    = Array.Empty <TypedValue>();
            var results = new TypedValue[1];

            TestHelper.DoString(
                "local a = { x = 10, y = 20, 30, 40 } " +
                "local b = {} " +
                "for k, v in next, a do " +
                "    b[k] = v " +
                "end " +
                "return b",
                TestHelper.DefaultEnv, args, results);
            Assert.AreEqual(LuaValueType.Table, results[0].ValueType);
            var table = results[0].TableVal;

            Assert.AreEqual(2, table.SequenceSize);

            Assert.True(table.GetRaw(TypedValue.MakeString("x"), out var val));
            Assert.AreEqual(TypedValue.MakeInt(10), val);
            Assert.True(table.GetRaw(TypedValue.MakeString("y"), out val));
            Assert.AreEqual(TypedValue.MakeInt(20), val);
            Assert.True(table.GetRaw(TypedValue.MakeInt(1), out val));
            Assert.AreEqual(TypedValue.MakeInt(30), val);
            Assert.True(table.GetRaw(TypedValue.MakeInt(2), out val));
            Assert.AreEqual(TypedValue.MakeInt(40), val);
        }
Exemple #4
0
        static TestHelper()
        {
            AssertEnv = new Table();
            AssertEnv.SetRaw(TypedValue.MakeString("assert"), TypedValue.MakeNClosure(AssertFunc));

            DefaultEnv = new Table();
            DefaultEnv.SetRaw(TypedValue.MakeString("assert"), TypedValue.MakeNClosure(AssertFunc));
            DefaultEnv.SetRaw(TypedValue.MakeString("add"), TypedValue.MakeNClosure(AddFunc));
        }
Exemple #5
0
        private static int GetBucketSize(int count)
        {
            var t = new Table();

            for (int i = 0; i < count; ++i)
            {
                t.SetRaw(TypedValue.MakeString(i.ToString()), TypedValue.True);
            }
            return(t.BucketSize);
        }
        public LiteralExpressionGenerator(FunctionGenerator func, LiteralExpressionSyntaxNode expr) : base(0)
        {
            var type = expr.SpecializationType.GetVMSpecializationType();
            var k    = func.Constants;

            _constIndex = type switch
            {
                VMSpecializationType.Nil => k.GetUnspecializedNil(),
                VMSpecializationType.Bool => expr.BoolValue ? k.GetUnspecializedTrue() : k.GetUnspecializedFalse(),
                VMSpecializationType.Double => k.AddUnspecialized(TypedValue.MakeDouble(expr.DoubleValue)),
                VMSpecializationType.Int => k.AddUnspecialized(TypedValue.MakeInt(expr.Int32Value)),
                VMSpecializationType.String => k.AddUnspecialized(TypedValue.MakeString(expr.StringValue)),
                _ => throw new Exception(), //Internal exception.
            };

            _type = type.Deoptimize();
        }