Angel \”Java\” Lopez on Blog

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

4 Comments »

  1. [...] AjBasic- an open source Basic-like interpreter [...]

    Pingback by AjSharp: a C Sharp-like interpreter, work in progress « Angel “Java” Lopez on Blog — September 25, 2008 @ 12:13 pm

  2. [...] AjBasic- un intérprete Basic de código abiertoAjBasic- an open source Basic-like interpreter [...]

    Pingback by AjSharp: un intérprete a la C Sharp, trabajo en progreso - Angel "Java" Lopez — September 26, 2008 @ 11:57 am

  3. [...] AjBasic- un intérprete Basic de código abiertoAjBasic- an open source Basic-like interpreter [...]

    Pingback by AjSharp: un intérprete a la C Sharp, trabajo en progreso | Buanzolandia — September 27, 2008 @ 12:01 am

  4. [...] AjGenesis Code Generation with AjGenesis in a Real Project Models for Code Generation in AjGenesis AjBasic: An Open Source Basic-alike Interpreter Generating Code Hello World with [...]

    Pingback by AjGenesis: Basis of its Implementation « Angel “Java” Lopez on Blog — November 18, 2011 @ 12:08 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 56 other followers

%d bloggers like this: