Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand IronPython Compiler

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to understand the IronPython compiler, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Since the official release of IronPython, driven by the love of the Python language, and I want to understand the IronPython compiler, analyzer and other programs of the programming language, how they work, so I began to learn the IronPython compiler.

But the code has also been watching for some time. Before, I looked at some implementation details, but the more I saw it, the more confused I became. Now I find it necessary to change the strategy, because we always start to understand how a system uses it, and if we directly understand how it works at the bottom, we may get lost in the ocean of code. So I am also going to adopt a top-down analysis method to pick up soft persimmons and start from a simple and macro point of view. As for the specific implementation details, you can slowly and in-depth study.

Going straight to the point, we see the Compile () method, which is the main control method responsible for compilation. This method is not difficult to understand, I read it once, and the notes are as follows:

/ compile / public void Compile () {string fullPath = Path.GetFullPath (outputAssembly); string outDir = Path.GetDirectoryName (fullPath); string fileName = Path.GetFileName (outputAssembly); / / acceptance pool of the Python compiler PythonCompilerSink sink = new PythonCompilerSink (compilerSink) / / Assembly generator assemblyGen = new AssemblyGen (Path.GetFileNameWithoutExtension (outputAssembly), outDir, fileName, includeDebugInformation, staticTypes, executable, machine); / / whether to set the entry point (entry point) bool entryPointSet = false / / set the default master file (for non-DLL output file types) if (mainFile = = null & & sourceFiles.Count = = 1 & & targetKind! = PEFileKinds.Dll) {mainFile = sourceFiles [0];} / one pair of each source file compiles foreach (string sourceFile in sourceFiles) {/ / whether to generate the Main method bool createMainMethod = sourceFile = = mainFile / / each source code file is compiled into a module CompilePythonModule (sourceFile, sink, createMainMethod); if (sink.Errors > 0) return; if (createMainMethod) {entryPointSet = true;}}

In this code, the private method CompilePythonModule () of the IronPython compiler itself is called to complete the function of the compilation module. Let's take a look at what this method does:

/ / add all resource files to the assembly if (resourceFiles! = null) {foreach (ResourceFile rf in resourceFiles) {assemblyGen.AddResourceFile (rf.Name, rf.File, rf.PublicResource? ResourceAttributes.Public: ResourceAttributes.Private);}} / / A pair of non-DLL object files must have an entry point if (targetKind! = PEFileKinds.Dll & &! entryPointSet) {sink.AddError ("", string.Format ("Need an entry point for target kind {0}", targetKind), String.Empty, CodeSpan.Empty,-1, Severity.Error) } / / the final output assembly assemblyGen.Dump ();} this article is from the CSDN blog, please indicate the source of the reprint: http://blog.csdn.net/inelm/archive/2006/10/09/4612996.aspx

In the above two methods, we see that several important classes have emerged, which will be the key clues for our next analysis:

/ / compilation module private void CompilePythonModule (string fileName, PythonCompilerSink sink, bool createMain) {/ / set the current source file to compile assemblyGen.SetPythonSourceFile (fileName); / / create compiler environment object CompilerContext context = new CompilerContext (fileName, sink); / / create parser Parser p = Parser.FromFile (state, context) / / call the parser's analysis method to get a statement object (the statement should take advantage of a nested concept of the combination pattern, this statement represents a large statement in the whole file) Statement body = p.ParseFileInput (); if (sink.Errors > 0) return; / / create a global suite. It may refer to the dictionary object globals (). To be analyzed. / / what the Binder does need to be studied. GlobalSuite gs = Compiler.Ast.Binder.Bind (body, context); string moduleName = GetModuleFromFilename (fileName); / / you see TypeGen here, this class represents a type generator / / tg points to a module type (in IronPython, each module is generated as a corresponding class. ) TypeGen tg = OutputGenerator.GenerateModuleType (moduleName, assemblyGen); / / compile the _ _ init__ method of the module (guess) CodeGen init = CompileModuleInit (context, gs, tg, moduleName)

So far, we have roughly seen the workflow of the IronPython compiler, starting from a series of source code files, resource files, and other configuration properties, through the operation of Parser, various Generator, and finally to the Dump () method of AssemblyGenerator, outputting the resulting assembly.

On how to understand the IronPython compiler to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report