Angel \”Java\” Lopez on Blog

October 13, 2011

AjTalk and Javascript (Part 1) Abstract Model

Filed under: AjTalk, JavaScript, Open Source Projects, Programming Languages, Smalltalk — ajlopez @ 9:24 am

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
http://www.ajlopez.com
http://twitter.com/ajlopez

4 Comments »

  1. […] working with Node.js in some of my projects (social game example, distributed AjFabriq, Smalltalk to Javascript running on Node), but I’m still far from being an expert. So, when I find some interesting links, new […]

    Pingback by Node.js: Links, news, Resources (3) « Angel “Java” Lopez on Blog — October 24, 2011 @ 9:30 am

  2. […] AjTalk And Javascript (Part 1) Abstract Model Smalltalk And Javascript AjTalk: Implementing An Smalltalk-Like Interpreter (Part 1) Object Structure […]

    Pingback by Smalltalks 2011 Argentina: Your Conference « Angel “Java” Lopez on Blog — October 27, 2011 @ 9:20 am


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

Blog at WordPress.com.

%d bloggers like this: