Angel \”Java\” Lopez on Blog

September 2, 2010

Writing an interpreter in .NET (Part 2)

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

In my previous post:

Writing an Interpreter in .NET (Part 1)

I presented expressions and the implementation of ConstantExpression. Now, I want to implement a variable expression: an expression that, given the name of a variable, returns its associated value. (the code of this step can be downloaded from InterpreterStep02.zip ). And again, I want to implement it using Test-Driven Development. The code shows the END of this process, but I hope it serves as an example of such approach.

The code of this series is at my AjCodeKata Google Project, in the trunk, Interpreter folder. If you want more complex examples, check AjSharp, AjTalk, AjLisp.

The current solution:

I implemented a BindingEnvironment, a place to save names and associated values:

My tests:

    public class BindingEnvironmentTests
    {
        [TestMethod]
        public void SetAndGetValue()
        {
            BindingEnvironment environment = new BindingEnvironment();
            environment.SetValue("one", 1);
            Assert.AreEqual(1, environment.GetValue("one"));
        }
        [TestMethod]
        public void GetNullIfUndefinedName()
        {
            BindingEnvironment environment = new BindingEnvironment();
            Assert.IsNull(environment.GetValue("undefined"));
        }
    }

Note that I specified that obtaining the value of an undefined named variable returns null. If I had decided the raise of an exception, my test would reflect that decision.

Then, I changed IExpression.Evaluate to receive a BindingEnvironment:

    public interface IExpression
    {
        object Evaluate(BindingEnvironment environment);
    }

I wrote a new VariableExpression:

My tests:

        [TestMethod]
        public void EvaluateIntegerVariable()
        {
            BindingEnvironment environment = new BindingEnvironment();
            IExpression expression = new VariableExpression("one");
            environment.SetValue("one", 1);
            Assert.AreEqual(1, expression.Evaluate(environment));
        }
        [TestMethod]
        public void EvaluateUndefinedVariableAsNull()
        {
            BindingEnvironment environment = new BindingEnvironment();
            IExpression expression = new VariableExpression("undefined");
            Assert.IsNull(expression.Evaluate(environment));
        }

In the current solution, all tests are green:

But remember: using TDD I passed by red, green, refactor cycle.

Good code coverage:

Next steps: implements commands, and SetCommand: change the value of a variable, again using TDD.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

2 Comments »

  1. [...] 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

  2. [...] 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 [...]

    Pingback by Escribiendo un intérprete en .NET (Parte 3) - Angel "Java" Lopez — September 17, 2010 @ 9:32 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. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 65 other followers

%d bloggers like this: