Angel \”Java\” Lopez on Blog

May 12, 2010

Objects in AjIo (Part 1)

Filed under: .NET, AjIo, C Sharp, Open Source Projects, Programming Languages — ajlopez @ 1:50 pm

I was writing an Io-like interpreter, in C#. It’s named AjIo (what else?). I wrote about it in my previous post:

Working in AjIo: Io-like interpreter in C#

Now, I want to discuss the object design I adopted in this project. AjIo is not a true, full reimplementation of Io programming language. It’s my toy project. I want to rewrite Io (partially) so I can use .NET class library and native objects. I’m a fan of using existing VMs and class library (.NET, Java) to reimplement dynamic, functional and other kind of languages.

IObject interface is the mother of all objects in AjIo:

More detail in direct code:

namespace AjIo.Language
{
    public interface IObject
    {
        string TypeName { get; }
        IObject Self { get; }
        IObject Parent { get; }
        object Evaluate(object expression);
        void SetSlot(string name, object value);
        object GetSlot(string name);
        void UpdateSlot(string name, object value);
    }
}

The slot methods are used to implement dynamic properties in AjIo objects. UpdateSlot needs that the slot was previously defined, with a SetSlot. As I choose to support native objects, the value parameter is a .NET object.

TypeName property is used to print object. That is, evaluating:

Object clone

prints as

Object_14fd2b5

The “Object” prefix is the type name of the new object. Note that Object is an object in AjIo, not a class name. And it's the top object in the parent hierarchy.

Self property points to the current object. It could not be the current .NET object pointed by this C# keyword. It will be discussed in an upcoming post about local objects.

Original Io is a prototype based language. Each Io object has a Protos slot, a list of prototypes to which it belongs. Using YAGNI, I didn’t need a list of prototypes, yet. Only one in pointed by Parent property. There is a top object without parent, and is the prototype of all other AjIo objects.

Evaluate usually receives a message (message name plus arguments), but, again, native object processing implies that I use an object as expression parameter.

As this project is work in progress, the above interface is not carved in stone, and it could suffer changes in the near future. I’m using TDD: if in a case I need something more in this interface, I will add to it. But I guess it’s taking form. Only a major refactoring could affect it. The current source code is at:

http://code.google.com/p/ajcodekatas/

under the AjIo folder.

Next posts to write: description of the base class implementations of this interface.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

April 30, 2010

Working in AjIo: Io-Like interpreter in C#

Filed under: .NET, AjIo, C Sharp, Functional Programming, Open Source Projects — ajlopez @ 12:34 pm

Last year, I discovered Io programming language:

http://www.iolanguage.com/

overview

Io is a prototype-based programming language inspired by Smalltalk (all values are objects, all messages are dynamic), Self (prototype-based), NewtonScript (differential inheritance), Act1 (actors and futures for concurrency), LISP (code is a runtime inspectable/modifiable tree) and Lua (small, embeddable).

It has a simple syntax, and its features are based in functional languages, Smalltalk, Self and others. As usual, I want to write my own version, with a twist: support for native objects. That is, to implement an interpreter over a virtual machine plus class libraries. The current choices are Java or .Net.

Two weeks ago, @MartinSalias pointed me to Io, again. Then, past weekend, I started my own implementation, based only in the written specification. It will be not exactly the same language, and it is still work in progress.

The code is in my Google project AjCodeKata:

http://code.google.com/p/ajcodekatas/

in the trunk, under AjIo folder. There is a .NET solution, with three projects:

The class library is the core, the console program can be used to interact with the interpreter, and the test project has the test I wrote during the design/coding process.

Actually, I have written the lexer, the parser, and the main languages objects, using TDD approach, as usual. You can write:

Some days ago, I implemented the assignment operators (syntax suger over setSlot, newSlot, updateSlot):

but, now, you have native .NET object support:

Next steps:

- Write more comparison operators (I only write ==)
- Native numbers (now, only ints are parsed, but other can be managed, try 1 / 6, it returns a real)
- Precedence in arithmetic operators (now 2 + 2 * 3 returns 12, instead 8)
- for, foreach, range, and more (I only write if())
- block() (I have method())
- include()
- Begin to write some Io library objects

About last point: I should decide how to implements things like:

List clone

Possible approaches:

- List as a derived object (Object clone) with its own slots, wrapping a native list
- List clone returning native ArrayList, then ArrayList type has associated “slots”
- No List, and use directly the native objects

As in AjSharp, original Io has agents and coroutines. I want to add AjSharp features (agents, goroutines, channels, queue channels, futures) to AjIo. Original Io implements agents with virtual machine “threads”: that is, it’s not using operating system threads, the virtual machine executes the many agents code, switching from one code to another during running (I guess, I don’t see the implementation code in Io). I won’t take that approach, in my first attempts.

As usual, I had fun written this stuff!

Keep tuned!

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

Theme: Shocking Blue Green. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.

Join 67 other followers