Angel \”Java\” Lopez on Blog

May 29, 2012

AjGenesis Code Generation in .NET, Javascript and Ruby, All the Gates are Open

Usually, I get up early in the morning. Today, I got up a bit earlier, with some ideas about how to implement a simple template engine for my personal project, AjGenesis in Ruby. Then, I put my hands at work, and 3 hours later I had a solution, built using TDD (I made frequent commits, so you can see the progress of development in the GitHub Log).

The result at my GitHub account:

https://github.com/ajlopez/AjGenesisRb

Initially, I planned to use one of the template engines already available in Ruby, like Haml. But I decided to write my own implementation. Past week, I wrote AjGenesis in Javascript/NodeJs, see:

https://github.com/ajlopez/AjGenesisNode

In that project, I separated the template engine in another package:

https://github.com/ajlopez/SimpleTpl

In this way, I designed the template format to be similar to the original templates in classic AjGenesis.

I reuse the Javascript template engine in another new project (code kata of the past weekend):

https://github.com/ajlopez/SimpleMvc

a simple MVC implementation over NodeJs/Express.

So, now you have three projects:

- Classical AjGenesis code generation, implemented in .NET (see examples folder)
- AjGenesis in Javascript/Node, with SimpleTpl template engine, with interpolated Javascript expressions, and embedded Javascript commands (see samples folder)
- AjGenesis in Ruby, with a similar internal template engine, with interpolated Ruby expressions, and embedded Ruby commands (see samples folder)

AjGenesis in Javascript was published to npm (node package manager) as beta.

AjGenesis in Ruby is still not a gem: work in progress.

REMEMBER: all these projects GENERATES the text artifacts YOU WANT to generate. There are not limited to a technology, platform or programming languages.

It’s really interesting to see how simple ideas can be reimplemented in dynamic languages. Next steps: more samples, ruby gem, node package, and web site implementing code generation as a service (ASP.NET implementation commented in my old post). (see AjGenesis: Basis of Its Implementation).

Pending implementation languages: Python, maybe Java.

(Original photo from: http://commons.wikimedia.org/wiki/File:Table_Rock_Dam_during_April_2008_White_River_Flood.jpg)

Keep tuned!

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

May 20, 2012

May 2, 2012

New Month’s Resolutions: May 2012

A new month begins, and it’s time to review the past month resolutions and to write down the new ones. Status:

- Complete Acquarella (comment, number detection, new styles, language extension..) partial see repo
- Add features to AjTalk in Javascript/NodeJs (class support, fileouts processing) complete see repo see online demo
- Add features to AjLogo in Javascript/NodeJs (canvas support) complete see repo see online demo
- Complete verb support in SetTuples pending
- Give a talk about Programming Languages (Javascript/NodeJs, Clojure, Erlang, Python, Ruby, Scala) complete see video 1, video 2 (Spanish)
- AjContab inmemory model pending

Additionaly, I did:

- Give a talk about Clojure complete
- Add features to DartSharp complete see repo

This month’s resolutions:

- AjContab in-memory model
- Add verbs in SetTuples
- Add features to Acquarella (extend by language, multi-line comments…)
- Add features to AjLogo in Javascript
- Add features to AjTalk in Javascript
- Add features to AjTalk .st to javascript compiler
- Give a talk about implementing programming languages in Javascript
- Start AjConsorSite coding (condo and real estate site)

A lot of things, but I will have fun ;-)

Keep tuned!

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

 

April 6, 2012

New Month’s Resolutions: April 2012

It’s time to write down my list of resolution for this month, April 2012. First, a review of my March ones:

- Templates in AjGenesis in Ruby pending
- Support for flow control in AjLang complete
- Support for native objects in AjLisp in Java pending
- First structures, simple matching, simple REPL in AjErl (erlang-like in C#) partial
- First web site pages in AjContab, simple ASP.NET MVC, with an in-memory domain pending
- Move AjPython to GitHub, and review internal implementation complete
- Move AjSudoku to GitHub, and review test, code coverage complete
- Start internal refactoring AjRools algorithm, towards Rete-like one pending
- Start form and invoice processing in AjComprobantes complete

Hmmm.. too many pending items ;-) Ok, to balance them, I had new items:

- Start Acquarella Syntax Highlither complete
- Start AjTalk in Javascript/NodeJs complete
- Start AjLogo in Javascript/NodeJs complete
- Start SetTuples set programming in C# complete

This month’s resolutions:

- Complete Acquarella (comment, number detection, new styles, language extension..)
- Add features to AjTalk in Javascript/NodeJs (class support, fileouts processing)
- Add features to AjLogo in Javascript/NodeJs (canvas support)
- Complete verb support in SetTuples
- Give a talk about Programming Languages (Javascript/NodeJs, Clojure, Erlang, Python, Ruby, Scala)|
- AjContab inmemory model

Keep tuned!

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

March 26, 2012

March 22, 2012

AjTalk in Javascript (1) First implementations

Some days ago, I moved my AjTalk project from Google Code to GitHub:

https://github.com/ajlopez/AjTalk

Last year, I added the compilation of Smalltalk fileouts to Javascript, into that project. Past Sunday, I created a new project at:

https://github.com/ajlopez/AjTalkJs

as code kata for another approach: implementing an AjTalk virtual machine, from scratch, in JavaScript. The idea is to compile Smalltalk code to methods in bytecodes, and to have an interpreter for those methods. The code is at´

https://github.com/ajlopez/AjTalkJs/blob/master/lib/ajtalk.js

In that implementation, I’m using a sendMessage method and a custom lookup for methods. Excerpt (lib/ajtalk.js):

BaseObject.prototype.sendMessage = function(selector, args)
{
    var method = this.lookup(selector);
    return method.apply(this, args);
};

function BaseClass(name, instvarnames, clsvarnames, supercls) {
    this.name = name;
    this.instvarnames = instvarnames;
    this.clsvarnames = clsvarnames;
    this.supercls = supercls;
    this.methods = {};
};

BaseClass.prototype.__proto__ = BaseObject.prototype;

BaseClass.prototype.defineMethod = function (selector, method)
{
    this.methods[selector] = method;
};

BaseClass.prototype.getInstanceSize = function() {
    var result = this.instvarnames.length;
    if (this.supercls)
        result += this.supercls.getInstanceSize();

    return result;
};

BaseClass.prototype.lookupInstanceMethod = function (selector)
{
    var result = this.methods[selector];
    if (result == null && this.supercls)
        return this.supercls.lookupInstanceMethod(selector);
    return result;
};

At Monday, I had new ideas for implementing the virtual maching. I could use the prototype features of Javascript. Then, I wrote some new code (lib/ajtalknew.js):

function createClass(name, superklass, instvarnames, clsvarnames)
{
    var protoklass = new Function();

    if (superklass)
    {
        // Chain class prototypes
        protoklass.prototype.__proto__ = superklass.proto;
    }
    else
    {
        // First class methods
        protoklass.prototype.basicNew = function()
        {
            var obj = new this.func;
            obj.klass = this;
            return obj;
        }

        protoklass.prototype.defineSubclass = function(name, instvarnames, clsvarnames)
        {
            return createClass(name, this, instvarnames, clsvarnames);
        }

        protoklass.prototype.defineMethod = function(name, method)
        {
            var mthname = name.replace(/:/g, '_');
            if (typeof method == "function")
                this.func.prototype[mthname] = method;
            else
                this.func.prototype[mthname] = method.toFunction();
        }

        protoklass.prototype.defineClassMethod = function(name, method)
        {
            var mthname = name.replace(/:/g, '_');
            if (typeof method == "function")
                this.proto[mthname] = method;
            else
                this.proto[mthname] = method.toFunction();
        }

        // TODO Quick hack. It should inherits from Object prototype
        protoklass.prototype.sendMessage = function(selector, args)
        {
            return this[selector].apply(this, args);
        }
    }

    var klass = new protoklass;

    // Function with prototype of this klass instances
    klass.func = new Function();
    klass.proto = protoklass.prototype;
    klass.name = name;
    klass.super = superklass;
    klass.instvarnames = instvarnames;
    klass.clsvarnames = clsvarnames;

    klass.func.prototype.klass = klass;

    if (superklass)
    {
        // Chaining instances prototypes
        klass.func.prototype.__proto__ = superklass.func.prototype;
    }
    else
    {
        // First instance methods
        klass.func.prototype.sendMessage = function(selector, args)
        {
            return this[selector].apply(this, args);
        }
    }

    Smalltalk[name] = klass;

    return klass;
}

createClass('Object');

Smalltalk.Object.defineClassMethod('compileMethod:', function(text)
    {
        var compiler = new Compiler();
        var method = compiler.compileMethod(text, this);
        this.defineMethod(method.name, method);
        return method;
    });

Smalltalk.Object.defineClassMethod('compileClassMethod:', function(text)
    {
        var compiler = new Compiler();
        var method = compiler.compileMethod(text, this);
        this.defineClassMethod(method.name, method);
        return method;
    });

Ok, it’s a bit tricky, but it works!

Notably, I wrote test using Node builtin assert module (I had no Internet connection, and had no NodeUnit in my machine). The code was nurtured using TDD and it was a good experience for me.

I have a javascript Compiler function/”class” that I can use to compile Smalltalk code to my bytecode version. Only a few bytecodes are implemented:

var ByteCodes = {
    GetValue: 0,
    GetArgument: 1,
    GetLocal: 2,
    GetInstanceVariable: 3,
    GetGlobalVariable: 4,
    GetSelf: 5,
    SetLocal: 10,
    SetInstanceVariable: 11,
    SetGlobalVariable: 12,
    Add: 20,
    Subtract: 21,
    Multiply: 22,
    Divide: 23,
    SendMessage: 40,
    Return: 50
};

Pending work: implement class variables, metaclasses, with TDD. Improve hmtl samples

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

March 13, 2012

Acquarella Syntax Highlighter

Filed under: .NET, Acquarella, C Sharp, Open Source Projects — ajlopez @ 2:15 pm

Past weekend, I was coding a simple and configurable syntax highlighter, in C#, named Acquarella. You can see the result at:

https://github.com/ajlopez/Acquarella

The current solution has a class library project, a console program and a test project:

The idea is to have a library you can reference and use from your projects, in case you need some syntax highlighting functionality. The library takes a text (an string) and, giving the source code language and style name, transform it to another text, using configuration files. There is a more detailes information at project README.md.

Dog fooding, the Token.cs class transformed by Acquarella:

namespace Acquarella.Lexers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class Token
    {
        private string text;
        private int start;
        private int length;
        private TokenType type;

        public Token(TokenType type, string text, int start, int length)
        {
            this.type = type;
            this.text = text;
            this.start = start;
            this.length = length;
        }

        public string Value
        {
            get
            {
                return this.text.Substring(this.start, this.length);
            }
        }

        public TokenType Type { get { return this.type; } }

        public int Start { get { return this.start; } }

        public int Length { get { return this.length; } }
    }
}

Nice :-)

Pending work: Recognize comments, more styles (maybe using css files), more language configurations, extending Lexer (maybe RubyLexer, CSharpLexer, etc.)

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

March 7, 2012

New Month’s Resolutions: March 2012

First, a review of my February resolutions:

- Implements first ADO.NET commands in AjBase (in memory database) complete
- Implements Id and read/write properties in AjCoRe (simple Content Repository) complete
- Start to write AjComprobantes, a simple PHP app (in Spanish) using AjFwkPhp complete
- Prepare a presentation about programming languages (to give at local Microsoft User Group on March) complete
- Give a presentation about AjLisp in Ruby at Ruby Buenos Aires Meetup complete
- Prepare a presentation about Clojure (I hope to give it at local Java group on March) partial
- Write a REPL in my AjLisp in Ruby complete
- More coding on AjContab (I should decide if I will do it on PHP or .NET version) pending
- Templates in AjGenesis in Ruby pending
- Add first object support in AjRools Expert complete
- Add method with parameters in AjLang complete
- Post about my work on AjRools complete
- New post about my work on AjLisp in Ruby pending
- Post about my work on AjBase complete
- Play with Clojure REPL complete
- Post about Understanding Node.js (first of a series) pending
- Post about Understanding Git (first of a series) pending

