in Notes

Note 3. CoffeeScript. Scheme on Coffee.

After the recent ECMAScript article on lexical environments, I wanted to provide some practical implementation of the general scope theory and variables look-up in the environment.

A good candidate for that became a toy Scheme interpreter written in the new and excellent language CoffeeScript. Also, beside the environment features, this task is quite interesting to play with interpreters themselves and their theory.

The interpreter is actually just a port of The Metacircular Evaluator — an interpreter of Scheme written in Scheme itself, which is described in the chapter 4.1 of the highly recommended “book of the Knowledge” — SICP (Structure and Interpretation of Computer Programs). All the comments in the eval.coffee source file are not my comments, but the text of the mentioned chapter 4.1.

Currently the version 0.1 of the interpreter is really just a toy. It still has some bugs, the system implements only about 15-20% of the real Lisp functionality. Also, it’s not a register-based virtual machine, but just an inefficient runtime interpreter without even optimized pre-parsing. However, this means we have a big further fields of application and plans 😉

The program doesn’t use all the power of the JavaScript/CoffeeScript constructions and objects (for example, to handle an environment and its frames as inherited JS objects), but instead imitates Lisp’s lists. For now it’s done specially — just for the academic curiosity and teaching practical examples — to show how e.g. the variable look-up is made in the environment frames (and by the way, exactly this model of environments is used in the ECMAScript itself, though, of course in the more optimized way).

Source code can be found on the Github repository — Scheme on Coffee.
Test page with the interpreter is also available online.

Possible future plans:

  • optimize the interpreter using all the power of JavaScript semantics;
  • implement register-based virtual machine (that one from chapter 5 of the SICP book);
  • probably to write some additional sub-articles on implementing the interpreter, explaining the semantics.

Dmitry.

Write a Comment

Comment