Angel \”Java\” Lopez on Blog

May 19, 2015

AjErl Implementing Erlang in C# (2) Expressions

Filed under: .NET, AjErl, C Sharp, Erlang, Open Source Projects — ajlopez @ 9:58 am

Previous Post

Some time ago I write this project, an Erlang implementation as interpreter using C #. Let review today the implementation of expressions. An expression is evaluated in a context. For example, the variable X, is an expression that is evaluated in a context that tells how that variable is bound to a value or it is free.

All expressions are written using the workflow TDD (Test-Driven Development). After some expressions, this interface emerge in a refactor, and now all expressions implement it:

public interface IExpression
{
    object Evaluate(Context context, bool withvars = false);

    bool HasVariable();
}

The key method is Evaluate. It can allow or not that some variables are still unbound after the evaluate. There is a boolean method HasVariable that indicates the existence of variables in the expression. The simplest expression is a constant expression:

public class ConstantExpression : IExpression
{
    private object value;

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

    public object Value { get { return this.value; } }

    public object Evaluate(Context context, bool withvars = false)
    {
        return this.value;
    }

    public bool HasVariable()
    {
        return false;
    }
}

The Evaluate method does not use the provided context, it is only returning the original constant value, defined in the constructor. The expressions are grouped in a tree of expressions that is recursively evaluated.

An expression that evaluates a variable:

public class VariableExpression : IExpression
{
    private Variable variable;

    public VariableExpression(Variable variable)
    {
        this.variable = variable;
    }

    public Variable Variable { get { return this.variable; } }

    public object Evaluate(Context context, bool withvars = false)
    {
        if (!context.HasValue(this.variable.Name))
            if (!withvars)
                throw new Exception(string.Format("variable '{0}' is unbound", this.variable.Name));
            else
                return this.variable;

        return context.GetValue(this.variable.Name);
    }

    public bool HasVariable()
    {
        return true;
    }
}

This time, the context is used. If the variable is not bound to a value, it can return the unbound value, or raise an exception, depending on a boolean withValues. Some times, Erlang expects and needs that an expression be complete evaluated, having no unbound variables.

This code was implemented following the workflow of TDD. Some tests as examples (but it is only the final result, the important thing is the workflow, the dynamic of the programming activity). For a constant expression:

[TestMethod]
public void CreateSimpleConstantExpression()
{
    ConstantExpression expr = new ConstantExpression(10);

    Assert.AreEqual(10, expr.Value);
}

[TestMethod]
public void EvaluateSimpleConstantExpression()
{
    ConstantExpression expr = new ConstantExpression(10);

    Assert.AreEqual(10, expr.Evaluate(null));
}

For a variable expression:

[TestMethod]
public void CreateSimpleVariableExpression()
{
    Variable variable = new Variable("X");
    VariableExpression expression = new VariableExpression(variable);

    Assert.AreEqual(variable, expression.Variable);
}

[TestMethod]
public void EvaluateVariableExpression()
{
    Variable variable = new Variable("X");
    Context context = new Context();
    context.SetValue("X", 1);
    VariableExpression expression = new VariableExpression(variable);

    Assert.AreEqual(1, expression.Evaluate(context));
}

[TestMethod]
public void EvaluateUndefinedVariableExpression()
{
    Variable variable = new Variable("X");
    Context context = new Context();
    VariableExpression expression = new VariableExpression(variable);

    Assert.AreEqual(variable, expression.Evaluate(context, true));
}

[TestMethod]
public void RaiseIfEvaluateUndefinedVariableExpression()
{
    Variable variable = new Variable("X");
    Context context = new Context();
    VariableExpression expression = new VariableExpression(variable);

    try
    {
        expression.Evaluate(context, false);
        Assert.Fail();
    }
    catch (Exception ex)
    {
        Assert.AreEqual("variable 'X' is unbound", ex.Message);
    }
}

Upcoming topics: explore other expression implementations, data structure implementations for variables, maps, and more…

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

July 20, 2014

AjErl Implementing Erlang in C# (1) The Project

Filed under: .NET, AjErl, C Sharp, Erlang, Functional Programming, Open Source Projects — ajlopez @ 10:49 am

Next Post

Some time ago, to practice TDD and C # and explore ideas Erlang, I started an Erlang-like interpreter in C#:

https://github.com/ajlopez/AjErl

The solution structure:

It has the core class library, draft tests, project and I am putting together a REPL (Read Eval Print Loop) as console program. The interesting thing is that Erlang internally has lightweight processes, implemented in the virtual machine itself, which can communicate with each other by sending and receiving immutable messages. I do not intend to implement a virtual machine of this type, but have processes in threads. They will never be as fast as Erlang, but it’s a first “baby step” to explore, before trying to improve implementation.

Messages can be sent to a queue per process (thread here), and I have implemented the acceptance of messages, and turn his rejection process, as in native Erlang.

But today I wanted to briefly discuss how to implement transforms input text into Erlang expressions and forms:

:

The Lexer returns tokens, and these tokens have a value and a type. It can already recognize an atom (a name that begins in lower case), a variable, delimiters, operators, integers and real numbers, strings. The Lexer is consumed by a Parser, which has two public methods: one that parses expressions, used from the REPL, and another that parses what Erlang is called a form, would like a function definition in a module of Erlang. There are also ways to declare the name of the module, or other forms exported with name and number of arguments. In these modules will not accept expressions, only statements such form.

If you look at the history of commits, you will see that not everything went ahead, but grew slowly, as small raised me to implement use cases.

Upcoming topics: discuss the implementation of expressions, forms, processes and threads with mailbox for messages, and the REPL. And keep working on the implementation of messages and distributed processes.

Nos leemos!

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

April 15, 2014

End Of Iteration 2014w15

Previous Post
Next Post

Code Generation

I wrote new tasks, templates using my AjGenesis code generation tool. I create a simple:

https://github.com/ajlopez/AjGenesisNode-Django

to generate a Django web site, with admin page. The code generation creates the model, and admin feature allows you to edit the data, using Sqlite. It’s simple, but it is working. Next steps: create custom views, and a new project to generate Flask web site.

I added text area input fields to https://github.com/ajlopez/AjGenesisNode-Express the project that generates Express/Node.js web site from a model. Next steps: client-side validation, better server code organization, some initial tests.

I want to integrate these tools in a online app, so I started https://github.com/ajlopez/AjGenesisNodeServer to have a web site that generate codes from a model, defined at client side. You will select technology (Express, PHP, Sinatra, Django, Flask, …), the models (mainly, entities like: customer, supplier, department, employee, invoice… ), the database, the features (multiple pages, single page, angular? backbone? other?, REST API?, phonegap wrapper?), and voila. A zip will be generated with the generated solution. That is, code generation as a service. Maybe I could add an API to be externally consumed.

Python

I added a Flask web site example, work in progress, to my Python Samples:

https://github.com/ajlopez/PythonSamples/tree/master/Flask

I will use as a basis for my AjGenesis code generation version for Flask web sites.

Actor Model in C#

I refactored my project

https://github.com/ajlopez/Aktores

an Akka-like actor model implemented in C#. Now, I have a mailbox per actor, but the actor message process is executed consuming task from a queue, with n thread launched by the initial actor system. It was good to see such refactor working: all is in place, now. It is a proof that you don’t need to design everything before coding. If you follow TDD (Test-Driven Development) new ideas (or old ideas that have not implemented yet) can be added without much effort.

Erlang in C#

I started to add serialization of message to my project

https://github.com/ajlopez/AjErl

As usual, I started simple, using TDD: write input and out channel, using tests, red to green, refactoring. Instead of consuming an external library for serialization, I wrote a simple code, towards my first use case of distributed application. When the use case will be in place, I could start to think about other implementation path.

Google Code Jam

Past Saturday, I participated in Google Code Jam, Qualification Round. My code, usually written following TDD:

https://github.com/ajlopez/TddRocks/tree/master/Gcj2014

The Minesweeper problem is the tricky one. I should add the description of the problems, copying it from the original site.

Others

I added some code kata tests to my JavaScript samples https://github.com/ajlopez/JavaScriptSamples. I added metadata macro readind go my Clojure in C# https://github.com/ajlopez/ClojSharp. I started to add type checking in my node tree for Scala in C# https://github.com/ajlopez/ScalaSharp/commits/master. I added qualified name evaluation to my Rust interpreter in JavaScript https://github.com/ajlopez/RustScript. I worked on two non-public projects, too.

More fun is coming.

Keep tuned!

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

March 8, 2014

New Month’s Resolutions: March 2014

Filed under: AjErl, C Sharp, ClojSharp, Clojure, Erlang, Open Source Projects, Scala, ScalaSharp — ajlopez @ 7:27 pm

Review of my February’s Resolutions:

– Complete distributed messaging in AjErl [partial] see repo
– Complete dot notation in AjLisp [pending]
– Improve ClojSharp [complete] see repo
– Work on ScaScript [partial] see repo
– Work on ScalaSharp [complete] see repo
– Add variable scope to Mass [pending]
– Complete first version Aktores actor model in C# [pending]
– More code generation tasks, templates, models in AjGenesis for Node, generating Express, Meteor, Sinatra applications [partial] see repo

To compensate missing coding, I worked on:

– Create Annalisa [complete] see repo with online web services and demo
– Start SimpleAsync [complete] see repo
– Create my first Meteor samples [complete] see repo
– Start Templie, a simple template engine in Java [complete] see repo
– Start SimpleScraper, a simple scraper in JavaScript/Node.js [complete] see repo

And I made minor improvements on DylanSharp

New month’s resolution:

– Work on DictSharp
– Give talk about Node.js Distributed Applications
– Improve SimpleGammon
– Improve Annalisa
– Add @for to Templie
– Work on PreciosaAnnalisa online web services
– Improve my Node.js Distributes Applications samples
– Work on ScalaSharp
– Improve ClojSharp
– Improve SimpleAsync, do operation (functions in parallel)
– Improve Aktores
– Distributed messages in AjErl
– Add variable scope to Mass language
– Start code generation as a service

More fun is coming.

Keep tuned!

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

February 12, 2014

End Of Iteration 2014w06

Previous Post 
Next Post

More deliberate TDD practice in my open source projects

AjErl

My Erlang-like interpreter in C#:

https://github.com/ajlopez/AjErl

I added selective receive to process mailboxes. It was an interesting implementation, where each mailboxes has a save queue to keep the rejected messages. Once a message is accepted, the save queue is put again as a message queue, ready to be consumed again.

ScalaSharp

My Scala interpreter in C#:

https://github.com/ajlopez/ScalaSharp

I added binary expressions. I started to think about type check implementation. This next week I will add an explicit AST (Abstract Syntax Tree) to be used for type checking. Not all Scala expression can be type checked at parser time, because possible not-resolved cross references.

ScaScript

My Scala interpreter in JavaScript:

https://github.com/ajlopez/ScaScript

Minor additions: parse class with body, parse a suite of statement (a list of statement). One thing to review in this project and the previous one: Scala new line in lexer/parser. The language has special rules to skip or not a new line, depending on the context. I should add type checkting to this JavaScript project.

ClojSharp

My Clojure interpreter in C#:

https://github.com/ajlopez/ClojSharp

I applied a surgical refactor: internal reimplementation of top level context to save internally clojure vars. In this way, the top level definitions are saved not as name/value but as named var/value. The rest of the context are simple name/value dictionaries (like the context created by the special form let).

SimpleScraper

I created a new JavaScript/Node.js package:

https://github.com/ajlopez/SimpleScraper

