Writing an interpreter in .NET (Part 1)

I’m preparing a talk about writing interpreters and compilers in .NET, for Code Camp Buenos Aires http://www.codecamp.com.ar

I wrote many simple interpreters (AjSharp, AjTalk, AjLisp….) and I’m continuing with that work. But now, I would like to write the usual steps I follow for a simple interpreter.

First, I use Test Driven Development. For each artifact I add to the code, I write a test, make it run in read, change the code to run the test in green, and then refactor. (the code for this post can be downloaded from InterpreterStep01.zip). The solution for this post:

In this first installment of a post series, the initial point is the concept of an expression.

namespace Interpreter
    public interface IExpression
        object Evaluate();

I wrote an interface, but I could write a concrete class, and then, extract the interface. In this simple interpreter, an expression is something to be evaluated, like





The first concrete class is ConstantExpression: new ConstantExpression(3) will be evaluated to 3. A class diagram:

ConstantExpression implementation:

    public class ConstantExpression : IExpression
        private object value;
        public ConstantExpression(object value)
            this.value = value;
        public object Evaluate()
            return this.value;

Simple tests:

    public class ExpressionTests
        public void EvaluateIntegerConstantExpression()
            IExpression expression = new ConstantExpression(1);
            Assert.AreEqual(1, expression.Evaluate());
        public void EvaluateStringConstantExpression()
            IExpression expression = new ConstantExpression("foo");
            Assert.AreEqual("foo", expression.Evaluate());

The test are in green:

Good code coverage:

Ok, it’s a very simple example. Next steps: add variables, its evaluation. In a third steps, I will add commands, and the set of a variable. I should write functions, lexer, parser, and more, all using TDD.

Keep tuned!

Angel “Java” Lopez



7 thoughts on “Writing an interpreter in .NET (Part 1)

  1. Brian Hall

    Good stuff. I’m currently learning about unit testing by reading “The Art of Unit Testing”, so this is exactly what I need right now. Thanks!!

  2. Pingback: Writing an interpreter in .NET (Part 2) « Angel “Java” Lopez on Blog

  3. Pingback: Writing an Interpreter in .NET (Part 3) « Angel “Java” Lopez on Blog

  4. Pingback: Escribiendo un intérprete en .NET (Parte 2) - Angel "Java" Lopez

  5. Pingback: Escribiendo un intérprete en .NET (Parte 3) - Angel "Java" Lopez

  6. Pingback: Writing an Interpreter in .NET (Part 9) « Angel “Java” Lopez on Blog

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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