// GlobalSuite public bool Walk(GlobalSuite node) { current = node; return Process(node); }
// GlobalSuite public override void PostWalk(GlobalSuite node) { PopScope(); }
internal void BindNames(GlobalSuite globals, Binder binder) { foreach (KeyValuePair<SymbolId, Binding> kv in names) { Binding b = kv.Value; SymbolId 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 void PostWalk(GlobalSuite node) { AssertCurrent(node); current = current.Parent; }
public ModuleScope(Module module, Scope parent, GlobalSuite statement) : base(module, parent) { this.statement = statement; }
private CodeGen CompileModuleInit(CompilerContext context, GlobalSuite gs, TypeGen tg, string moduleName) { CodeGen init; if (!AutoImportAll) { init = OutputGenerator.GenerateModuleInitialize(context, gs, tg); } else { // auto-import all compiled modules, useful for CodeDom scenarios. init = OutputGenerator.GenerateModuleInitialize(context, gs, tg, staticTypes, delegate(CodeGen cg) { Location dummyLocation = new Location(1, 1); for (int i = 0; i < sourceFiles.Count; i++) { string otherModName = GetModuleFromFilename(sourceFiles[i]); if (otherModName == moduleName) continue; FromImportStatement stmt = new FromImportStatement( new DottedName(new SymbolId[] { SymbolTable.StringToId(otherModName) }), FromImportStatement.Star, null); stmt.Start = dummyLocation; stmt.End = dummyLocation; stmt.Emit(cg); } // Import the first part of all namespaces in all referenced assemblies // First, determine the set of unique such prefixes Dictionary<string, object> nsPrefixes = new Dictionary<string, object>(); foreach (string name in ReferencedAssemblies) { Assembly a = LoadAssembly(name); foreach (Type t in a.GetTypes()) { // We only care about public types if (!t.IsPublic) continue; // Ignore types that don't have a namespace if (t.Namespace == null) continue; string nsPrefix = t.Namespace.Split('.')[0]; nsPrefixes[nsPrefix] = null; } } // Import all the uniquer prefixes we found foreach (string nsPrefix in nsPrefixes.Keys) { SymbolId symbolId = SymbolTable.StringToId(nsPrefix); cg.Names.CreateGlobalSlot(symbolId); DottedName dottedName = new DottedName(new SymbolId[] { symbolId }); ImportStatement importStmt = new ImportStatement( new DottedName[] { dottedName }, new SymbolId[] { SymbolTable.Empty }); importStmt.Start = dummyLocation; importStmt.End = dummyLocation; importStmt.Emit(cg); } }); } return init; }
public override bool Walk(GlobalSuite node) { node.CreateGlobalSlots(stack.Peek().StaticConstructor); return true; }
internal static CodeGen GenerateModuleInitialize(CompilerContext context, GlobalSuite gs, TypeGen tg, bool staticTypes, CustomModuleInit customInit) { CodeGen ncg = tg.DefineMethodOverride(typeof(CompiledModule).GetMethod("Initialize", BindingFlags.Public | BindingFlags.Instance)); ncg.Context = context; ncg.EmitSetTraceBackUpdateStatus(false); ncg.Names = CodeGen.CreateStaticFieldNamespace(tg); if (context.TrueDivision) { ncg.ModuleSlot.EmitGet(ncg); ncg.EmitInt(1); ncg.EmitCall(typeof(ICallerContext), "set_TrueDivision"); } // Add __doc__ and __name__ ncg.Names.CreateGlobalSlot(SymbolTable.Doc); ncg.Names.CreateGlobalSlot(SymbolTable.Name); string doc = gs.Documentation; ncg.EmitStringOrNull(doc); ncg.EmitSet(SymbolTable.Doc); if (customInit != null) customInit(ncg); if (staticTypes) { UserTypeGenerator.DoStaticCompilation(gs, ncg); } else { gs.Emit(ncg); } ncg.EmitPosition(Location.None, Location.None); ncg.EmitReturn(); ncg.Finish(); FinishCustomDict(tg, ncg.Names); return ncg; }
// GlobalSuite public virtual bool Walk(GlobalSuite node) { return true; }
public virtual void PostWalk(GlobalSuite node) { }
private GlobalSuite DoBind(Statement 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.ScopeAttributes.ContainsFreeVariables | ScopeStatement.ScopeAttributes.ContainsImportStar | ScopeStatement.ScopeAttributes.ContainsUnqualifiedExec | ScopeStatement.ScopeAttributes.ContainsNestedFreeVariables)) == 0) { continue; } FunctionDefinition func; if ((func = scope as FunctionDefinition) != 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 FunctionDefinition) { 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); } if (func.ContainsUnqualifiedExec && func.IsClosure) { ReportSyntaxError(String.Format("unqualified exec is not allowed in function '{0}' it is a nested function", func.Name.GetString()), func); } } ClassDefinition cls; if ((cls = scope as ClassDefinition) != null) { if (cls.ContainsImportStar) { // warning } } } return global; }
// GlobalSuite public override void PostWalk(GlobalSuite node) { current = current.Parent; }
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); ag.AddPythonModuleAttribute(tg, moduleName); Type ret = tg.FinishType(); Assembly assm = ag.DumpAndLoad(); ret = assm.GetType(moduleName); PythonModule pmod = CompiledModule.Load(moduleName, ret, state); return pmod; }
private Module DoAnalyze(Modules modules, string name, Statement root) { GlobalSuite global = new GlobalSuite(root); module = new Module(modules, name, global, scopes); ModuleScope modsc; module.ModuleScope = modsc = new ModuleScope(module, null, global); PushScope(modsc); root.Walk(this); foreach (FieldAssignment fer in this.fields) { fer.Infer(module); } return module; }
public static TypeGen DoStaticCompilation(GlobalSuite gs, CodeGen cg) { GeneratorWalker gw = new GeneratorWalker(cg); gs.Walk(gw); return gw.FinishedType; }
public Module(Modules references, string name, GlobalSuite global, Dictionary<ScopeStatement, Scope> scopes) { this.references = references; this.name = name; this.scopes = scopes; this.global = global; }
internal static CodeGen GenerateModuleInitialize(CompilerContext context, GlobalSuite gs, TypeGen tg) { return GenerateModuleInitialize(context, gs, tg, false /*staticTypes*/, null); }