1) DLR - Introduction into implementation of .NET languages
I’m not an English native speaker however I decided to write this tutorial in my poor Czech English because there isn’t much information about implementing dynamic languages on the DLR.
Why dynamic languages?
Dynamic languages have more flexible type system and they are more convenient to rapid development. Two examples of dynamic languages are Python and Ruby, both of which are widely used for web development.
Is .NET powerful enough to support dynamic languages?
.NET was designed to support different programming languages on the Common Language Runtime (CLR). CLR is an interpreter of low-level language called Common Intermediate Language (CIL, previously known as MSIL). Programmers write applications in some high-level .NET language such as C# or VB.NET and compiler transforms their code into CIL.

- Compiler transforms C#, VB.NET or other .NET language code into CIL (exe, dll)
- CIL code can be executed on any computer where is CLR installed (it is a part of the .NET Framework)
CIL
As already mentioned, CIL is low-level language that can be run on any computer with installed CLR.
Here is an example of C# code compiled into CIL:

As you can see, CIL is a statically typed language - add instruction is mathematical operation that expects two numbers on top of the stack and overload of method Console.WriteLine() expects integer.
In compile time of C# code compiler knows that variable x is always of type integer, then x + x is addition of two integers and that Console.WriteLine() is supposed to print a number. So it is easy for compiler to produce corresponding CIL code. But we can do this only because C# is statically typed language and we know all types already in compile time. But this differs in dynamic languages.
Dynamic languages
For dynamic languages are characteristic various features such as modifying types on runtime (creating new property or replacing method on object on runtime, etc). Another difference between static and dynamic language is usually dynamic type system, where compiler doesn’t know types in compile time and so it can’t so straightly generate CIL code as in the previous example. Let’s look at this JavaScript code:
if (Math.random() < 0.5)
var x = 5;
else
var x = "abc";
var res = x + x;
In this case compiler can’t emit CIL code for x + x because it doesn’t know in compile time whether x is a number and x + x is mathematical addition or whether x is a string and then x + x is a string concatenation. For mathematical addition and string concatenation there are different instructions in CIL.
So does it mean that it isn’t possible to run dynamic languages on .NET? No, there were already implementations of dynamic languages on top of the CLR before DLR such as Ruby.NET or Phalanger (PHP for .NET). There are ways how to implement mentioned dynamic features in CIL, in JavaScript example compiler could generate a method AddObjects(object, object) that on runtime decides whether to sum or concatenate given arguments:
Now compiler can transform x + x into:
call void [myProject]MyCompiler::AddObjects(object, object)
This is one possible solution how to deal with dynamic type system. There is just one hitch in this solution and that is performance. There are plenty of other data types such as double, float, decimal, etc and for all these data types would be need an if-else condition in the AddObject() method that would lead to a significant performance problem.
Dynamic Language Runtime
Microsoft developed dynamic language runtime - set of libraries that simplifies implementation of dynamic languages and their dynamic features on top of the CLR and what is more important, it contains mechanisms to improve performance of dynamic operations.
DLR is offered under the Microsoft Permissive License so you can download and study its source code. Microsoft released also two languages implemented on the DLR - IronPython and IronRuby. IronPython is also offered under the Microsoft Permissive License, you can download the source codes on Codeplex (www.codeplex.com/IronPython ^). The DLR lives in the IronPython repository which is the best place where to get the latest version of the DLR.
There isn’t official documentation of the DLR that tells you how to build a language on top of the DLR and source code of IronPython is quite large to study the implementation. Fortunately IronPython distribution includes a small sample language ToyScript (IronPython_Main/Src/ToyScript). It is really a simple language with basic functionality such as variable scoping, function definitions and function calls. ToyScript also demonstrates interaction with .NET libraries.
Compiler architecture
Compiler usually consists of three parts - scanner, parser and generator. Scanner takes code string and splits it into list of tokens. Parser takes list of tokens and builds an abstract syntax tree (AST). Generator takes AST and recursively generates assembler code (or CIL code in case of .NET) for each node.
.NET compiler:

What is abstract syntax tree (AST)?
AST represents a code in the tree structure where leafs are values, nodes are operations and the tree structure defines how code should be evaluated.
Let’s look at this simple code string “x = 3 * 2 - 4 / 2”, its AST is:

Tree is evaluated recursively so at first are evaluated 3 * 2 and 4 / 2 and results are subtracted. Finally result of subtraction is assigned to the variable x.
Conclusion
This chapter was supposed to introduce you into the common architecture of language compilers. In the next chapter will be described scanner and its implementation.

