Archive for April 7th, 2008

BBC Radio presents… Jet Morgan in…

Monday, April 7th, 2008

Yesterday I heard a trailer for next Saturday’s play on Radio 4:

14:30 — Journey into Space: Frozen in Time

A tribute to Charles Chilton’s iconic 1960s series Journey into Space. The crew of the spacecraft Ares awake from suspended animation in 2013 to find themselves unwittingly caught up in a seismic deception. At stake is not only their own lives but also the very future of the planet Earth.

I am so excited…

C# Metaprogramming

Monday, April 7th, 2008

I often have a number of little ’side projects’ on the go at any one moment. Mostly these projects are not designed to ever see the light of day; I mostly use them to teach me various technologies that I might find useful at some point. At the moment I am writing a little compiler for a domain specific language targeted at image processing. I’m doing this mostly to learn a) how to write a compiler and b) to learn a bit more about C#.

So, first things first: I know C# is a bit of an emotive issue for many people, what with its heritage and all. Personally I’m enjoying writing code with it. I’m having happy memories of Delphi along with finding there to be a pleasingly complete standard library (which is, admittedly, more a function of .NET than C#). Plus I’m developing under Mono so I hope I’m not offending the freedom lovers too much :).

The first part of writing a compiler is parsing your code into a parse tree and thence into an abstract syntax tree (AST). Now, being all for re-using technology, I thought I’d experiment with storing the AST in an internal XML document as opposed to a custom tree structure. This had two advantages I could see:

  1. I could trivially dump the AST out to disk to run unit tests or manually inspect it for bugs.
  2. I could (partially) verify the AST using an XML Schema document.

It was also, of course, fully buzzword compliant and allowed me to learn a bit more about XML processing in general.

Once the AST was generated from the parse tree, I wanted to run a pass over it to do type- and scope-checking, etc. To this end I made use of a new—to me, but not the world at large—concept: method attributes.

(more…)