Example #1
0
        public void GeneratorTest(SourceUnit sourceUnit, bool useLua52)
        {
            var options = new LuaCompilerOptions()
            {
                SkipFirstLine = true,
                UseLua52Features = useLua52,
            };

            var reader = TestUtils.OpenReaderOrIgnoreTest(sourceUnit.GetReader);

            TestUtils.AssertSyntaxError(() =>
            {
                var tokenizer = new Tokenizer(ErrorSink.Default, options);
                tokenizer.Initialize(null, reader, sourceUnit, SourceLocation.MinValue);
                var parser = new Parser(tokenizer, tokenizer.ErrorSink, options);
                var ast = parser.Parse();
                Assert.That(ast, Is.Not.Null);

                var codeContext = new CodeContext((LuaContext)sourceUnit.LanguageContext);

                var gen = new Generator(codeContext);
                var expr = gen.Compile(ast, sourceUnit);
                Assert.That(expr, Is.Not.Null);
            });
        }
Example #2
0
        public void ParserErrorReportTests(string luaFile, bool useLua52)
        {
            var options = new LuaCompilerOptions()
            {
                SkipFirstLine = true,
                UseLua52Features = useLua52,
            };

            var engine = Lua.CreateEngine();
            var context = Lua.GetLuaContext(engine);
            var sourceUnit = context.CreateFileUnit(luaFile);

            //var reader = TestUtils.OpenReaderOrIgnoreTest(() => File.OpenText(luaFile));
            var reader = TestUtils.OpenReaderOrIgnoreTest(sourceUnit.GetReader);

            var tokenizer = new Tokenizer(ErrorSink.Default, options);
            tokenizer.Initialize(null, reader, sourceUnit, SourceLocation.MinValue);
            var parser = new Parser(tokenizer, tokenizer.ErrorSink, options);

            TestUtils.AssertSyntaxError(() =>
            {
                var ast = parser.Parse();
                Assert.That(ast, Is.Not.Null);
            });
        }
Example #3
0
        static Func<dynamic> CompileString(CodeContext context, string source)
        {
            ContractUtils.RequiresNotNull(context, "context");

            var sourceUnit = context.Language.CreateSnippet(source, SourceCodeKind.Statements);

            //var options = (LuaCompilerOptions)context.GetCompilerOptions();
            //var errorSink = context.GetCompilerErrorSink();
            //var lexer = context.GetService<TokenizerService>();

            var lexer = new Tokenizer(ErrorSink.Default, LuaCompilerOptions.Default);
            lexer.Initialize(null, sourceUnit.GetReader(), sourceUnit, SourceLocation.MinValue);

            var parser = new Parser(lexer, lexer.ErrorSink);
            var ast = parser.Parse();
            var gen = new Generator(context);

            var fnStack =context.FunctionStacks.LastOrDefault(x => x.ExecScope != null);
            if (fnStack == default(FunctionStack))
                fnStack = new FunctionStack(context, null, LuaScope.CreateRoot(context), "=(compiled code)");

            var expr = gen.CompileInline(ast, fnStack.ExecScope, context.ExecutingScopeStorage, sourceUnit);
            return expr.Compile();
        }
Example #4
0
        static Func<dynamic> CompileString(LuaContext context, string source)
        {
            ContractUtils.RequiresNotNull(context, "context");

            var sourceUnit = context.CreateSnippet(source, SourceCodeKind.Statements);

            //var options = (LuaCompilerOptions)context.GetCompilerOptions();
            //var errorSink = context.GetCompilerErrorSink();
            //var lexer = context.GetService<TokenizerService>();

            var lexer = new Tokenizer(ErrorSink.Default, LuaCompilerOptions.Default);
            lexer.Initialize(null, sourceUnit.GetReader(), sourceUnit, SourceLocation.MinValue);

            var parser = new Parser(lexer, lexer.ErrorSink);
            var ast = parser.Parse();
            var gen = new Generator(context);
            var expr = gen.CompileInline(ast, context.Trace.CurrentEvaluationScope.GetRoot(), context.Trace.CurrentScopeStorage, sourceUnit);
            return expr.Compile();
        }