Lots of pending! and completes ;-) Key points: prepare the programming languages talk (Javascript on NodeJs, Scala, Clojure, Erlang, Python, Ruby) tooks a lot of time (+- 40hs). So I should review my complex talks time preparation. This month, March, will be dedicated to write code, no talk preparation, no post writing:

- Templates in AjGenesis in Ruby
- Support for flow control in AjLang
- Support for native objects in AjLisp in Java
- First structures, simple matching, simple REPL in AjErl (erlang-like in C#)
- First web site pages in AjContab, simple ASP.NET MVC, with an in-memory domain
- Move AjPython to GitHub, and review internal implementation
- Move AjSudoku to GitHub, and review test, code coverage
- Start internal refactoring AjRools algorithm, towards Rete-like one
- Start form and invoice processing in AjComprobantes

Keep tuned!

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

February 29, 2012

AjBase (1) Implementing an In-Memory Database

Filed under: .NET, AjBase, C Sharp, Open Source Projects — ajlopez @ 8:29 pm

In mid 2009, I started to write an in-memory database, as a coding exercise in C#. These days, I resumed work on that project. Now the code resides in my GitHub repository:

https://github.com/ajlopez/AjBase

Nice history image at Github:

The current solution:

As usual, it was developed using Test-Driven Development. All tests in green:

The key implemented elements are:

Engine: It has a list of created databases (in memory, there is no persistence).

Database: It is the container for tables.

Table: A table has rows, and a RowDefinition.

RowDefinition: it manages the list of columns in a table.

Row: it keeps the values of the table columns, in an object array.

I have a doubt: to keep the schema (RowDefinition) per table, or go for a schema-less (free columns) per row. Now, I will keep the schema approach.

This year, I started to parse SQL statements. SQL is not my preferred languages (too many quirks, etc…), but the project can parse the simplest commands:

I want to add an ADO.NET provider, see the Data namespace:

I have a simple ADO.NET DbConnection, with some very basic (incomplete) ExecuteQuery, ExecuteNonQuery support.

Next steps: complete ADO.NET provider, more SQL parse support, concurrency and transaction support (big challenges! :-) .

Next posts: examples for creating a database, creating a table, inserting data, selecting data, by code and by SQL statements. Meanwhile, you can explore the tests code, to see how to code those examples.

Keep tuned!

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

February 25, 2012

AjRools (1) Rule Engine in C#

Filed under: .NET, AjRools, Open Source Projects, Rule Engine — ajlopez @ 6:50 pm

Since last year, I was conducting a bit of research about rule engines, specially in Java:

http://delicious.com/ajlopez/ruleengine

I reviewed JBoss Drools Expert (see http://delicious.com/ajlopez/drools). Past December, I assisted to Buenos Aires JBoss Meetup (see Spanish post)

In the old eighties, I met Rete Algorithm (see The Rete Matching Algorithm, Dr.Dobb’s). After all that, I decide to implement my own rule engine, in C#. You can check my progress at:

https://github.com/ajlopez/AjRools

This is the current solution:

As usual, it was coded using TDD.

I have facts, that are asserted, like Temperature = 38. And rules, that based on the asserted facts, produce other asserted facts (I plan to add actions, too, beyond fact assertions). A rule example:

# Rules example
rules
# Rule for Child Fever
rule
	when
		Temperature > 38
		Age <= 12
	then
		HasFever is true
end
# Rule for Adult Fever
rule
	when
		Temperature > 37
		Age > 12
	then
		HasFever is true
end
end

I have ideas to extend the rule engine to support objects with properties, not only variables:

rule
when
	p is_a Patient
	p.Temperature is 39
then
	p.HasFever is true
end

but this feature is still in progress. I didn't adopt the Rete algorithm, yet, or something similar. I want to have a running implementation, with tests. Then, changing internal algorithm is like an exercise on refactoring.

Pending work: more object+properties support, session (client use of the internal implementation), etc.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Older Posts »

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.