private GlobalSuite DoBind(Stmt root) { GlobalSuite global = new GlobalSuite(root); current = global; // Detect all local names root.Walk(this); // Binding the free variables foreach (ScopeStatement scope in processed) { scope.BindNames(global, this); } // Validate foreach (ScopeStatement scope in processed) { if ((scope.ScopeInfo & (ScopeStatement.ScopeFlags.ContainsFreeVariables | ScopeStatement.ScopeFlags.ContainsImportStar | ScopeStatement.ScopeFlags.ContainsUnqualifiedExec | ScopeStatement.ScopeFlags.ContainsNestedFreeVariables)) == 0) { continue; } FuncDef func; if ((func = scope as FuncDef) != null) { if (func.ContainsImportStar && func.IsClosure) { ReportSyntaxError(String.Format("import * is not allowed in function '{0}' because it is a nested function", func.name.GetString()), func); } if (func.ContainsImportStar && func.parent is FuncDef) { ReportSyntaxError(String.Format("import * is not allowed in function '{0}' because it is a nested function", func.name.GetString()), func); } if (func.ContainsImportStar && func.ContainsNestedFreeVariables) { ReportSyntaxError(String.Format("import * is not allowed in function '{0}' because it contains a nested function with free variables", func.name.GetString()), func); } if (func.ContainsUnqualifiedExec && func.ContainsNestedFreeVariables) { ReportSyntaxError(String.Format("unqualified exec is not allowed in function '{0}' it contains a nested function with free variables", func.name.GetString()), func); } } ClassDef cls; if ((cls = scope as ClassDef) != null) { if (cls.ContainsImportStar) { // warning } } } return(global); }
public static FrameCode GenerateSnippet(CompilerContext context, Stmt body, string name, bool printExprStmts) { GlobalSuite gs = Binder.Bind(body, context); if (name.Length == 0) { name = "<empty>"; // The empty string isn't a legal method name } CodeGen cg; List <object> staticData = null; TypeGen tg = null; cg = snippetAssembly.DefineDynamicMethod(name, typeof(object), new Type[] { typeof(Frame) }); staticData = new List <object>(); cg.staticData = staticData; cg.doNotCacheConstants = true; cg.ModuleSlot = cg.GetLocalTmp(typeof(PythonModule)); cg.ContextSlot = cg.GetArgumentSlot(0); cg.Names = CodeGen.CreateFrameNamespace(cg.ContextSlot); cg.Context = context; cg.printExprStmts = printExprStmts; if (printExprStmts) { cg.Names.EnsureLocalSlot(Name.Make("_")); } cg.ContextSlot.EmitGet(cg); cg.EmitFieldGet(typeof(Frame), "__module__"); cg.ModuleSlot.EmitSet(cg); if (context.TrueDivision) { cg.ContextSlot.EmitGet(cg); cg.EmitInt(1); cg.EmitCall(typeof(ICallerContext), "set_TrueDivision"); } gs.Emit(cg); if (!(body is ReturnStmt)) { cg.EmitPosition(Location.None, Location.None); cg.EmitReturn(null); } if (tg != null) { tg.FinishType(); } FrameCode frameCode = new FrameCode(name, (FrameCodeDelegate)cg.CreateDelegate(typeof(FrameCodeDelegate)), staticData); return(frameCode); }
public static PythonModule GenerateModule(SystemState state, CompilerContext context, Stmt body, string moduleName, string outSuffix) { if (Options.GenerateModulesAsSnippets) { return(GenerateModuleAsSnippets(state, context, body, moduleName)); } GlobalSuite gs = IronPython.Compiler.Binder.Bind(body, context); return(DoGenerateModule(state, context, gs, moduleName, context.SourceFile, outSuffix)); }
internal static CodeGen GenerateModuleInitialize(CompilerContext context, GlobalSuite gs, TypeGen tg, CustomModuleInit customInit) { CodeGen ncg = tg.DefineUserHiddenMethod(MethodAttributes.Public, "Initialize", typeof(void), Type.EmptyTypes); ncg.Context = context; if (Options.StaticModules) { ncg.Names = CodeGen.CreateStaticFieldNamespace(tg); } else { throw new NotImplementedException(); //.names = new FieldNamespace(null, tg, new ModuleSlot(tg.myType)); } if (context.TrueDivision) { ncg.ModuleSlot.EmitGet(ncg); ncg.EmitInt(1); ncg.EmitCall(typeof(ICallerContext), "set_TrueDivision"); } // Add __doc__ and __name__ ncg.Names.CreateGlobalSlot(Name.Make("__doc__")); ncg.Names.CreateGlobalSlot(Name.Make("__name__")); string doc = gs.GetDocString(); ncg.EmitStringOrNull(doc); ncg.EmitSet(Name.Make("__doc__")); if (customInit != null) { customInit(ncg); } gs.Emit(ncg); ncg.EmitPosition(Location.None, Location.None); ncg.EmitReturn(); FinishCustomDict(tg, ncg.Names); return(ncg); }
public void BindNames(GlobalSuite globals, Binder binder) { foreach (KeyValuePair <Name, Binding> kv in names) { Binding b = kv.Value; Name n = kv.Key; // Global binding if (b.IsGlobal) { globals.Bind(n); continue; } // Free variable binding if (b.IsFree) { // Bind in the parent if (parent != null) { if (parent.BindInParent(kv.Key, binder)) { IsClosure = true; continue; } } // Free, but not defined in parent scopes ==> global globals.Bind(n); b.MakeUnboundGlobal(); continue; } // Defined in this scope if (b.IsBound) { continue; } Debug.Fail("Unbound name", n.GetString()); } }
public static PythonModule GenerateModule(SystemState state, CompilerContext context, Stmt body, string moduleName) { if (Options.GenerateModulesAsSnippets) { return(GenerateModuleAsSnippets(state, context, body, moduleName)); } GlobalSuite gs = IronPython.Compiler.Binder.Bind(body, context); string suffix = ""; int counter = 0; for (; ;) { try { return(DoGenerateModule(state, context, gs, moduleName, context.SourceFile, suffix)); } catch (System.IO.IOException) { suffix = "_" + (++counter).ToString(); } } }
internal static CodeGen GenerateModuleInitialize(CompilerContext context, GlobalSuite gs, TypeGen tg) { return(GenerateModuleInitialize(context, gs, tg, null)); }
private static PythonModule DoGenerateModule(SystemState state, CompilerContext context, GlobalSuite gs, string moduleName, string sourceFileName, string outSuffix) { string fullPath; string outDir; string fileName; if (sourceFileName == "<stdin>") { fullPath = Environment.CurrentDirectory; outDir = Environment.CurrentDirectory; fileName = "__stdin__"; } else { fullPath = Path.GetFullPath(sourceFileName); outDir = Options.BinariesDirectory == null?Path.GetDirectoryName(fullPath) : Options.BinariesDirectory; fileName = Path.GetFileNameWithoutExtension(sourceFileName); } AssemblyGen ag = new AssemblyGen(moduleName + outSuffix, outDir, fileName + outSuffix + ".exe", true); ag.SetPythonSourceFile(fullPath); TypeGen tg = GenerateModuleType(moduleName, ag); CodeGen cg = GenerateModuleInitialize(context, gs, tg); CodeGen main = GenerateModuleEntryPoint(tg, cg, moduleName, null); ag.SetEntryPoint(main.MethodInfo, PEFileKinds.ConsoleApplication); Type ret = tg.FinishType(); Assembly assm = ag.DumpAndLoad(); ret = assm.GetType(moduleName); CustomFieldIdDict dict = (CustomFieldIdDict)ret.GetConstructor(Type.EmptyTypes).Invoke(new object[0]); InitializeModule init = (InitializeModule)Delegate.CreateDelegate( typeof(InitializeModule), dict, "Initialize"); PythonModule pmod = new PythonModule(moduleName, dict, state, init); pmod.InitializeBuiltins(); return(pmod); }
// GlobalSuite public override void PostWalk(GlobalSuite node) { current = current.parent; }
public virtual void PostWalk(GlobalSuite node) { }
// GlobalSuite public virtual bool Walk(GlobalSuite node) { return(true); }