Thursday, May 14, 2009

New Sample: Writing a DLR Language in C# or IronPython

As well as being the basis for language implementations like IronRuby and IronPython the Dynamic Language Runtime is intended to be a framework making it easy for YOU to implement a dynamic language that runs on .NET and Mono.

So far there hasn't been a rush of languages using the DLR. The only ones I'm aware of are:
  • IronScheme (at 1.0 Beta 3a released May 2nd 2009)
  • Groovy DLR (at 1.0 Beta 4 released May 9th but may already be abandoned - see my previous blog entry)
  • Nua (Lua for the DLR) (no releases and last checkin July 2007 - so looks dead)
  • DLR Basic (no releases and last checkin November 2008)
  • IronSmalltalk (at version 0.1 with last checkin October 2008)
  • IronLogo (at version 0.0.1 with last checkin January 2008)
  • Managed JScript (Javascript) - developed by Microsoft and only ever released as Silverlight binaries. The future of Managed JScript is very unclear.
Python, Ruby, Javascript (Managed JScript), Scheme, Groovy, Lua, Smalltalk, Basic and Logo would be an impressive set of languages for the DLR; but of these only Python, Ruby and Scheme look as if they are under active development - and only IronScheme is a non-Microsoft project. However, various people have blogged about writing your own programming language with the DLR or using the DLR as an expression evaluator for custom DSLs so there is a lot of experimentation going on which may yet bear fruit.

Importantly there is a new kid on the block. The DLR has always shipped with a very small example language called Toyscript. This has only ever demonstrated a subset of what is involved in using the DLR. Bill Chiles (DLR program manager for Microsoft) has just announced on the IronPython mailing list a new example language called Sympl.

Not only does it include walkthrough documentation, but the language implementation is done in both C# and IronPython. Yep, it is possible to write DLR based languages in Python which is not something I expected.

Dino (core IronPython developer) says of Sympl:
One of the really cool things about Sympl that I think is worth emphasizing is the version that’s implemented in IronPython. Not only is it the 1st language I know of implemented in IronPython but it’s able to do cool stuff like create IDynamicMetaObjectProvider implementation from Python objects which are already dynamic objects! Now we just need to get someone interested in writing IpyIpy.
If you're interested in Sympl it is included in the DLR sources:
Here is the full announcement from Bill:
There’s a new DLR sample to which I wanted to draw folks’ attentions. There is a small language implementation sample, implemented in both IronPython and C#, with an accompanying walkthrough document. The code is now on the DLR Codeplex site under ...\Languages\Sympl, and the document is on the “Documents and Specs” page which you can get to from the list of links on the home page. Under the Sympl directory is also an examples directory with some code written in Sympl, which all runs when you execute Main in program.cs. The code and document will be in zip files we will produce to go along with the VS Beta1 release coming soon.

Sympl demonstrates how to implement a very simple language using the Dynamic Language Runtime (DLR) as it ships in .NET 4.0. The goal is to make you aware of how to get started building a language on the DLR. Sympl does not show production quality rich .NET interoperability and binding logic. It does walk you through the following:
  • Using DLR Expression Trees (which include LINQ Expression Trees v1) for code generation
  • Using DLR dynamic dispatch caching
  • Building runtime binders
  • Building dynamic objects and dynamic meta-objects
  • Supporting dynamic language/object interoperability
  • Very simple hosting with application supplied global objects
  • Basic arithmetic and comparison operators with fast DLR dispatch caching
  • Control flow
  • Name binding within Sympl
  • Method and top-level function invocation with fast DLR dispatch caching
  • Very simple .NET interoperability for runtime method and operation binding
  • Using the DLR’s built-in COM binding
  • Closures
  • Assignment with various left-hand-side expressions
Sympl does not yet demonstrate using any Codeplex-only DLR code such as the DefaultBinder, the faster and nicer .NET reflection trackers (Sympl’s is painfully slow and simple), or the common DLR hosting (yet).

A few caveats on the documentation. First, the walk through document still needs some polishing, but I wanted to get it out to people. Feel free to send me comments or ask questions. Second, the document refers to a couple of other documents on our Codeplex site, but we haven’t updated some of them since DEC 08. Those will be updated in the next couple of weeks. I think the sympl.doc walkthrough is very readable on its own and has all the relevant code snippets included in it as well. Lastly, while the example is overall reasonably simple, it does demonstrate all the basic concepts of building a language on the DLR with explanations, and therefore, is quite long. However, the document unfolds in the same way the Sympl language implementation evolved, so it progresses well for incrementally increasing your awareness of how to build a language on the DLR.

Hope you enjoy and find this code and doc helpful!

3 comments:

  1. The thing that would convince me to implement a programming language on the DLR is support for data structures. Currently you have to use the CLR reflection APIs to build your own classes at runtime. A basic facility that can build structured objects would be very helpful.

    I'm thinking of a facility that takes a set of fields names in and produces: 1) a function to create an instance of the data structure, 2) a predicate to test if something is an instance of the data structure and 3) accessor functions.

    ReplyDelete
  2. Have you looked at Xronos? http://bitbucket.org/stefanrusek/xronos/wiki/Home

    ReplyDelete
  3. Xronos looks interesting. No commits for five months which isn't encouraging.

    ReplyDelete

Note: only a member of this blog may post a comment.