AjTalk and Javascript (Part 1) Abstract Model

Next Post

Three weeks ago I wrote the post:

Smalltalk and Javascript

This year I write a lot of Javascript code, so I’m more confident using it. As I commented in the above post, I’m working to extend my open source Smalltalk-alike VM AjTalk (implemented using C#) to generate Javascript code (yes, me too! ;-). This first post is dedicated to describe the new abstract model that resides inside AjTalk library project.

AjTalk parses Smalltalk-alike code to memory (methods as bytecodes) and executes it. But now, I have a new abstract Model in memory. This is the current Model folder:

The idea is to have in memory representations of classes, methods, expressions. Some interfaces:

There are many classes that implement IExpression: SetExpression, ConstantExpression, MessageExpression, ArrayExpression, etc. The most interesting is MessageExpression: it represents code that has a receiver, message selector and other expressions as parameters:

a + b. “binary messages”
anObject doSomething. “unary messages”
anObject do: aParameter with: anotherParameter. “keyword messages”

The ModelParser class is in charge of reading an stream of chars and converts it to an abstract model in memory:

Actually, I could parse an file out of Object class from Squeak. My idea is to process all the Squeak kernel classes (I’m a bit lazy to implement them 😉

The main classes of the obtained model:

CodeModel has elements: each chunk parsed to memory is added to the code model in process. If a chunk represents the definition of a new class, a ClassModel object is created. For each method defined, a MethodModel is created.

Now, I can manipulate this abstract representation in memory. I want to experiment using this model in an interpreter pattern: visit expressions evaluating them, instead of generation of bytecodes. But now, I can use a visitor pattern to traverse the model in memory and translate it to Javascript code:

These are the involved classes:

Notice that I could implement other visitors: maybe to generate other code (Ruby? server side Javascript? Dart? 😉 or, I could execute the parsed code using an Interpreter pattern (i.e., each IExpression could have an Evaluate method).

AjTalk.Compiler is a console application that takes a file as parameter (i.e Object.st) and writes a Program.js in Javascript. The generated code (Work in progress):

You know: I’m addict to Twitter, part of my TweetDeck was captured at right bottom corner ;-). The above code is an excerpt of Program.js generated from Squeak Object.st. I’m using Node.js to load it (no run yet), without errors. I should implement some Squeak/Smalltalk primitives (notice the commented primitive invocation in the above code). The strategies I’m using to translate Smalltalk construct to Javascript could change in the near future. I’m experimenting with different approaches.

Next topics: how to implements Smalltalk ideas in Javascript.

Keep tuned!

Angel “Java” Lopez

4 thoughts on “AjTalk and Javascript (Part 1) Abstract Model

  1. Pingback: Node.js: Links, news, Resources (3) « Angel “Java” Lopez on Blog

  2. Pingback: Smalltalks 2011 Argentina: Your Conference « Angel “Java” Lopez on Blog

  3. Pingback: Javascript: Links, News and Resources (3) « Angel “Java” Lopez on Blog

  4. Pingback: AjTalk And Javascript (Part 2) Compiling a Simple Class « Angel “Java” Lopez on Blog

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