The early implementations of various dynamic languages on the. NET platform were relatively independent: for example, the 1.x Implementation of ironpython basically built their own implementations on the basis of. Net CLR. At ironpython2.x, the architecture of Dynamic Language has undergone great changes. This change has revolutionary significance and is as important and great as the current CLR (CLR unifies static language, GC, JIT... Saves a ton of nonsense ). Microsoft introduced DLR (Dynamic Language Runtime: Dynamic Language Runtime), and added a set of core features in. Net CLR to better support dynamic languages. DLR is built on Clr and provides services for all dynamic languages running on. NET platforms. Of course, DLR is also cross-platform (Mono )!
The functions provided by DLR include: A Shared dynamic type system and a standard host model, supporting faster dynamic generationCodeAnd quick symbol table. With these additional features, you can easily. net to build high-quality Dynamic Language implementation, so that you can use your favorite Dynamic Language to write code, the original dynamic language programming experience, thinking, skills, tools are also retained. The most important thing is that DLR allows all dynamic languages built on it to share code with each other and share class libraries written in CLR-Based Static languages. Ironpython and DLRSource codeIt can be downloaded from www.codeplex.com, which all follow Microsoft's open-source protocol.
We have heard that DLR has been around for some time, but what is it like? How can we provide these features and how can it work? Follow the "no secret before source code"Famous sayingLet's use some code to explain it. I will show and try to explain how ipy.exe executes a code file (how to perform syntax analysis and how to compile it ), we will also see how DLR improves performance through caching (DLR uses adaptive caching to generate fast and dynamicProgramAnd all the languages implemented on the DLR will benefit from this. DLR avoids repeated Search Class priority lists to make the code run faster. It also avoids parsing of. Net overload methods every time an object's method is called ).
Test code: Demo. py
Def Func (name ):
Text = " Hello, "
Return Text + Name
Print Func ( " World " )
This code is a little more than the classic "Hello World", so that we can discuss more interesting ironpython implementations. For example, we will encounter the identifier name binding and the cache of the "+" meaning in the func method. Before the experiment, go to http://www.codeplex.com/ironpythondownload the source code of beta 3: IronPython-2.0B3-Src.zip. Upload/. dll file. We need to set visual studio2008 first: Set the ironpythonconsole project to the startup project. And set its startup command line parameter to "demo. py" (copy demo. py to the generated directory of the project:... \ bin \ debug ).
Start and unzip ipy.exe
Now let's see how ipy.exe is started. The DLR provides several default console classes so that the language implementers can easily run an interpreter (the interpreter and runtime implementation of the language are still required ). Console class handles input and output, command line switches, and some sharing diagnostic debugging switches implemented by DLR. This enables the language implementer to first run some code on the DLR and provides a test tool. First... \ IronPython-2.0B3-Src \ IronPython-2.0B3 \ SRC \ ironpython \ Runtime \ pythoncontext. the statement in parsesourcecode () of the python context class in the CS file is: Using (parser = parser. createparser (context, pythoncontext. getpythonoptions (null.
Press F5 to run the program. When ironpython source code is parsed, it reaches the breakpoint we set. At this time, we can check the call stack to see how it runs to the breakpoint step by step:
The mainiterator of pythonconsolehostis the entry point of ipy.exe. Then, the program enters the DLR code. When the program returns the ironpython code to process the command line parameters, our script code starts to be executed. The pythonconsolehost class (inherited from the consolehost class provided by DLR) has very few codes, and it initializes the script engine. The consolehost class allows the language implementers to quickly create a console-style interactive interpreter. The pythonconsolehost class reloads the createcommandline () method of the consolehost class to return a pythoncommandline object. When consolehost calls the run () method of CommandLine, The ironpython code is executed. Pythoncommandline does some initialization work, such as setting the path of the python loading module, initializing some built-in modules...
Next article: Inside ironpython: ironpython ast syntax tree (2/2)