Mass Programming Language (2) First Expressions

Previous Post
Next Post

Before using Mass language in samples (see repo), I want to visit some implementation points. First, a news: now there is a solution (at (en https://github.com/ajlopez/Mass/blob/master/Src/Mass.sln) that can be compiled with Visual Studio C# Express.

The Mass solution has a class library project. There is a namespace dedicated to expressions:

An expression implements IExpression:

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

A Context keeps a key/value dictionary, to save the current variables:

  
public void Set(string name, object value)
{
	this.values[name] = value;
}

public object Get(string name)
{
	if (this.values.ContainsKey(name))
		return this.values[name];

	if (this.parent != null)
		return this.parent.Get(name);

	return null;
}

(notice that it supports nested context: each context can have a parent).

A simple expression is ConstantExpression, that returns a constant, without using the received context. A constant is any .NET value/object:

  
public class ConstantExpression : IExpression
{
	// ...

	private object value;

	public ConstantExpression(object value)
	{
		this.value = value;
	}

	public object Evaluate(Context context)
	{
		return this.value;
	}

	// ...
}

Another expression is the one that evaluates a variable name in the current context:

  
public class NameExpression : IExpression
{
	// ...

	private string name;

	public NameExpression(string name)
	{
		this.name = name;
	}

	public object Evaluate(Context context)
	{
		return context.Get(this.name);
	}

	// ...
}

The context is important: it could be the context of the current function, of a closure, of current object, of current module, etc… The same name could refer defers differente associated variables, as in other programming languages.

As usual, all these classes (expressions, Context, …) were written using the flow of TDD. You can check the test project and the repo commits history were there is evidence of that flow.

Next posts: some additional expressions, commands, using Mass for scripting, using Mass from our .NET programs.

Kee tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

4 thoughts on “Mass Programming Language (2) First Expressions

  1. Pingback: Mass Programming Language (1) Inception | Angel "Java" Lopez on Blog

  2. Pingback: Mass Programming Language (3) Commands | Angel "Java" Lopez on Blog

  3. Pingback: Mass Programming Language (1) Inception | MVPs de LATAM

  4. Pingback: Mass Programming Language (2) First Expressions | MVPs de LATAM

Leave a comment