Angel \”Java\” Lopez on Blog

September 25, 2008

AjSharp: a C Sharp-like interpreter, work in progress

Filed under: AjBasic, AjGenesis, AjSharp — ajlopez @ 12:12 pm

Last month I published a project in Google code:

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

It’s the AjBasic interpreter, written in VB.NET. The code is the evolution of previous work, for my code generation project AjGenesis. In the new code, the interpreter was separated in a new project, and the grammar for the AjBasic language and its compiler resides in another project.

That approach ables to write another grammar in compiler, using the same core interpreter. AjSharp is born: you can download from the same AjBasic code repository.

The solution

AjInterpreter project is the same as in AjBasic solution. So, both solution share the same core implementation of their primitives.

AjSharp project contains the compiler, that recognize AjSharp code, like:

printline Hello World;

There are some primitives verbs, taken from AjBasic, as print and printline. But the rests of the verbs are more C Sharp-like ones. My plan is to include these new versions of AjBasic and AjSharp into the next AjGenesis release, so template writters can choose which language to use.

As I mentioned in my previous post:

AjBasic- an open source Basic-like interpreter

AjBasic was born to support code generation inside AjGenesis. I designed it to manage native .NET objects, as dynamic object. Now, such feature is supported in AjSharp. A dynamic object is an object that has no class, but that can support new properties on the fly (the nearest example I could think now, are the Javascript objects).

You can write code in AjSharp, like this:

a.FirstName = “Adam“; 

a.LastName = “Doe“;

a.Age = 800;

You have to declare the variable. You creates the object and add the property FirstName in one command (this automatic creation of object is not supported in AjGenesis, yet).

You can test the existence of a property:

if (a.LastName)
    printline “a has last name”;
else
    printline “a hasn’t last name”;

If the variable a has no assigned value, or if it has no LastName property, the above condition is evaluated to false. You can test something like foo.bar.anything and no exception is raised. The result value would be null, that is evaluated as a false in a condition.

You can access .NET objects, as well. An example:

di = new System.IO.DirectoryInfo(“.”);

foreach (fi in di.GetFiles()) {
printline System.String.Format(“File {0}”,fi.Name);
}

The interpreter

The core of AjBasic and AjSharp, is the AjInterpreter project, partial view:

It has many nodes of an abstract evaluation tree. AjSharp and AjBasic compilers work analyzing input text, and converting it to a tree of AjInterpreter objects. There are two main kinds of such objects: Expressions and Commands. An expression node can be evaluated, returning an arbitrary object. A command node is executed. Both operations work on an environment, that holds the current variables and associated values.

The tests

The new compiler, scanner and other objects, were written with the tests. Currently, the tests are green:

Console and GUI

AjSharp.Console is a little program to test AjSharp code. You can run .ajs file from this program if you provide the file names as arguments:

AjSharp.GUI is an ugly program, only to show the invocation of the interpreter from a WinForm program:

Work in progress

The current code has new features, like the try/catch/finally support in both language.

All this work is still work in progress. Next steps:

- Integrate AjBasic and AjSharp into AjGenesis next version

- Support for System.Array in both interpreter

- Add for command in AjBasic (now, there is a C Sharp-like for in AjSharp)

- Increment, decrement operands in AjSharp

- And thousands more… ;-)

There are two ways in the future: translate these work to Java, or/and integrate it with the new Dynamic Language support in .NET. But for me, AjGenesis integration is the priority.

It’s a funny job for me!

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

 

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

Follow

Get every new post delivered to your Inbox.

Join 68 other followers