a simple web scraper in JavaScript/Node.js. The initial commits only process HTML in text, iterating over tag items. Light weight, no dependencies needed yet.

Others

I added minor changes to:

https://github.com/ajlopez/AjSharp
https://github.com/ajlopez/AjScript
https://github.com/ajlopez/RubySharp
https://github.com/ajlopez/RuScript

I worked on three non-public projects, too.

More fun is coming.

Keep tuned!

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

February 7, 2014

New Month’s Resolutions: February 2014

Review of my January’s Resolutions:

– Start to implement Akka-like actor model in C# [complete] see repo
– Start to implement Scala interpreter in JavaScript [complete] see repo
– Work on AjErl [complete] see repo
– Work on Mass (I have many ideas to implement as module pattern and variable scope access) [pending]
– Work on DylanSharp [complete] see repo
– Start an implementation of ML (JavaScript? C#?) [pending]

I also worked on:

– Improving Scala interpreter in C# [complete] see repo
– Adding dot notation to Lisp interpreter in C# [complete] see repo
– Improving Ruby interpreter in JavaScript [complete] see repo
– Improving Clojure-like interpreter in C# [complete] see repo
– First templates, tasks generating Sinatra application in AjGenesis for Node [complete] see repo

My new month’s resolutions:

– Complete distributed messaging in AjErl
– Complete dot notation in AjLisp
– Improve ClojSharp
– Work on ScaScript
– Work on ScalaSharp
– Add variable scope to Mass
– Complete first version Aktores actor model in C#
– More code generation tasks, templates, models in AjGenesis for Node, generating Express, Meteor, Sinatra applications

More fun is coming

Keep tuned!

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

February 4, 2014

End Of Iteration 2014w05

Filed under: AjErl, C Sharp, Erlang, Iteration, JavaScript, NodeJs, Scala, ScalaSharp — ajlopez @ 2:45 pm

Previous Post
Next Post

As usual, I was working on my open source projects:

AjErl

Big advances:

https://github.com/ajlopez/AjErl

I implemented mailbox in processes, and now I have a send operator !, and a receive expression. I started to add map support (the new Erlang feature) and now I have export form in place.

Using messages, send and receive, I could communicate local processes.

Next steps: selective receive, distributed applications.

ScalaSharp

My project:

https://github.com/ajlopez/ScalaSharp

It is still an interpreter, work in progress, of Scala, written in C#. But this week I added first type info support. In general, in my interpreter, I didn’t write type checks. But in this case, the source language, Scala, is a typed one. So I added TypeInfo to def arguments, returns, and var and val commands. I should decide where to put the type checking: I could add some checkpoints at parser time, but some class def are not known at that time, so I should add a second pass visitor for pending type checking.

Anyway, TDD supports my work and my refactor/redesigns!

ScaScript

My Scala interpreter in JavaScript (not a compiler, yet)

https://github.com/ajlopez/ScaScript

Few additions, but now I have types for var, val, def. I’m applying “dog fooding” of my SimpleGrammar module.

The same for

https://github.com/ajlopez/RuScript

I was working on three non-public projects, involving C#, Excel plugin, web applications in ASP.NET MVC, and simple server with Node.js/JavaScript and geolocation trends using d3.js

More fun is comming.

Keep tuned!

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

January 28, 2014

End of Iteration 2014w04

Filed under: AjErl, C Sharp, Erlang, Iteration, JavaScript, NodeJs, Open Source Projects, Scala, ScalaSharp — ajlopez @ 3:18 pm

Previous Post
Next Post

I worked on my open source projects:

AjErl

My Erlang implementation as interpreter in C#:

https://github.com/ajlopez/AjErl

First mailbox tests, including delayed message; adding lists as predefined module; write and test lists:map, lists:filter, lists:any, lists:all, lists:sum.

Parsing fun, with one clause and multiple match clauses.

ScalaSharp

My Scala-like interpreter in C#

https://github.com/ajlopez/ScalaSharp

I added binary arithmetic functions, variable expression first implementation (with context to hold the values), val command, object command.

Others

Updates in:

https://github.com/ajlopez/DylanSharp Dylan as interpreter in C#
https://github.com/ajlopez/ClojSharp Clojure as interpreter in C#
https://github.com/ajlopez/RuScript Ruby as interpreter in JavaScript
https://github.com/ajlopez/ScaScript Scala as interpreter in JavaScript
https://github.com/ajlopez/AjLisp Lisp interpreter in JavaScript

I worked on three non-public projects.

More fun is coming.

Keep tuned!

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

January 21, 2014

End Of Iteration 2014w03

Filed under: AjErl, AjLisp, C Sharp, Iteration, JavaScript, NodeJs, Open Source Projects — ajlopez @ 6:25 pm

Previous Post
Next Post

Great week extending interpreters.

AjErl

My Erlang implementation as interpreter in C#

https://github.com/ajlopez/AjErl

I added multi-functions, that is, functions with the same name and arity, but with different argument matching conditions. And I started to write a minimal Process, to have threads running functions. Next steps: implement process mailboxes.

AjLisp

I was working on my C# implementation

https://github.com/ajlopez/AjLisp

I started to add Java Dot Notation, to access native types and objects. So, I grabbed some code from RubySharp/PythonSharp/AjSharp to access .NET types and objects, and wrapped functions to invoke properties, methods and constructors.

Aktores

More work on implementing Akka-like actor model in C#

https://github.com/ajlopez/Aktores

I should refactor mailbox implementation. After a bit of design, I decided to have a worker thread by actor, consuming a concurrent queue. ActorRef could add message to the queue.

RuScript, ScaScript and SimpleGrammar

I updated the version of SimpleGrammar

https://github.com/ajlopez/SimpleGrammar

to fix a bug detected consuming the library from RuScript:

https://github.com/ajlopez/RuScript

I’m using SimpleGrammar in

https://github.com/ajlopez/ScaScript

my Scala-like interpreter in JavaScript (I just implemented only a minimal parser).

Others

I was working in two non-public projects, and updating old PHP personal sites. I moved the code generated for Sinatra

https://github.com/ajlopez/AjGenesisNode-Sinatra

to use a modular instead of classic approach.

More fun is comming.

Keep tuned!

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

January 7, 2014

End of Iteration 2014w01

Previous Post
Next Post

First iteration of the year, more open source ideas, design, and TDD deliberate practice.

Code Generation, using AjGenesis

I updated

https://github.com/ajlopez/AjGenesisNode my AjGenesis tool for code generation
https://github.com/ajlopez/AjGenesisNode-Entity AjGenesis module to manage entity model
https://github.com/ajlopez/AjGenesisNode-Express AjGenesis module with tasks and templates to generate a simple web site, based on Node.js, Express, MongoDB

You can try the Quick Start of the Express module. Next steps:

– More property types
– Client-side validation
– Generate some basic tests
– Support of one-to-many entity relation

I could adapt the same templates to have another module that will generate PHP, MySQL web sites.

SimpleFlow

I published version 0.0.1 of:

https://github.com/ajlopez/SimpleFlow

I need it for chaining async calls in my tests and in my controlles in a non-public project. Use case to be described.

DylanSharp

I started to write a C# interpreter:

https://github.com/ajlopez/DylanSharp

trying to emulate Dylan language:

http://opendylan.org/

It’s a lisp-like programming language, with classes, and with a more Ruby/Python-like syntax, instead of parenthesis. As usual, I want to access native .NET types, classes and ecosystem, from a dynamic languages.

AjErl

I added first module support for my Erlang C# interpreter

https://github.com/ajlopez/AjErl

Others

I continue to adapt my old PHP sites https://github.com/ajlopez/ajlopezsite https://github.com/ajlopez/Todocontenidos to support latests PHP versions. I was working on three non-public projects, too.

More fun is coming.

Keep tuned!

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

Older Posts »

Blog at WordPress.com.