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

 

August 19, 2008

AjBasic: an open source Basic-like interpreter

Filed under: AjBasic, AjGenesis, Programming Languages — ajlopez @ 12:05 am

My code generation project AjGenesis uses a interpreted language to execute tasks and expand templates. The language was named AjBasic. It’s used in all the examples I wrote with AjGenesis (more info at AjGenesis posts). Years ago, I had separated the interpreter code from the mother project. This weekend, I was refactoring the code. The result is now published at:

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

In my opinion, Google Code is easy to manage. Contrary to SourceForge and CodePlex, they are using Subversion as code repository. I use the Tortoise SVN client to checking the changes. Google people are gentle: I can create many projects, until now:

http://code.google.com/p/ajlisp (it’s in good form, I guess)
http://code.google.com/p/ajtalk (it’s still in its infancy)

More info in previous posts:

AjLisp- a Lisp interpreter in .NET
AjTalk- a Smalltalk-like interpreter

The Solution

The solution AjBasicWithTests is composed by the projects:

AjBasic: It implements the compiler and tokenizer. The tokenizer separates the incoming text in tokens. The compiler makes abstract trees from such input.

AjInterpreter: A new project, not present in AjGenesis. It contains all the support for the abstract tree, its nodes, the environment for values, and an evaluator of programs. The management of .NET objects, and dynamic objects, are in this project, too. My intention is to continue to use this project to support other interpreted languages.

AjBasic.Console: A simple console to test the interpreter.

AjBasic.GUI: A simple (and ugly) WinForm to test the interpreter.

AjBasic.Test: Some test fixtures, using NUnit (there is another solution AjBasic that not includes the tests)

All the projects are written using Visual Basic .NET. The solution was compiled using Visual Studio 2005. The console and GUI project could be used as a base that explains how to use the interpreter from your own application.

There are many points to discuss about the implementation: they would deserve future posts. By now, let’s explore some of the main features of the project, at its current status. Then, I’ll write down some ideas to explore in future versions.

As I mentioned, the language was born to support code generation inside AjGenesis. I designed it to manage native .NET objects, as dynamic object. But, what is a dynamic object? Its an object that has no class, but that can be expanded with new properties (the nearest example I could think now, are the Javascript objects).

So, you can write code like this:

a.FirstName = Adam a.LastName = Doe a.Age = 800

You don’t have to declare the variable. You can create 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 then it has a LastName not nothing elseend if

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 a nothing, what is evaluated as a false in a condition.

The Tests

The solution was not developed using TDD (Test-driven development). Instead, I wrote the tests after the code, only to be sure the core functionality were well implemented, according to my expectations. I didn’t use descriptive names, but they were useful when I refactored the full solution:

You can try the language from the console AjBasic.Console:

and from the WinForm AjBasic.GUI

None of the above is the “ultimate app”, but they work… 😉

Next Steps

There are many ideas flying near my remaining neuron, and I have to put order on them. Some points, for the record:

– Now the interpreter core is separated from compiler, I could write AjSharp, an interpreter with C# syntax. Then, it could be added to AjGenesis, to write templates in a language more palatable to die-hard C# programmers.

– Add support to dynamic classes and prototypes.

– Refactor the tests

– Write down a minimal manual, explaining the language and its features

– Extend the language to support delayed values and delayed evaluation

– The internal form of this interpreter could be rewritten in Java

– The option to write an AST related to DLR support doesn’t attract me, it could be a lot of work. But it could be interesting to learn more about .NET DLR.

– Use the language as the basis for programming distributed agents (an ambitious point, that deserves more evaluation).

As usual, I had fun written this software.

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

Create a free website or blog at WordPress.com.