Angel \”Java\” Lopez on Blog

September 1, 2010

Writing an interpreter in .NET (Part 1)

Filed under: .NET, Programming Languages, Test-Driven Development — ajlopez @ 9:51 am

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

3

”foo”

3+5

fun(a)

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:

    [TestClass]
    public class ExpressionTests
    {
        [TestMethod]
        public void EvaluateIntegerConstantExpression()
        {
            IExpression expression = new ConstantExpression(1);
            Assert.AreEqual(1, expression.Evaluate());
        }
        [TestMethod]
        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

http://www.ajlopez.com

http://twitter.com/ajlopez

6 Comments »

  1. 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!!

    Comment by Brian Hall — September 1, 2010 @ 12:51 pm

  2. [...] Writing an Interpreter in .NET (Part 1) [...]

    Pingback by Writing an interpreter in .NET (Part 2) « Angel “Java” Lopez on Blog — September 2, 2010 @ 9:14 am

  3. [...] Writing an Interpreter in .NET (Part 1) Writing an Interpreter in .NET (Part 2) [...]

    Pingback by Writing an Interpreter in .NET (Part 3) « Angel “Java” Lopez on Blog — September 3, 2010 @ 10:29 am

  4. [...] Writing an Interpreter in .NET (Part 1) Escribiendo un intérprete en .NET (Parte 1) [...]

    Pingback by Escribiendo un intérprete en .NET (Parte 2) - Angel "Java" Lopez — September 16, 2010 @ 9:41 am

  5. [...] Writing an Interpreter in .NET (Part 1) Escribiendo un intérprete en .NET (Parte 1) Writing an Interpreter in .NET (Part 2) Escribiendo un intérprete en .NET (Parte 2) [...]

    Pingback by Escribiendo un intérprete en .NET (Parte 3) - Angel "Java" Lopez — September 17, 2010 @ 9:31 am

  6. [...] Previous Post First post of the series [...]

    Pingback by Writing an Interpreter in .NET (Part 9) « Angel “Java” Lopez on Blog — January 27, 2011 @ 11:21 am


RSS feed for comments on this post. TrackBack URI

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

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 68 other followers

%d bloggers like this: