Example #1
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            try
            {
                switch (sourceUnit.Kind)
                {
                    case SourceCodeKind.SingleStatement:
                    case SourceCodeKind.Expression:
                    case SourceCodeKind.AutoDetect:
                    case SourceCodeKind.InteractiveCode:
                        return new TotemScriptCode(
                            engine, engine.ParseExprToLambda(sourceUnit),
                            sourceUnit);

                    //case SourceCodeKind.Statements:
                    //case SourceCodeKind.File:
                    //    return new TotemScriptCode(
                    //        engine, engine.ParseFileToLambda(sourceUnit.Path, sourceUnit.GetCode()),
                    //        sourceUnit);

                    default:
                        throw Assert.Unreachable;
                }
            }
            catch (Exception e)
            {
                // Real language implementation would have a specific type
                // of exception.  Also, they would pass errorSink down into
                // the parser and add messages while doing tighter error
                // recovery and continuing to parse.
                errorSink.Add(sourceUnit, e.Message, SourceSpan.None, 0,
                              Severity.FatalError);
                return null;
            }
        }
Example #2
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            var transformed = ParseScript(sourceUnit, (ScriptCompilerOptions)options, errorSink);

            return(new LegacyScriptCode(
                       transformed as MSAst.LambdaExpression ??
                       MSAst.Expression.Lambda(transformed),
                       sourceUnit));
        }
Example #3
0
        public CompilerContext(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink, ParserSink parserSink)
        {
            Contract.RequiresNotNull(sourceUnit, "sourceUnit");

            _sourceUnit = sourceUnit;
            _options = options ?? sourceUnit.Engine.GetDefaultCompilerOptions();
            _errors = errorSink ?? sourceUnit.Engine.GetCompilerErrorSink();
            _parserSink = parserSink ?? ParserSink.Null;
        }
        public PythonParser()
        {
            pythonEngine = Python.CreateEngine ();

            var langContext = HostingHelpers.GetLanguageContext (pythonEngine);
            compilerOptions = langContext.GetCompilerOptions ();
            langOptions = (PythonOptions) langContext.Options;

            walker = new CustomPythonWalker ();
        }
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            SilverLexer lexer = new SilverLexer("source", sourceUnit.GetCode());
            CommonTokenStream stream = new CommonTokenStream(lexer.Queue);
            SilverParser parser = new SilverParser(stream);
            lexer.SetLines (parser);
            parser.SourceUnit = sourceUnit;

            var res = parser.program();

            if (res != null) {
                Expression mainBlock = AstExpression.Block (res);
                return new SilverScriptCode(mainBlock, sourceUnit);
            } else {
                throw new SyntaxErrorException("Syntax error", sourceUnit, SourceSpan.None, 0, Severity.Error);
            }
        }
Example #6
0
 public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
 {
     throw new NotImplementedException();
 }
Example #7
0
        public void Compile_InvokeWithOptions()
        {
            // BUG - CompiledCode is only a stub currently.

            string singleExp = _codeSnippets[CodeType.SimpleExpressionOnePlusOne];
            ScriptSource source = _testEng.CreateScriptSourceFromString(singleExp,
                                                                      SourceCodeKind.Expression);
            // This is only a stub - I think.
            CompilerOptions options = new CompilerOptions();

            CompiledCode ccode = source.Compile(options);
            object results = ccode.Execute();
            Assert.Fail("This test is block by the missing CompiledCode class");
        }
Example #8
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            var context = this;
            // Here we parse the code and then eventually create Expressions.
            // For testing we will skip the parsing step and just do the generate step here.

            var statements = new List<Expression>();

            // Assume our code is: return 'abc' < 'xyz'

            var left = Expression.Constant("abc");
            var right = Expression.Constant("xyz");
            var opr = ExpressionType.LessThan;

            var binExpr = Expression.Dynamic(
                context.CreateBinaryOperationBinder(opr),
                typeof(bool), left, right);

            var binExprObj = Expression.Convert(binExpr, typeof(object));

            var blockReturnLabel = Expression.Label(typeof(object));
            statements.Add(Expression.Return(blockReturnLabel, binExprObj, typeof(object)));

            statements.Add(Expression.Label(blockReturnLabel, Expression.Constant(null)));

            var block = Expression.Block(typeof(object), statements);
            var code = Expression.Lambda<Func<dynamic>>(block);

            Func<dynamic> compiledCode = code.Compile();
            return new MyScriptCode(sourceUnit, compiledCode);
        }
Example #9
0
        public ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            Contract.RequiresNotNull(sourceUnit, "sourceUnit");

            if (options == null) options = GetCompilerOptions();
            if (errorSink == null) errorSink = Engine.GetCompilerErrorSink();

            CompilerContext context = new CompilerContext(sourceUnit, options, errorSink);

            CodeBlock block = ParseSourceCode(context);

            if (block == null) {
                throw new SyntaxErrorException();
            }

            //DumpBlock(block, sourceUnit.Id);

            AnalyzeBlock(block);

            DumpBlock(block, sourceUnit.Id);

            // TODO: ParseSourceCode can update CompilerContext.Options
            return new ScriptCode(block, Engine.GetLanguageContext(context.Options), context);
        }
Example #10
0
 public ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options)
 {
     return CompileSourceCode(sourceUnit, options, null);
 }
Example #11
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            ScriptCode res = CompileTotemCode(sourceUnit, options, errorSink);
            if (res != null)
            {
                Scope scope = res.CreateScope();

                // if this is an optimized module we need to initialize the optimized totemCode.
                // Optimized scopes come w/ extensions already attached so we use that to know
                // if we're optimized or not.
                TotemScopeExtension scopeExtension = (TotemScopeExtension)scope.GetExtension(ContextId);
                if (scopeExtension != null)
                {
                    InitializeModule(sourceUnit.Path, scopeExtension.ModuleContext, res, ModuleOptions.None);
                }
            }
            return res;
        }
Example #12
0
 /// <summary>
 /// Compiles a list of source units into a single module.
 /// <c>options</c> can be <c>null</c>
 /// <c>errroSink</c> can be <c>null</c>
 /// <c>dictionary</c> can be <c>null</c>
 /// </summary>
 public IScriptModule CompileModule(string name, ScriptModuleKind kind, CompilerOptions options, ErrorSink errorSink, 
     IAttributesCollection dictionary, params SourceUnit[] sourceUnits)
 {
     return _manager.CompileModule(name, kind, new Scope(dictionary), options, errorSink, sourceUnits);
 }
Example #13
0
        protected override Microsoft.Scripting.ScriptCode CompileSourceCode(Microsoft.Scripting.SourceUnit sourceUnit, Microsoft.Scripting.CompilerOptions options, Microsoft.Scripting.ErrorSink errorSink)
        {
            return(null);

            //    ClojureParser cp = new ClojureParser(sourceUnit);
            //    LambdaExpression ast;

            //    switch (sourceUnit.Kind)
            //    {
            //        case SourceCodeKind.InteractiveCode:
            //            {
            //                ScriptCodeParseResult result;
            //                object code = cp.ParseInteractiveStatement(out result);
            //                sourceUnit.CodeProperties = result;
            //                if (result != ScriptCodeParseResult.Complete)
            //                    return null;
            //                //ast = Generator.Generate(code, true);
            //                ast = Compiler.GenerateLambda(code, true);
            //            }
            //            break;

            //        default:
            //            sourceUnit.CodeProperties = ScriptCodeParseResult.Complete;
            //            ast = Generator.Generate(cp.ParseFile(), sourceUnit);
            //            break;
            //    }

            //    //ast = new GlobalLookupRewriter().RewriteLambda(ast);

            //    //DEBUG!!!
            //    //Compiler.SaveContext();

            //    return new LegacyScriptCode(ast, sourceUnit);
        }
        /// <summary>
        /// Compiles a list of source units into a single module.
        /// <c>scope</c> can be <c>null</c>.
        /// <c>options</c> can be <c>null</c>.
        /// <c>errorSink</c> can be <c>null</c>.
        /// </summary>
        public ScriptModule CompileModule(string name, ScriptModuleKind kind, Scope scope, CompilerOptions options, ErrorSink errorSink, 
            params SourceUnit[] sourceUnits)
        {
            Contract.RequiresNotNull(name, "name");
            Contract.RequiresNotNullItems(sourceUnits, "sourceUnits");

            // TODO: Two phases: parse/compile?

            // compiles all source units:
            ScriptCode[] scriptCodes = new ScriptCode[sourceUnits.Length];
            for (int i = 0; i < sourceUnits.Length; i++) {
                scriptCodes[i] = LanguageContext.FromEngine(sourceUnits[i].Engine).CompileSourceCode(sourceUnits[i], options, errorSink);
            }

            return CreateModule(name, kind, scope, scriptCodes);
        }
Example #15
0
 public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
 {
     Module module = Parser.ParseModule(sourceUnit, this);
     #if WRITE_AST
     module.WriteString(new AstWriter(Console.Out));
     #endif
     AstAnalyzer ctsg = new AstAnalyzer(this);
     return ctsg.Analyze(module, sourceUnit);
 }
Example #16
0
 /// <summary>
 /// Executes in a new scope created by the language.
 /// </summary>
 public object Execute(CompilerOptions options, ErrorSink errorSink)
 {
     return(Compile(options, errorSink).Run());
 }
Example #17
0
        internal static ScriptCode CompileTotemCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            var totemOptions = (TotemCompilerOptions)options;

            if (sourceUnit.Kind == SourceCodeKind.File)
            {
                totemOptions.Module |= ModuleOptions.Initialize;
            }

            CompilerContext context = new CompilerContext(sourceUnit, options, errorSink);
            TotemAst ast = ParseAndBindAst(context);
            if (ast == null)
            {
                return null;
            }

            return ast.ToScriptCode();
        }
 protected override LanguageContext GetLanguageContext(CompilerOptions compilerOptions)
 {
   return LanguageContext;
 }
Example #19
0
 public CompilerContext(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
     : this(sourceUnit, options, errorSink, null)
 {
 }