Scheme from Scratch - Bootstrap v0.1 - Integers
Scheme from Scratch has begun. Our first objective is a Bootstrap Scheme interpreter implemented in C that can be used to compile a Scheme-to-Assembly compiler. Bootstrap Scheme is a quick and very dirty REPL interpreter.
Bootstrap Scheme is a C port of the the metacircular evaluator presented in SICP section 4.1. The SICP metacircular evaluator is able to cut many large corners because the implementation and implemented languages are both Scheme. There is no need to build a parser or garbage collector, for example. With a C port, all of these sorts of features need to be implemented from scratch eventually.
I’ll introduce the interpreter incrementally in the style of Ghuloum’s An Incremental Approach to Compiler Construction. This first step is only a language of integers with some of the general structure of the C implementation. The objective is to have an interactive REPL session like the following.
$ ./scheme Welcome to Bootstrap Scheme. Use ctrl-c to exit. > 123 123 > -123 -123 > 007 7 > ^C $
We need to implement a model layer of integer (a.k.a fixnum) objects allocated in the heap. There also needs to be a read layer to read an integer. The evalulate layer is particularly simple because integers are self evaluating. The results are finally displayed in the print layer. A little driver loop in
main directs the show.
The code is only about 100 lines long. For anyone familiar with very basic C and SICP should be quite straightforward. It is written to be readable above all else. I remember a time when I thought only super-humans could implement an interpreter but a naive interpreter like this one is really just a matter of putting one foot in front of the next.
There is no garbage collection for now and there won’t be for quite a few more steps. There is no pressing need for garbage collection because the programs being run are very small and so is the leaked memory. Also a precise garbage collector really gums up the implementation making it less readable. It is better to leave that until we are more comfortable with the interpreter implementation.
I’ve put the code on github. I’m new to git and github which adds a bit more flavor to the project. You can browse the code at the following address
and I created a branch specifically for this integers-only version
You can get the code with the following command
$ git clone git://github.com/petermichaux/bootstrap-scheme.git
You should be able to just run
make and then the above REPL session example should work.
If anyone happens to be following along I’d be glad to know.
Have something to write? Comment on this article.