Introduction to IronPython Compiler

Source: Internet
Author: User

Since the official release of IronPython, I have been driven by my love for the Python language. At the same time, I want to learn how IronPython compiler, analyzer, and other programs of the programming language work, so I started learning the IronPython compiler.

However, the Code has been read for a while. I used to look at some implementation details, and the results become more and more confused. Now I find that we need to change the strategy, because we understand that a system always starts to understand its usage. If we directly understand the underlying operating principles, it may be lost in the Code ocean. So I am also preparing to take the top-down analysis method, pick up the soft persimmon, and start from a simple, Macro. The specific implementation details can be further studied.

Go straight to the topic. We can see the Compile () method, which is the main control method for compilation. This method is not hard to understand. I read it again, and the notes are as follows:

 
 
  1. ///<Summary> 
  2. /// Compile
  3. ///</Summary> 
  4. Public void Compile (){
  5. StringFullPath= Path. GetFullPath (outputAssembly );
  6. StringOutDir=Path. GetDirectoryName (fullPath );
  7. StringFileName=Path. GetFileName (outputAssembly );
  8.  
  9. // The acceptance pool of the Python Compiler
  10. PythonCompilerSinkSink=NewPythonCompilerSink (compilerSink );
  11.  
  12. // Assembly Generator
  13. AssemblyGen=NewAssemblyGen (
  14. Path. GetFileNameWithoutExtension (outputAssembly ),
  15. OutDir, fileName, includeDebugInformation, staticTypes, executable, machine
  16. );
  17.  
  18. // Whether to set the entry point)
  19. BoolEntryPointSet=False;
  20.  
  21. // Set the default main file type for non-DLL output files)
  22. If (MainFile= Null &&SourceFiles. Count= 1 & targetKind! = PEFileKinds. Dll ){
  23. MainFile=SourceFiles[0];
  24. }
  25.  
  26. // Compile each source file in sequence
  27. Foreach (string sourceFile in sourceFiles ){
  28. // Whether the Main method is generated
  29. BoolCreateMainMethod=SourceFile= MainFile;
  30. // Each source code file is compiled into a module
  31. CompilePythonModule (sourceFile, sink, createMainMethod );
  32.  
  33. If (sink. Errors>0) return;
  34.  
  35. If (createMainMethod ){
  36. EntryPointSet=True;
  37. }
  38. }

In this Code, the private method CompilePythonModule () of the IronPython compiler is called to compile the module. Next let's take a look at what this method is doing:

 
 
  1. // Add all resource files to the Assembly in sequence
  2. If (resourceFiles! = Null ){
  3. Foreach (ResourceFile rf in resourceFiles ){
  4. AssemblyGen. AddResourceFile (rf. Name, rf. File, rf. PublicResource? ResourceAttributes. Public: ResourceAttributes. Private );
  5. }
  6. }
  7.  
  8. // For non-DLL target files, there must be an entry point
  9. If (targetKind! = PEFileKinds. Dll &&! EntryPointSet ){
  10. Sink. addError ("", string. format ("Need an entry point for target kind {0}", targetKind), String. empty, CodeSpan. empty,-1, Severity. error );
  11. }
  12.  
  13. // The final output assembly
  14. AssemblyGen. Dump ();
  15. }
  16.  
  17. This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/inelm/archive/2006/10/09/4612996.aspx

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

 
 
  1. // Compile the module
  2. Private void CompilePythonModule (string fileName, PythonCompilerSink, bool createMain ){
  3. // Set the source file to be compiled
  4. AssemblyGen. SetPythonSourceFile (fileName );
  5. // Create a compiler environment object
  6. CompilerContextContext=NewCompilerContext (fileName, sink );
  7. // Create a Analyzer
  8. ParserP=Parser. FromFile (state, context );
  9. // Call the analyzer analysis method to obtain a statement object statement that uses the nested concept of the combination mode. This statement represents a large statement in the entire file)
  10. StatementBody=P. ParseFileInput ();
  11.  
  12. If (sink. Errors>0) return;
  13.  
  14. // Create a global suite ?? It may be the dictionary object globals. To be analyzed...
  15. // What is the Binder here.
  16. GlobalSuiteGs=Compiler. Ast. Binder. Bind (body, context );
  17. StringModuleName=GetModuleFromFilename(FileName );
  18. // Here we see TypeGen, which represents a Type Generator
  19. // Tg points to a module type IronPython, and each module is generated as a corresponding class .)
  20. TypeGenTg=OutputGenerator. GenerateModuleType (moduleName, assemblyGen );
  21. // Compile the _ init _ method of the module ?? Guess)
  22. CodeGenInit=CompileModuleInit(Context, gs, tg, moduleName );

So far, we have seen the IronPython compiler workflow, starting from a series of source code files, resource files, and other configuration attributes, through the operations of Parser and various Generator, the Dump () method of AssemblyGenerator is finally reached, and the compilation result assembly is output.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and coding Solutions
  5. Analysis of Python GIL and thread security

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.