コード例 #1
0
        private void CompilePythonModule(string fileName, PythonCompilerSink sink, bool createMain)
        {
            assemblyGen.SetPythonSourceFile(fileName);
            CompilerContext context = new CompilerContext(fileName, sink);
            Parser          p       = Parser.FromFile(state, context);
            Stmt            body    = p.ParseFileInput();

            if (sink.Errors > 0)
            {
                return;
            }

            GlobalSuite gs         = IronPython.Compiler.Binder.Bind(body, context);
            string      moduleName = Path.GetFileNameWithoutExtension(fileName);
            TypeGen     tg         = OutputGenerator.GenerateModuleType(moduleName, assemblyGen);
            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, delegate(CodeGen cg) {
                    for (int i = 0; i < sourceFiles.Count; i++)
                    {
                        string otherModName = Path.GetFileNameWithoutExtension(sourceFiles[i]);
                        if (otherModName == moduleName)
                        {
                            continue;
                        }

                        FromImportStmt stmt = new FromImportStmt(
                            new DottedName(new Name[] { Name.Make(otherModName) }),
                            FromImportStmt.Star, null);
                        stmt.start = new Location(1, 1);
                        stmt.end   = new Location(1, 1);
                        stmt.Emit(cg);
                    }
                });
            }

            if (createMain)
            {
                CodeGen main = OutputGenerator.GenerateModuleEntryPoint(tg, init, Path.GetFileNameWithoutExtension(mainFile), referencedAssemblies);
                assemblyGen.SetEntryPoint(main.MethodInfo, targetKind);
            }

            AddPythonModuleAttribute(tg, moduleName);
            tg.FinishType();
        }