Wednesday, October 24, 2007

A quick way to implement an interpreter

Are you wandering how to create your own interpreted programming language?
I've been looking around for some time now, for material on the same topic.
Here are some results:

Less related by interesting:

My idea was in fact simpler:
    Let's call your language it L:
  1. take the grammar of L (or some draft anyway)

  2. convert it (manually) into a set of classes, following the interpreter design pattern.
    With these classes you will be able to build an AST for any program in L.

  3. Then, instead of implementing a simple evaluation method, use a visitor/strategy design pattern to rewrite the AST one step at the time.
    A computation will then be a sequence of rewriting steps, that terminates when you have consumed all the commands of the AST (or you cannot apply any more rules).

In this way you can build an interpreter of L without writing any parser (that can always be written later), leaving your implementation open for extensions and re-design until the very last moment.
Also building a GUI, to manipulate the AST, on top of this interpreter will be quite straightforward.

(I will provide an example ASAP)

No comments:

Post a Comment