Angel \”Java\” Lopez on Blog

September 13, 2011

AjScript: Javascript-Alike interpreter in C# (2) Expressions

Previous Post

As in other interpreters, one key piece is the Expression, something to evaluate during the execution of a program. In AjScript (repository) I have an IExpression:

public interface IExpression
{
    object Evaluate(IContext context);
}

These are the implemented expression in current solution:

A simple expression, ConstantExpression:

public class ConstantExpression : IExpression
{
    private object value;
    public ConstantExpression(object value)
    {
        this.value = value;
    }
    public object Value { get { return this.value; } }
    public object Evaluate(IContext context)
    {
        return this.value;
    }
}

A bit more interesting, VariableExpression, given a name, and a context, returns the value of a named variable:

public class VariableExpression : IExpression
{
    private string name;
    public VariableExpression(string name)
    {
        this.name = name;
    }
    public string Name { get { return this.name; } }
    public object Evaluate(IContext context)
    {
        return context.GetValue(this.name);
    }
}

Sometimes, an expression has one or two parameters. I have UnaryExpression, BinaryExpression as abstract class. The BinaryExpression code:

public abstract class BinaryExpression : IExpression
{
    private IExpression leftExpression;
    private IExpression rigthExpression;
    public BinaryExpression(IExpression left, IExpression right)
    {
        this.leftExpression = left;
        this.rigthExpression = right;
    }
    public IExpression LeftExpression { get { return this.leftExpression; } }
    public IExpression RightExpression { get { return this.rigthExpression; } }
    public abstract object Apply(object leftValue, object rightValue);
    public object Evaluate(IContext context)
    {
        object leftValue = this.leftExpression.Evaluate(context);
        object rightValue = this.rigthExpression.Evaluate(context);
        return this.Apply(leftValue, rightValue);
    }
}

Note that the Apply method is abstract. A concrete implementation:

using Microsoft.VisualBasic.CompilerServices;
public class ConcatenateExpression : BinaryExpression
{
    public ConcatenateExpression(IExpression left, IExpression right)
        : base(left, right)
    {
    }
    public override object Apply(object leftValue, object rightValue)
    {
        if (leftValue == null)
            leftValue = string.Empty;
        if (rightValue == null)
            rightValue = string.Empty;
        return Operators.ConcatenateObject(leftValue, rightValue);
    }
}

I'm a lazy programmer: note the use of Visual Basic Operators. I could write a custom implementation. I have all the tests in place to refactor this expression.

I didn't write all expressions at once. An example: I wrote Context class using tests, and then the VariableExpression class using tests. Then I added variable support in Parser, and evaluates simple expressions that use constants and variables. I use this pattern many times: add a new base class (Context, Function…), add a new expression, improve lexer/parser to support the new expression, use the expression in code tests, etc.

Upcoming topics: commands, function implementation, closures, hoisting, native objects. And AjScript examples.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

1 Comment »

  1. [...] by AjScript: Javascript-Alike interpreter in C# (2) Expressions « Angel “Java” Lopez … — September 13, 2011 @ 9:15 am [...]

    Pingback by AjScript: Javascript-Alike interpreter in C# (1) Base Interface « Angel “Java” Lopez on Blog — September 13, 2011 @ 9:24 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 57 other followers

%d bloggers like this: