Angel \”Java\” Lopez on Blog

July 16, 2014

RubySharp, implementing Ruby in C# (3)

Filed under: .NET, C Sharp, Open Source Projects, Programming Languages, Ruby, RubySharp — ajlopez @ 4:43 pm

Previous Post

In RubySharp, we can define new functions (methods of the current object), and invoke them. There are some built-in functions in C#:

Every function should implement the interface:

public interface IFunction
{
    object Apply(DynamicObject self, Context context, IList<object> values);
}

On apply, each function receives the object (self), the context for variables (locals, arguments, closure….), and a list of already evaluated arguments.

An example, the implemention of puts:

public class PutsFunction : IFunction
{
    private TextWriter writer;

    public PutsFunction(TextWriter writer)
    {
        this.writer = writer;
    }

    public object Apply(DynamicObject self, Context context, IList<object> values)
    {
        foreach (var value in values)
            this.writer.WriteLine(value);

        return null;
    }
}

It receives the object to which it is a method, the context, and a list of arguments. Each of the arguments was evaluated. The implementation simply sends the arguments to a TextWriter, one argument per line. The TextWriter is provided when the function object is created (in the Machine object, that represents the current running environment). This injected facilites the test of the function, example:

[TestMethod]
public void PutsTwoIntegers()
{
    StringWriter writer = new StringWriter();
    PutsFunction function = new PutsFunction(writer);

    Assert.IsNull(function.Apply(null, null, new object[] { 123, 456 }));

    Assert.AreEqual("123\r\n456\r\n", writer.ToString());
}

The above code was born using the TDD workflow.

Let’s see another built-in function, require:

public class RequireFunction : IFunction
{
    private Machine machine;

    public RequireFunction(Machine machine)
    {
        this.machine = machine;
    }

    public object Apply(DynamicObject self, Context context, IList<object> values)
    {
        string filename = (string)values[0];
        return this.machine.RequireFile(filename);
    }
}

This time, the job of loading a file is delegated to the Machin object, injected in the constructor.

And finally, let’s review the code of a defined function (defined in RubySharp):

public class DefinedFunction : IFunction
{
    private IExpression body;
    private IList<string> parameters;
    private Context context;

    public DefinedFunction(IExpression body, IList<string> parameters, Context context)
    {
        this.body = body;
        this.context = context;
        this.parameters = parameters;
    }

    public object Apply(DynamicObject self, Context context, IList<object> values)
    {
        Context newcontext = new Context(self, this.context);

        int k = 0;
        int cv = values.Count;

        foreach (var parameter in this.parameters) 
        {
            newcontext.SetLocalValue(parameter, values[k]);
            k++;
        }

        return this.body.Evaluate(newcontext);
    }
}

In this case, a new contexts is built, containing the new arguments to the defined function, but with the original closure, injected in the constructor. The context provided in the Apply is not used.

Next topics: some command implementations, the Machine object, the REPL, etc…

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

April 3, 2014

New Month’s Resolutions: April 2014

Review of my March’s resolutions:

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

Additionally, I worked on:

– Complexo, simple complex numbers operations in JavaScript [complete] see repo
– Started RustScript, a Rust programming language interpreter in JavaScript [complete] see repo
– Continue RuScript, a Ruby interpreter in JavaScript [complete] see repo
– Continue RubySharp, a Ruby interpreter in C# [complete] see repo
– Started AjLispScala, a Lisp interpreter in Scala, using TDD and SBT [complete] see repo
– Recorded a second video Node.js Spanish tutorial [complete] see post

For this new month, my resolutions are:

– Continue AjLispScala
– Continue AjGenesisNode-Express
– Continue AjGenesisNode-PHP
– Continue RuScript
– Continue RustScript
– Distributed Messages in AjErl
– Give a talk Intro to Node.js
– Publish a new Node.js video tutorial

More fun is coming.

Keep tuned!

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

April 1, 2014

End Of Iteration 2014w13

Previous Post
Next Post

AjGenesis Agents

I started to add agents support to AjGenesis for Node, Express tasks:

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

The idea of agents is: instead of having code to render or produce page, code, classes, the agents are queried to solve problems: render this field, render this input, create the validation code, etc. Each agent could accept or not the problem: if an agent specialized in Angular detects that Angular is not being used in the application, then it is not loaded. Work in progress.

Ruby in C#

I updated:

https://github.com/ajlopez/RubySharp

My Ruby interpreter in C#. I added local variables detection. Ruby is a language where the local variables used in an scope can be listed. I wrote code to retrieve the name of local variables from expressions.

Clojure in C#

I worked on:

https://github.com/ajlopez/ClojSharp

There are a few new primitived forms and special form, and more parser/lexer support.

Scala in C#

I started to add dynamic object support to

https://github.com/ajlopez/ScalaSharp

The idea is to have objects with defined methods. Maybe, I will re-implement the object without dynamic methods: these should be defined at compile time, instead of runtime. I’m thinking about the definition of classes for many objects, and singleton classes for only one object.

Rust in JavaScript

I updated my Rust interpreter in JavaScript:

https://github.com/ajlopez/RustScript

with the support of modules, and public functions exposed in modules. I should add the references to them, using double colon operator.

AjLisp in Scala

To practice TDD and the use of SBT (Simple Build Tool), I started:

https://github.com/ajlopez/AjLispScala

Where I’m reimplemented AjLisp ideas in Scala, using scalatest library for TDD

JavaScript Samples

To explore WebGL, canvas, and implement some code katas, I created:

https://github.com/ajlopez/JavaScriptSamples

WebGL is not easy: lot of details to setup an scene. Canvas examples were borrowed from other of my projects. To practice TDD, I started to wrote simple code katas, using SimpleUnit and Node.js

I was working on two non-public projects, too. More fun is coming.

Keep tuned!

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

March 25, 2014

End Of Iteration 2014w12

Filed under: AjGenesis, Code Generation, JavaScript, NodeJs, Ruby, RubySharp, RuScript — ajlopez @ 10:41 am

Previous Post
Next Post

Ruby Interpreters

I updated my projects

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

The first in a Ruby interpreter in C#. I started to add the support to detect the local variables in an scope. In Ruby, there is the function local_variables() that returns a list of the names of local declared variables (not only the already initialized).

The second project is the seed to my next talk: it implements Ruby in JavaScript, as an interpreter. I added more support for modules and classes, and function declaration with and without arguments and parenthesis. I’m using my grammar/parser generation:

https://github.com/ajlopez/SimpleGrammar

It was updated to support new use cases (dog fooding!).

RustScript

I added some simple features to

https://github.com/ajlopez/RustScript

My Rust Programming Language interpreter in JavaScript. Not, it has function definition with arguments, let and let mut (mutable), some new arithmetic, bitwise and logical operators, with precedence. And a powerful assert! to start writing test programs.

ScalaSharp

I updated

https://github.com/ajlopez/ScalaSharp

My Scala interpreter in C#. The additions were simple: more node support. A node is the product of a parser, and define a part of the program. It has a type to be checked. That is the main difference with expression: a node has a type that can be inferred from the rest of the program, or by explicit declaration. As a typed language, an Scala interpreter cannot directly evaluate expression. I should check the types of the nodes, and THEN, generate a correct expression tree.

Code generation

I created

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

A new global tasks to be used by AjGenesis and related tasks to modify the model using command line. I started the new global tasks

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

to generate code for PHP and Django. There are only the skeletons, but I want to generate code for these languages/frameworks using a free model. Meanwhile, you can check the simple pages generated from:

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

The next big step: having a code-as-a-service application, written in Node.js, using AjGenesisNode, and deployed on Heroku.

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

December 5, 2013

New Month’s Resolutions: December 2013

Filed under: C Sharp, JavaScript, NodeJs, Python, Ruby, RubySharp — ajlopez @ 5:00 pm

Review of my November’s resolutions:

This new month’s resolution:

– Start compiler reduced Python to C, using JavaScript [complete] repo
– Give a talk about Ruby in C# [complete] slides repo
– Start Ruby to JavaScript compiler [complete] repo
– Complete variable scope in Mass language [pending]
– Give a talk about compiling languages to JavaScript [complete] slides
– Write web framework for AjTalkJs (to be used in Node.js) (plain? MVC?) [pending]
– Improve NPM modules in AjTalkJs and AjTalk [partial] repo
– Improve unit test support in AjTalkjs and AjTalk [partial] repo
– Improve match and data structure in AjErl [pending]

My December’s resolutions:

– Give a one-day Node.js course
– Refactor and improve CobolScript
– Continue writing Ruby to JavaScript compiler
– Variable scope in Mass language
– More modules in AjTalkJs

More fun is coming ;-)

Keep tuned!

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

December 3, 2013

End Of Iteration 2013w48

Filed under: Iteration, JavaScript, NodeJs, Open Source Projects, Ruby, RubySharp — ajlopez @ 4:35 pm

Previous Post 
Next Post

Talk about Implementing Ruby

I gave a talk at RubyConf Argentina 2013, about implementing Ruby. Slides source at:

https://github.com/ajlopez/Talks/tree/master/ImplementingRuby

To be viewed online at:

http://ajlopez.github.io/Talks/ImplementingRuby/index.html

It mentioned my work on RubySharp

https://github.com/ajlopez/RubySharp

my Ruby interpreter written in C#. Now, it can access native types and objects, requiring .rb modules or native .dlls.

My Apps

I updated the repo of my dynamic apps application (Node.js)

https://github.com/ajlopez/MyApps

I should add MongoDB as first persistence implementation.

SimpleGrammar

I published 0.0.3 version:

https://github.com/ajlopez/SimpleGrammar/commits/master

The new feature: a “peek” function, to look at the next token, without removing it.

I added the new feature to https://github.com/ajlopez/RuScript grammar (my Ruby interpreter in JavaScript).

Other works:

– Update SimpleYaml https://github.com/ajlopez/SimpleYaml to support real numbers

– Test refactor of CobolScript https://github.com/ajlopez/CobolScript/commits/master

– Update ClojScript to Visual Studio 2010 https://github.com/ajlopez/ClojSharp/commits/master

And I was working on two non-public projects.

This new week will be dedicated to some JavaScript projects and ideas, and to give a one-day course of Node.js

Keep tuned!

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

November 26, 2013

End Of Iteration 2013w47

Filed under: Iteration, JavaScript, Open Source Projects, Ruby, RubySharp — ajlopez @ 2:16 pm

Previous Post
Next Post

Implementing Ruby

My main outcome is my work on implementing Ruby, using C#:

https://github.com/ajlopez/RubySharp

I implemented:

– Access to native types and objects
– Modules
– Classes
– Nested modules and classes
– Require of Ruby programs
– Require of native dlls
– Singleton class
– Examples (console, win forms, http)

I will present this work this week, at RubyConf Argentina 2013

http://rubyconfargentina.org/en/

Implementing Ruby in JavaScript

I started a new implementation, this time in JavaScript

https://github.com/ajlopez/RuScript

It is still work in progress. I’m using SimpleGrammar for the parser/lexer, but I should review this path. Ruby syntax has some quirks to tackle.

Other works:

– Refactor of CobolScript: https://github.com/ajlopez/CobolScript
– Work on three non-public projects

Keep tuned!

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

October 17, 2013

RubySharp, Implementing Ruby In C# (2)

Filed under: C Sharp, Open Source Projects, Programming Languages, Ruby, RubySharp — ajlopez @ 7:11 pm

Previous Post
Next Post

I’m working on my Ruby implementation as interpreter written in C#

https://github.com/ajlopez/RubySharp

Today I would like to show that I have implemented many expressions that can be evaluated:

I have implemented commands, too. Initially they were separated:

But now, expressions and commands implement the same interface, to compy with Ruby moto “every is a value”

Evaluate method receives a Context:

I should review if I want or not that Context object has reference to self and other fields. But I wrote all with TDD, so, I could refactor it at any moment.

A code sample, NameExpression .Evaluate method:

public object Evaluate(Context context)
{
    var result = context.GetValue(this.name);

    if (result is IFunction)
        return ((IFunction)result).Apply(null, emptyvalues);

    return result;
}

Notice that I had to take into account the case: the name argument refers to a function. In Ruby, you can call a function simply using the name, the parenthesis are not mandatory.

Next steps: internal refactor, more commands and expressions.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

October 14, 2013

RubySharp, implementing Ruby in C# (1)

Filed under: C Sharp, Open Source Projects, Programming Languages, Ruby, RubySharp — ajlopez @ 12:02 pm

Next Post

Every day I write code and push it to my GitHub account. The purpose is training: practice programming, programming languages and TDD (don’t forget TDD!).

I’m working in RubySharp in my spare time:

https://github.com/ajlopez/RubySharp

It is a Ruby interpreter written in C# (like PythonSharp, see posts). There are two solutions, with and without tests, so you can compile the small solucion witn Visual Studio Express. The solucion with tests:

I have commands and expressions. But I learned that in Ruby every is an expression, so now expressions and commands implements the same interface. The programming language Mass derived from ideas in RubySharp.

My tests (now are more tests):

To give evidence about “put my money where my mouth is”, you can check my commits at:

https://github.com/ajlopez/RubySharp/commits/master

Next posts: implementation details, examples, scripting over .NET.

Keep tuned!

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

Blog at WordPress.com.