Beispiel #1
0
        public static LuaResult Compile(
            StaticMetaTables staticTables,
            string source,
            dynamic globals = null,
            string name = null)
        {
            var result = new LuaResult();
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            try
            {
                var stream = new ANTLRStringStream(source);
                var lexer = new ChunkLexer(stream);
                var tokenStream = new CommonTokenStream(lexer);
                var parser = new ChunkParser(tokenStream);

                var r = parser.chunk();
                result.Errors.AddRange(parser.Errors);

                Expression e;
                var scope = Scope.NewTopLevelScop();
                var chunk = new Chunk();
                result.Errors.AddRange(chunk.Generate(staticTables, scope, r.Tree, out e));

                var fnExp = Expression.Lambda<Func<Table, object>>(e, scope.Env.GlobalParameter);
                result.Chunk = new CompiledChunk(fnExp.Compile(), globals ?? new Table(), name);
            }
            finally
            {
                stopwatch.Stop();
                result.ElapsedTime = stopwatch.Elapsed;
                result.Success = !result.Errors.ContainsError();
            }
            return result;
        }