Angel \”Java\” Lopez on Blog

March 19, 2011

Dynamic Language Runtime (Part 1) Hello World!

Filed under: .NET, C Sharp, Code Generation, Dynamic Language Runtime — ajlopez @ 11:22 am

Next post

I want to compile my interpreters (AjSharp, AjLisp, upcoming AjScript), written in C#. I could use CodeDome, Reflection.Emit, Cecil. But there is a library dedicated to implement dynamic languages: Dynamic Language Runtime. I got 1.0 version from

and I compiled its release version using VS2008. Notably, they removed a simple language implementation (I guess it is still at version 0.9, as ToyScript). So, I need help to understand the inner guts and features of this library. Documentation is technically good, but they introduce so many pieces with names like “Call Site” that for me (English is not my mother tongue) are ambiguous. What is a “Call Site”? Maybe a web site for phone calls 😉 ;-). So, I decided to write some code katas, short experiments, to investigate DLR. Hopefully, now I have the help of the book “Pro DLR in .NET 4” by Chaur Wu. I found this first example in that book.

First experiment: create a REPL (Read-Eval-Print Loop), for a simple “Hello, World!”.

(The code of this post series is at
under trunk/Dlr)

I created a console project, using Visual Studio 2008 (my interpreters are simple, and they don’t need any .NET 4.x feature, yet), named DlrHelloWorld.

I added the reference to Microsoft.Scripting.dll, Microsoft.Scripting.Code.dll, Microsoft.Dynamic.dll (curiously, one of the class I need for this example, ConsoleHost, in this dll).

The first class:

    public class HelloScriptCode : ScriptCode
        public HelloScriptCode(SourceUnit sourceUnit)
            : base(sourceUnit)
        public override object Run(Scope scope)
            Console.WriteLine("Hello, World");
            return null;

Objects of this class will run a simple “Hello, World”. Note that it receives a SourceUnit, something that has the source code to compile. But it will be ignored in this simple example.

Then, the class that manage the HelloScriptCode:

    public class HelloContext : LanguageContext
        public HelloContext(ScriptDomainManager domainManager,
            IDictionary<string, object> options)
            : base(domainManager)
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, 
            CompilerOptions options, ErrorSink errorSink)
            return new HelloScriptCode(sourceUnit);

Its main responsability: given a SourceUnit, produce an ScriptCode object.

DLR can host a language, providing a REPL for it. I wrote a class inheriting ConsoleHost:

    public class HelloConsole : ConsoleHost
        protected override Type Provider
                return typeof(HelloContext);

The simple main is:

    class Program
        static void Main(string[] args)
            (new HelloConsole()).Run(args);


The output could be boring 😉 :

I should study and understand the involved pieces in this example (host options, source unit, language context, script code). Next steps: research on compiling simple expressions and commands.

Keep tuned!

Angel “Java” Lopez


  1. […] Dynamic Language Runtime (Part 2) Hello Expressions! Filed under: .NET, C Sharp, Code Generation, Dynamic Language Runtime — ajlopez @ 10:20 am Next postPrevious post […]

    Pingback by Dynamic Language Runtime (Part 2) Hello Expressions! « Angel “Java” Lopez on Blog — April 13, 2011 @ 2:33 pm

  2. Thank you for the first really simple example of DLR.

    Comment by Ido Ran — December 2, 2011 @ 12:11 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: