Angel \”Java\” Lopez on Blog

July 12, 2014

Mass Programming Language (5) Variables Scope

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

Previos Post

I finally started a new implementation of variables and their scope in programming language Mass. My idea is:

– Have local variables

– No need to explicitly declare

– Access available "outside" if necessary

The basic idea is:

– Any variable assignment involves the local level

That is, if I assign

a = 3

in a function, this refers to a local variable in the function. No matter if there is a variable external to the function

See for example

https://github.com/ajlopez/Mass/blob/master/Src/Mass.Core.Tests/MachineFiles/Local.ms

# file variable
a = 1

define foo()
    a = 2 # local variable
end

foo()

The first variable a is a variable of the file in process (the module). The variable assigned in the foo function is local to that function, and it is different from the outside variable.

But if we do not assign the variable, and only consulted it, its value is returned. example:

https://github.com/ajlopez/Mass/blob/master/Src/Mass.Core.Tests/MachineFiles/Scope.ms

# file variable
a = 1

define foo()
   a # access file variable
end

foo()

In this case, the function foo returns the value to "outside", since this variable has not been assigned in the function when evaluating the return value.

How to edit an "external” variable, that is, a non-local variablel? Well, this is a topic for an upcoming post. The idea: to access the value of a non-local variable, it must be implied, without declaring anything extra. BUT modifying its value must should be explicit: we put something to explicitly declare "we are changing the value of a non-local variable".

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

June 20, 2014

End Of Iteration 2014w24

Filed under: .NET, C Sharp, Iteration, Mass, Open Source Projects, SharpBus, SharpGo, SharpMongo — ajlopez @ 6:27 pm

Previous Post
Next Post

SharpGo

My Go Programming Language interpreter in C#

https://github.com/ajlopez/SharpGo

I added typed nodes to Abstract Syntax Tree, and nodes that generate expressions instead of commands. The types will help me to determine if an expression is valid or not, according to Go type specifications.

Mass

The Mass programming language interpreter

https://github.com/ajlopez/Mass

I reimplemented binary arithmetic operations (and string concatenation) without using ObOps, but directly using compiled lambda expressions. Next steps: removing ObOps dependency from compare operations. I think that a compiled lambda is a better outcome for such expression.

SharpMongo

My in-process, in-memory implementation in C# of a document database with a MongoDB-like interface:

https://github.com/ajlopez/SharpMongo

I fixed source analysis issuses, and added .count method to collections and to collection objects in REPL.

SharpBus

I started

https://github.com/ajlopez/SharpBus

A simple message bus implemented in C#, inspired by Mule. Notably, the flow of message can be defined using lambdas (something missing in Mule Java up to version 7). Using TDD, using fluent interface, consuming lamdbas, is the simplest way I found to implement a message bus.

Others

I worked fixing scripts in https://github.com/ajlopez/ajlopezsite. My parser “generator” https://github.com/ajlopez/GrammGen was  converted to VisualStudio 2010I worked on four non-public projects. More fun is coming.

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

May 25, 2013

Mass Programming Language (4) Lexer and Parser

Filed under: .NET, C Sharp, Mass, Open Source Projects, Programming Languages — ajlopez @ 10:38 am

Previous Post
Next Post

In the Mass programming language implementation, I have an enumeration and a class:

A token represents a word of code to be processed. Lexer is in charge of separate the code in words/tokens. And Parse takes that token stream and returns expressions and commands:

Lexer constructor receives a string:

public Lexer(string text)
{
    this.text = text;
}

This string is processed to be separated into tokens. Notice the lexer distinguish between operators (like +) and separators (like parenthesis). It take into account the end of line as a token, too (in other programming language, like C, the end of line is simply a blank space). The main method of Lexer is NextToken that returns the next token in code text. In some situations, it is needed to save a consumend token, so there are methods like PushToken and its variants.

Internally, Parser manages a Lexer. You can get the next command calling ParseCommand method, and the next expression using ParseExpression. When the text is exhausted, those methods return null.

I should modify Lexer to consume a text stream, instead of a string, so it could process an input, like console.

I should think about unifying commands and expressions, a la Ruby where all is an expression.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

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

April 30, 2013

Mass Programming Language (2) First Expressions

Filed under: .NET, C Sharp, Mass, Open Source Projects, Programming Languages — ajlopez @ 9:31 am

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

April 25, 2013

Mass Programming Language (1) Inception

Filed under: .NET, C Sharp, Mass, Open Source Projects, Programming Languages — ajlopez @ 9:09 am

Next Post

Three weeks ago, I was working on the implementation of an interpreted language, written in C#. The new language is called Mass (dedicated to  @MArtinSaliaS):

https://github.com/ajlopez/Mass

The current solution has three projects: a class library, its tests, and a console program, mass.exe, that launches Mass programs

You can run a hello.ms:

mass hello.ms

The classic Hello world source code:

println("Hello, world")

An example with classes and objects

class Person
	define initialize(firstname, lastname)
		self.firstname = firstname
		self.lastname = lastname
	end
	
	define getName()
		return self.lastname + ", " + self.firstname
	end
end

adam = new Person("Adam", "TheFirst")

println(adam.getName())

An example with access to .NET types and objects:

dirinfo = new System.IO.DirectoryInfo(".")

for fileinfo in dirinfo.GetFiles()
	println(fileinfo.Name)
end

The idea is  to have a dynamic language that leverages an underlying language provided with a rich class library and ecosystem, like in AjSharp and other of my projects. Before Mass, I was working on:

– Implementing Python in C# (see PythonSharp)

– Implementing Ruby in C# (see RubySharp)

– AjSharp (see repo and post)

But this time I wanted to implement something with simple sintax and semantic. Indeed, I was playing with “simple” ideas for a compiler over Javascript, see SimpleScript (1) First Ideas.

Then, with Mass, I deliberately wanted to avoid:

– Multiple commands in the same line (I discarded ‘;’ like in Ruby)

– Syntax based in spaces and indentation (Python discarded)

– Function invocation using only the name; Mass impose the explicit use of parenthesis (Ruby discarded; Mass is like JavaScript)

– Base values and classes (integers, strings, lists, etc…) having a crowd of methods (like Ruby and Python). No, Mass prefers to expose and use the underlying language/class library.

Then, I wanted:

– Functional values, as first-class citizens, like in JavaScript. So, having to put explicit parenthesis to invoke a function allows me to use the name of the function as a functional value

– Dynamic objects: each object can be extended at any moment, with new instance variables, object functions, a la JavaScript

– Syntax based in lines: each command has its own line. No command separation

– Syntax based in keywords: the end of a command list is marked with ‘end’, no braces

– As far as possible, only one way to do something, instead of the many ways motto a la Perl

– Complete keywords, then ‘define’ instead of ‘def’

– Simple inheritance at classes. But Mass could be expressive without written classes, using native classes from .NET framework and other libraries. It could be used as an scripting language.

– Explicit setting of variables that are out of the local scope (a topic for next posts)

– Variable scope by file, like in the require of JavaScript/NodeJs/CommonJS

– Module by file, with a require that automatically searches in directories, a la NodeJs/CommonJs. Notably, Mass can consume node_modules folder, so Mass module can be published and installed using NPM!

– Package manager, using NPM. You can use package.json to declare the dependencies, and publish new modules at NPM (using ‘mass-‘ as the suggested namespace).

In upcoming posts, I will write more details about implementation, guiding design ideas, examples. But now, you can see the code and the test examples at public repo. And yes, all was written by baby steps, using TDD.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Create a free website or blog at WordPress.com.