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 😉
Possible future plans:
- 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.