Angel \”Java\” Lopez on Blog

May 5, 2013

Mass Programming Language (3) Commands

Filed under: .NET, C Sharp, Mass, Open Source Projects, Programming Languages — ajlopez @ 6:16 pm

Previous Post
Next Post

Today let’s review command implementation in Mass (see repo). In the class library project, I have a dedicated namespace and folder for commands:

There are commands for if, while, for, for each, etc… Every command implements the ICommand interface:

public interface ICommand
{
    object Execute(Context context);
}

See that is very similar to IExpression. But I wanted to keep a separation between commands and expressions, at least for this first implementation, in order to have a clear separation of basis concerns.

A typical example of command is WhileCommand, partial code:

public class WhileCommand : ICommand
{
    private static int hashcode = typeof(WhileCommand).GetHashCode();

    private IExpression condition;
    private ICommand command;

    public WhileCommand(IExpression condition, ICommand command)
    {
        this.condition = condition;
        this.command = command;
    }

    public object Execute(Context context)
    {
        for (object value = this.condition.Evaluate(context); 
            value != null && !false.Equals(value);
            value = this.condition.Evaluate(context))
        {
            this.command.Execute(context);
            if (context.HasContinue())
                context.ClearContinue();
            if (context.HasBreak())
            {
                context.ClearBreak();
                break;
            }
        }

        return null;
    }
}

In Mass, every null or false value is false. All other value is true. I should refactor the code to have a central method IsFalse to be invoked in the above While code and in other commands, like IfCommand.

Another sample of command is ForEachCommand, partial code:

public class ForEachCommand : ICommand
{
    private string name;
    private IExpression expression;
    private ICommand command;

    public ForEachCommand(string name, IExpression expression, ICommand command)
    {
        this.name = name;
        this.expression = expression;
        this.command = command;
    }

    public object Execute(Context context)
    {
        var values = (IEnumerable)this.expression.Evaluate(context);

        foreach (var value in values)
        {
            context.Set(this.name, value);
            this.command.Execute(context);
            if (context.HasContinue())
                context.ClearContinue();
            if (context.HasBreak())
            {
                context.ClearBreak();
                break;
            }
        }

        return null;
    }
}

See that for Mass every IEnumerable value can be used as the element provider for ForCommand. I added some methods to the current context to signal the present of a break or continue (in the current version, I added return treatment, too).

And, as usual, all this code was developed using TDD flow: you can read repo commit history.

Next posts: Lexer, Parser, Mass samples, Mass scripting, using Mass from our .NET programs.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

5 Comments »

  1. [...] Previous Post Next Post [...]

    Pingback by Mass Programming Language (2) First Expressions | Angel "Java" Lopez on Blog — May 5, 2013 @ 6:18 pm

  2. [...] Mass Programming Language (3) Commands [...]

    Pingback by Mass Programming Language (3) Commands | MVPs de LATAM — May 7, 2013 @ 3:24 pm

  3. […] Previous Post […]

    Pingback by Mass Programming Language (4) Lexer and Parser | Angel "Java" Lopez on Blog — May 25, 2013 @ 10:38 am

  4. […] Node.js version repo – Update ClojSharp [complete] repo – New posts about Mass [complete] post, post, post – New posts about Distributed Applications and Node.js [complete] […]

    Pingback by New Month’s Resolutions: June 2013 | Angel "Java" Lopez on Blog — June 2, 2013 @ 10:56 am

  5. […] version repo – Actualizar ClojSharp [completo] repo – Nuevos posts sobre Mass [completo] post, post, post – Nuevos posts sobre Aplicaciones Distribuidas y Node.js [completo] post – Nuevos post sobre […]

    Pingback by Resoluciones del Nuevo Mes: Junio 2013 - Angel "Java" Lopez — June 4, 2013 @ 9:51 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 66 other followers

%d bloggers like this: