Exemplo n.º 1
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast) {            
            MSAst.Expression finalBody = Ast.Block(
                new[] { _globalCtx },
                Ast.Assign(
                    _globalCtx,
                    Ast.Call(typeof(PythonOps).GetMethod("CreateTopLevelCodeContext"),
                        _globalScope,
                        _language
                    )
                ),
                body
            );

            PythonCompilerOptions pco = ((PythonCompilerOptions)context.Options);
            string name = pco.ModuleName ?? "<unnamed>";
            var lambda = Ast.Lambda<Func<Scope, LanguageContext, object>>(
                finalBody, 
                name,
                new[] { _globalScope, _language } 
            );


            Func<Scope, LanguageContext, object> func;
            // TODO: adaptive compilation should be eanbled
            /*PythonContext pc = (PythonContext)context.SourceUnit.LanguageContext;
            if (pc.ShouldInterpret(pco, context.SourceUnit)) {
                func = CompilerHelpers.LightCompile(lambda);
            } else*/ {
                func = lambda.Compile(context.SourceUnit.EmitDebugSymbols);
            }

            return new PythonScriptCode(func, context.SourceUnit);
        }
		string Resolve(string variableName, PythonAst ast)
		{
			typeName = null;
			this.variableName = variableName;
			ast.Walk(this);
			return typeName;
		}
Exemplo n.º 3
0
 public override MSAst.LambdaExpression ReduceAst(PythonAst instance, string name) {
     return Ast.Lambda<Func<CodeContext, FunctionCode, object>>(
         AstUtils.Convert(instance.ReduceWorker(), typeof(object)),
         name,
         PythonAst._arrayFuncParams
     );
 }
Exemplo n.º 4
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast, Dictionary<int, bool> handlerLocations, Dictionary<int, Dictionary<int, bool>> loopAndFinallyLocations) {
            // create the CodeContext
            PythonGlobal[] globalArray = new PythonGlobal[_globals.Count];

            // now fill in the dictionary and create the array
            foreach (var global in _globals) {
                globalArray[global.Value.Index] = _globalVals[global.Key];
            }
            
            _array.Array = globalArray;

            // finally build the funcion that's closed over the array and
            string name = ((PythonCompilerOptions)context.Options).ModuleName ?? "<unnamed>";
            var func = Ast.Lambda<Func<FunctionCode, object>>(
                Ast.Block(
                    new[] { _globalArray, _globalContext },
                    Ast.Assign(_globalArray, Ast.Constant(globalArray)),
                    Ast.Assign(_globalContext, Ast.Constant(_context)),
                    Utils.Convert(body, typeof(object))
                ),
                name,
                new [] { AstGenerator._functionCode }
            );

            return new RuntimeScriptCode(context, func, ast, _context);
        }
        public override ScriptCode MakeScriptCode(PythonAst ast) {
            PythonCompilerOptions pco = ast.CompilerContext.Options as PythonCompilerOptions;
            // reduce to LightLambda then to Lambda
            var code = (MSAst.Expression<LookupCompilationDelegate>)ast.Reduce().Reduce();

            return new PythonSavableScriptCode(code, ast.SourceUnit, ast.GetNames(), pco.ModuleName);
        }
Exemplo n.º 6
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast) {
            // create the CodeContext
            PythonGlobal[] globalArray = new PythonGlobal[_globals.Count];

            // now fill in the dictionary and create the array
            foreach (var global in _globals) {
                SymbolId globalName = SymbolTable.StringToId(global.Key);
                
                globalArray[global.Value.Index] = _globalVals[globalName];
            }
            
            _array.Array = globalArray;

            // finally build the funcion that's closed over the array and
            string name = ((PythonCompilerOptions)context.Options).ModuleName ?? "<unnamed>";
            var func = Ast.Lambda<Func<object>>(
                Ast.Block(
                    new[] { _globalArray, _globalContext },
                    Ast.Assign(_globalArray, Ast.Constant(globalArray)),
                    Ast.Assign(_globalContext, Ast.Constant(_context)),
                    body
                ),
                name,
                new MSAst.ParameterExpression[0]
            );

            return new RuntimeScriptCode(context, func, ast, _context);
        }
        public DocumentationComment Parse(ProjectDom dom, string fileName, string content)
        {
            var document        = new DocumentationComment(fileName);
            var compilationUnit = new PythonCompilationUnit(fileName);

            document.CompilationUnit = compilationUnit;

            if (String.IsNullOrEmpty(content))
            {
                return(document);
            }

            var scriptSource = pythonEngine.CreateScriptSourceFromString(content, SourceCodeKind.File);
            var context      = new CompilerContext(HostingHelpers.GetSourceUnit(scriptSource),
                                                   compilerOptions, ErrorSink.Default);
            var parser = IronPythonParserEngine.CreateParser(context, langOptions);

            IronPythonAst ast = null;

            try {
                ast = parser.ParseFile(false);
            } catch (SyntaxErrorException exc) {
                // We could likely improve the error message
                document.Errors.Add(new Error(exc.Line, exc.Column, exc.Message));
                return(document);
            }

            walker.Reset();
            ast.Walk(walker);

            compilationUnit.Module = walker.Module;
            compilationUnit.Build();

            return(document);
        }
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast) {
            PythonCompilerOptions pco = ((PythonCompilerOptions)context.Options);
            PythonContext pc = (PythonContext)context.SourceUnit.LanguageContext;

            if (body is MSAst.ConstantExpression) {
                object value = ((MSAst.ConstantExpression)body).Value;
                return new PythonScriptCode(codeCtx => value, context.SourceUnit);
            }

            var lambda = Ast.Lambda<Func<CodeContext, object>>(
                Utils.Convert(body, typeof(object)),
                pco.ModuleName ?? "<unnamed>",
                ArrayGlobalAllocator._globalContextList
            );

            Func<CodeContext, object> func;

            if (pc.ShouldInterpret(pco, context.SourceUnit)) {
                func = CompilerHelpers.LightCompile(lambda);
            } else {
                func = lambda.Compile(context.SourceUnit.EmitDebugSymbols);
            }

            return new PythonScriptCode(func, context.SourceUnit);
        }
Exemplo n.º 9
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ lambda, CompilerContext/*!*/ compilerContext, PythonAst/*!*/ ast) {
            PythonContext context = (PythonContext)compilerContext.SourceUnit.LanguageContext;

            Type t = _typeGen.FinishType();
#if SILVERLIGHT
            _finalType.Value = t;
#endif

            // create the CodeContext for this optimized module
            InitOptimizedCodeContext(t);
            t.GetField("__global_context").SetValue(null, _context);

            // publish the cached constants
            foreach (var ci in _constants) {
                FieldInfo fi = t.GetField(ci.Value.Field.Name);
                fi.SetValue(null, ci.Key);
            }

            // publish all of the call site instances
            foreach (SiteInfo si in _sites) {
                FieldInfo fi = t.GetField(si.Field.Name);

                fi.SetValue(null, CallSite.Create(si.DelegateType, si.Binder));
            }

            // initialize all of the cached symbol IDs.
            ScriptingRuntimeHelpers.InitializeSymbols(t);

            string name = ((PythonCompilerOptions)compilerContext.Options).ModuleName ?? "<unnamed>";
            var func = Ast.Lambda<Func<object>>(lambda, name, new MSAst.ParameterExpression[0]);
            return new RuntimeScriptCode(compilerContext, func, ast, _context);
        }
Exemplo n.º 10
0
        public RuntimeScriptCode(PythonAst/*!*/ ast, CodeContext/*!*/ codeContext)
            : base(ast) {
            Debug.Assert(codeContext.GlobalScope.GetExtension(codeContext.LanguageContext.ContextId) != null);
            Debug.Assert(ast.Type == typeof(MSAst.Expression<Func<FunctionCode, object>>));

            _optimizedContext = codeContext;
        }
Exemplo n.º 11
0
 public RuntimeScriptCode(CompilerContext/*!*/ context, MSAst.Expression<Func<object>>/*!*/ expression, PythonAst/*!*/ ast, CodeContext/*!*/ codeContext)
     : base(context.SourceUnit) {
     _code = expression;
     _ast = ast;
     _context = context;
     _optimizedContext = codeContext;
 }
Exemplo n.º 12
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast) {
            MSAst.ParameterExpression scope = Ast.Parameter(typeof(Scope), "$scope");
            MSAst.ParameterExpression language = Ast.Parameter(typeof(LanguageContext), "$language ");

            // finally build the funcion that's closed over the array and
            var func = Ast.Lambda<Func<Scope, LanguageContext, object>>(
                Ast.Block(
                    new[] { GlobalArray },
                    Ast.Assign(
                        GlobalArray, 
                        Ast.Call(
                            null,
                            typeof(PythonOps).GetMethod("GetGlobalArray"),
                            scope
                        )
                    ),
                    Utils.Convert(body, typeof(object))
                ),
                ((PythonCompilerOptions)context.Options).ModuleName,
                new MSAst.ParameterExpression[] { scope, language }
            );

            PythonCompilerOptions pco = context.Options as PythonCompilerOptions;

            return new SavableScriptCode(func, context.SourceUnit, GetNames(), pco.ModuleName);
        }
Exemplo n.º 13
0
 public override LightLambdaExpression ReduceAst(PythonAst instance, string name) {
     return Utils.LightLambda<LookupCompilationDelegate>(
         typeof(object),
         AstUtils.Convert(instance.ReduceWorker(), typeof(object)),
         name,
         PythonAst._arrayFuncParams
     );
 }
Exemplo n.º 14
0
        public RuntimeScriptCode(CompilerContext/*!*/ context, MSAst.Expression<Func<FunctionCode, object>>/*!*/ expression, PythonAst/*!*/ ast, CodeContext/*!*/ codeContext)
            : base(context.SourceUnit) {
            Debug.Assert(codeContext.GlobalScope.GetExtension(codeContext.LanguageContext.ContextId) != null);

            _code = expression;
            _ast = ast;
            _context = context;
            _optimizedContext = codeContext;
        }
Exemplo n.º 15
0
 public override MSAst.LambdaExpression ReduceAst(PythonAst instance, string name) {
     return Ast.Lambda<Func<FunctionCode, object>>(
             Ast.Block(
                 new[] { PythonAst._globalArray, PythonAst._globalContext },
                 Ast.Assign(PythonAst._globalArray, instance.GlobalArrayInstance),
                 Ast.Assign(PythonAst._globalContext, Ast.Constant(instance.ModuleContext.GlobalContext)),
                 AstUtils.Convert(instance.ReduceWorker(), typeof(object))
             ),
             name,
             new[] { PythonAst._functionCode }
         );
 }
Exemplo n.º 16
0
 public override void PrepareScope(PythonAst ast, System.Runtime.CompilerServices.ReadOnlyCollectionBuilder<MSAst.ParameterExpression> locals, List<MSAst.Expression> init) {
     locals.Add(PythonAst._globalArray);
     init.Add(
         Ast.Assign(
             PythonAst._globalArray,
             Ast.Call(
                 typeof(PythonOps).GetMethod("GetGlobalArrayFromContext"),
                 PythonAst._globalContext
             )
         )
     );
 }
Exemplo n.º 17
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast, Dictionary<int, bool> handlerLocations, Dictionary<int, Dictionary<int, bool>> loopAndFinallyLocations) {
            PythonCompilerOptions pco = ((PythonCompilerOptions)context.Options);
            PythonContext pc = (PythonContext)context.SourceUnit.LanguageContext;

            var lambda = Ast.Lambda<Func<CodeContext, FunctionCode, object>>(
                Utils.Convert(body, typeof(object)),
                pco.ModuleName ?? "<unnamed>",
                ArrayGlobalAllocator._arrayFuncParams
            );

            return new PythonScriptCode(context, lambda, context.SourceUnit, handlerLocations, loopAndFinallyLocations, ast.Body.Span);
        }
Exemplo n.º 18
0
 public override MSAst.LambdaExpression ReduceAst(PythonAst instance, string name) {
     return Ast.Lambda<Func<CodeContext, FunctionCode, object>>(
         Ast.Block(
             new[] { PythonAst._globalArray },
             Ast.Assign(
                 PythonAst._globalArray,
                 Ast.Call(
                     null,
                     typeof(PythonOps).GetMethod("GetGlobalArrayFromContext"),
                     IronPython.Compiler.Ast.PythonAst._globalContext
                 )
             ),
             AstUtils.Convert(instance.ReduceWorker(), typeof(object))
         ),
         name,
         PythonAst._arrayFuncParams
     );
 }
Exemplo n.º 19
0
 public override LightLambdaExpression ReduceAst(PythonAst instance, string name) {
     return Utils.LightLambda<LookupCompilationDelegate>(
         typeof(object),
         Ast.Block(
             new[] { PythonAst._globalArray },
             Ast.Assign(
                 PythonAst._globalArray,
                 Ast.Call(
                     null,
                     typeof(PythonOps).GetMethod("GetGlobalArrayFromContext"),
                     IronPython.Compiler.Ast.PythonAst._globalContext
                 )
             ),
             AstUtils.Convert(instance.ReduceWorker(), typeof(object))
         ),
         name,
         PythonAst._arrayFuncParams
     );
 }
Exemplo n.º 20
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast) {            
            MSAst.Expression finalBody = Ast.Block(
                new[] { _globalCtx },
                Ast.Assign(
                    _globalCtx,
                    Ast.Call(typeof(PythonOps).GetMethod("CreateTopLevelCodeContext"),
                        _globalScope,
                        _language
                    )
                ),
                body
            );

            string name = ((PythonCompilerOptions)context.Options).ModuleName ?? "<unnamed>";
            var lambda = Ast.Lambda<Func<Scope, LanguageContext, object>>(
                finalBody, 
                name,
                new[] { _globalScope, _language } 
            );

            return new PythonScriptCode(lambda.Compile(context.SourceUnit.EmitDebugSymbols), context.SourceUnit);
        }
Exemplo n.º 21
0
        public override ScriptCode/*!*/ MakeScriptCode(MSAst.Expression/*!*/ body, CompilerContext/*!*/ context, PythonAst/*!*/ ast, Dictionary<int, bool> handlerLocations, Dictionary<int, Dictionary<int, bool>> loopAndFinallyLocations) {
            // finally build the funcion that's closed over the array
            var func = Ast.Lambda<Func<CodeContext, FunctionCode, object>>(
                Ast.Block(
                    new[] { GlobalArray },
                    Ast.Assign(
                        GlobalArray, 
                        Ast.Call(
                            null,
                            typeof(PythonOps).GetMethod("GetGlobalArrayFromContext"),
                            IronPython.Compiler.Ast.ArrayGlobalAllocator._globalContext 
                        )
                    ),
                    Utils.Convert(body, typeof(object))
                ),
                ((PythonCompilerOptions)context.Options).ModuleName,
                ArrayGlobalAllocator._arrayFuncParams
            );

            PythonCompilerOptions pco = context.Options as PythonCompilerOptions;

            return new PythonSavableScriptCode(func, context.SourceUnit, GetNames(), pco.ModuleName);
        }
 public override LightLambdaExpression ReduceAst(PythonAst instance, string name)
 {
     return(Utils.LightLambda <Func <FunctionCode, object> >(typeof(object), AstUtils.Convert(instance.ReduceWorker(), typeof(object)), name, new[] { PythonAst._functionCode }));
 }
Exemplo n.º 23
0
        private PythonAst ParseFileWorker(bool makeModule, bool returnValue) {
            _globalParent = new PythonAst(makeModule, _languageFeatures, false, _context);
            StartParsing();

            List<Statement> l = new List<Statement>();

            //
            // A future statement must appear near the top of the module. 
            // The only lines that can appear before a future statement are: 
            // - the module docstring (if any), 
            // - comments, 
            // - blank lines, and 
            // - other future statements. 
            // 

            MaybeEatNewLine();

            if (PeekToken(TokenKind.Constant)) {
                Statement s = ParseStmt();
                l.Add(s);
                _fromFutureAllowed = false;
                ExpressionStatement es = s as ExpressionStatement;
                if (es != null) {
                    ConstantExpression ce = es.Expression as ConstantExpression;
                    if (ce != null && ce.Value is string) {
                        // doc string
                        _fromFutureAllowed = true;
                    }
                }
            }

            MaybeEatNewLine();

            // from __future__
            if (_fromFutureAllowed) {
                while (PeekToken(Tokens.KeywordFromToken)) {
                    Statement s = ParseStmt();
                    l.Add(s);
                    FromImportStatement fis = s as FromImportStatement;
                    if (fis != null && !fis.IsFromFuture) {
                        // end of from __future__
                        break;
                    }
                }
            }

            // the end of from __future__ sequence
            _fromFutureAllowed = false;

            while (true) {
                if (MaybeEat(TokenKind.EndOfFile)) break;
                if (MaybeEatNewLine()) continue;

                Statement s = ParseStmt();
                l.Add(s);
            }

            Statement[] stmts = l.ToArray();

            if (returnValue && stmts.Length > 0) {
                ExpressionStatement exprStmt = stmts[stmts.Length - 1] as ExpressionStatement;
                if (exprStmt != null) {
                    var retStmt = new ReturnStatement(exprStmt.Expression);
                    stmts[stmts.Length - 1] = retStmt;
                    retStmt.SetLoc(_globalParent, exprStmt.Expression.IndexSpan);
                }
            }

            SuiteStatement ret = new SuiteStatement(stmts);
            ret.SetLoc(_globalParent, 0, GetEnd());
            return FinishParsing(ret);
        }
Exemplo n.º 24
0
 public PythonAst ParseTopExpression() {
     try {
         // TODO: move from source unit  .TrimStart(' ', '\t')
         _globalParent = new PythonAst(false, _languageFeatures, false, _context);
         ReturnStatement ret = new ReturnStatement(ParseTestListAsExpression());
         ret.SetLoc(_globalParent, 0, 0);
         return FinishParsing(ret);
     } catch (BadSourceException bse) {
         throw BadSourceError(bse);
     }
 }
Exemplo n.º 25
0
        public PythonAst ParseSingleStatement() {
            try {
                _globalParent = new PythonAst(false, _languageFeatures, true, _context);
                StartParsing();

                MaybeEatNewLine();
                Statement statement = ParseStmt();
                EatEndOfInput();
                return FinishParsing(statement);
            } catch (BadSourceException bse) {
                throw BadSourceError(bse);
            }
        }
Exemplo n.º 26
0
        private PythonAst FinishParsing(Statement ret) {
            var res = _globalParent;
            _globalParent = null;
            var lineLocs = _tokenizer.GetLineLocations();
            // update line mapping
            if (_sourceUnit.HasLineMapping) {
                List<int> newLineMapping = new List<int>();
                int last = 0;
                for (int i = 0; i < lineLocs.Length; i++) {
                    while (newLineMapping.Count < i) {
                        newLineMapping.Add(last);
                    }
                    last = lineLocs[i] + 1;
                    newLineMapping.Add(lineLocs[i]);
                }

                lineLocs = newLineMapping.ToArray();
            }
            res.ParsingFinished(lineLocs, ret, _languageFeatures);

            return res;
        }
Exemplo n.º 27
0
        //[stmt_list] Newline | compound_stmt Newline
        //stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
        //compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
        //Returns a simple or coumpound_stmt or null if input is incomplete
        /// <summary>
        /// Parse one or more lines of interactive input
        /// </summary>
        /// <returns>null if input is not yet valid but could be with more lines</returns>
        public PythonAst ParseInteractiveCode(out ScriptCodeParseResult properties) {
            bool parsingMultiLineCmpdStmt;
            bool isEmptyStmt = false;

            properties = ScriptCodeParseResult.Complete;

            _globalParent = new PythonAst(false, _languageFeatures, true, _context);
            StartParsing();
            Statement ret = InternalParseInteractiveInput(out parsingMultiLineCmpdStmt, out isEmptyStmt);

            if (_errorCode == 0) {
                if (isEmptyStmt) {
                    properties = ScriptCodeParseResult.Empty;
                } else if (parsingMultiLineCmpdStmt) {
                    properties = ScriptCodeParseResult.IncompleteStatement;
                }

                if (isEmptyStmt) {
                    return null;
                }

                return FinishParsing(ret);
            } else {
                if ((_errorCode & ErrorCodes.IncompleteMask) != 0) {
                    if ((_errorCode & ErrorCodes.IncompleteToken) != 0) {
                        properties = ScriptCodeParseResult.IncompleteToken;
                        return null;
                    }

                    if ((_errorCode & ErrorCodes.IncompleteStatement) != 0) {
                        if (parsingMultiLineCmpdStmt) {
                            properties = ScriptCodeParseResult.IncompleteStatement;
                        } else {
                            properties = ScriptCodeParseResult.IncompleteToken;
                        }
                        return null;
                    }
                }

                properties = ScriptCodeParseResult.Invalid;
                return null;
            }
        }
Exemplo n.º 28
0
 public void SetLoc(PythonAst globalParent, IndexSpan span) {
     _span = span;
     _parent = globalParent;
 }
Exemplo n.º 29
0
 public void SetLoc(PythonAst globalParent, int start, int end) {
     _span = new IndexSpan(start, end > start ? end - start : start);
     _parent = globalParent;
 }
Exemplo n.º 30
0
 public void SetLoc(PythonAst globalParent, int start, int end)
 {
     _span   = new IndexSpan(start, end > start ? end - start : start);
     _parent = globalParent;
 }
Exemplo n.º 31
0
 public override void PrepareScope(PythonAst ast, ReadOnlyCollectionBuilder <MSAst.ParameterExpression> locals, List <MSAst.Expression> init)
 {
     locals.Add(PythonAst._globalArray);
     init.Add(Ast.Assign(PythonAst._globalArray, ast._arrayExpression));
 }
Exemplo n.º 32
0
 public virtual ScriptCode MakeScriptCode(PythonAst ast) {
     return new RuntimeScriptCode(ast, ast.ModuleContext.GlobalContext);
 }
Exemplo n.º 33
0
 public void SetLoc(PythonAst globalParent, int start, int header, int end)
 {
     SetLoc(globalParent, start, end);
     _indexHeader = header;
 }
Exemplo n.º 34
0
 public override ScriptCode MakeScriptCode(PythonAst ast)
 {
     return(new PythonScriptCode(ast));
 }
Exemplo n.º 35
0
 public LookupVisitor(PythonAst ast, MSAst.Expression globalContext)
 {
     _globalContext = globalContext;
     _curScope      = ast;
 }
Exemplo n.º 36
0
 public abstract MSAst.LambdaExpression ReduceAst(PythonAst instance, string name);
Exemplo n.º 37
0
 public void SetLoc(PythonAst globalParent, IndexSpan span)
 {
     _span   = span;
     _parent = globalParent;
 }
Exemplo n.º 38
0
 public virtual void PrepareScope(PythonAst ast, ReadOnlyCollectionBuilder<MSAst.ParameterExpression> locals, List<MSAst.Expression> init) {
 }
Exemplo n.º 39
0
        public static void Check(PythonAst ast, CompilerContext context)
        {
            var finder = new StarredExpressionChecker(context);

            ast.Walk(finder);
        }