Introduction to Dynamic Language Runtime
This is supposed to be short introduction to DLR - Dynamic Language Runtime. DLR is a new layer on static CLR used to implement dynamic languages on .NET. Simply said, DLR hides all the tough stuff you have to deal with, if you want to implement dynamic language on static CLR.
If you are interested in implementation of programming languages, especially dynamic languages, for .NET and you want to play with DLR, here are few important points:
Where to download DLR
DLR is open source project and you can find it’s source codes or binaries as a part of another open source project IronPython. IronPython is implementation of Phyton for .NET on DLR and you can find it on http://www.codeplex.com/IronPython [^].
* At the end of this article you can download VS2008 solution with sample code including DLR source codes.
Community - where to ask DLR specific questions?
There is no DLR specific forum today, but you can post your questions about DLR on http://www.ruby-forum.com/forum/34 [^], where is bunch of people interested in this topic.
Documentation
Best official knowledge source is source codes of IronPhyton and ToyScript. ToyScript is small sample language build on DLR and is included in IronPhyton distribution. There are also few good bloggers experimenting with DLR:
- http://blogs.msdn.com/mmaly/archive/tags/Dynamic+Language+Runtime/default.aspx [^]
Here is first article about building custom language on DLR - http://www.winterdom.com/weblog/CategoryView,category,DLR.aspx [^]
Code
First time I tried to play with DLR, I downloaded source codes of ToyScript language from IronPhyton project and tried to find how to work with AST (Abstract Syntax Tree) on DLR. There is a lot of classes like ScriptEngine, LanguageProvider, ConsoleHost and many others you need only if you really want to implement your own language. But if you just want to play with DLR and try to run some manualy created abstract syntax trees, then you don’t need all this stuff. In DLR source codes is hidden very useful static class Microsoft.Scripting.Ast.TreeCompiler used for direct compiling and running AST.
* At the end of this article you can download VS2008 solution with this sample code including required DLR source code.
Here is short piece of code you need to run AST on DLR (blue color highlights AST definition):
using System;
using Microsoft.Scripting;
using Microsoft.Scripting.Ast;
namespace DlrExecution
{
class Program
{
public delegate int Adder();
static void Main(string[] args)
{
// "Add" expression
Expression exp = Ast.Add(
Ast.Constant(2, typeof(int)),
Ast.Constant(3, typeof(int))
);
// Compile
Adder add_compiled = TreeCompiler.CompileExpression<Adder>(exp);
// Execute
int n = add_compiled();
// Print result
Console.WriteLine(n.ToString());
Console.ReadLine();
}
}
}
And the output is:

If you want to see generated AST, there is a command:
using System;
using Microsoft.Scripting.Ast;
using Microsoft.Scripting;
namespace DlrExecution
{
class Program
{
public delegate int Adder();
static void Main(string[] args)
{
ScriptDomainManager.Options.ShowASTs = true;
// "Add" expression
Expression exp = Ast.Add(
Ast.Constant(2, typeof(int)),
Ast.Constant(3, typeof(int))
);
// Compile
Adder add_compiled = TreeCompiler.CompileExpression<Adder>(exp);
// Execute
int n = add_compiled();
// Print result
Console.WriteLine(n.ToString());
Console.ReadLine();
}
}
}
And the output (I have no idea, why AST is printed twice in the output…):

Project template
Here is a Visual Studio 2008 solution including DLR source codes and sample running custom AST

