Scheme from Scratch - Bootstrap v0.12 - Primitive Procedures Part 2
In the last version we implemented a single primitive procedure. In order for our bootstrap interpreter to be able to run a Scheme compiler written in Scheme, we will need several more primitive procedures. It it now a good time to bite off a reasonably meaty chunk and implement the majority of what is needed.
A few of the things you can execute in the REPL after today’s additions:
$ ./scheme Welcome to Bootstrap Scheme. Use ctrl-c to exit. > (boolean? #t) #t > (integer->char 99) #\c > (< 1 2 3) #t > (cons 'a 'b) (a . b)
To determine the following list of primitives to add, I trawled through R5RS looking for all the procedures that seemed like they would be required as primitive procedures. The following list also contains a few that could be implemented as library procedures later but I’m going with the list below. Perhaps some of the following won’t be used. Perhaps I’ve missed some and will have to sneak back to this article and append to the list.
working with integers:
working with pairs and lists:
polymorphic equality testing:
You will notice a distinct lack of argument checks on the arguments to the primitive procedures: the number of arguments and argument types. Bootstrap Scheme is not a production Scheme implementation. It is rickety. It is just enough to bootstrap a Scheme compiler. It has the fun parts of implementing because we are doing this in our spare time;-) If you type meaningful Scheme into the interpreter it should provide the correct output.
Another goal of Bootstrap Scheme is educational and to help readers over the hump of implementing a first Scheme in C with as little resistance as possible. Being bogged down by the requirements of production quality code is counter to that goal. With the error checking included in the code, the code might just bloat to big enough to scare people away.
All that said, it would be almost trivial to add bulky error checking through out the code. Anyone who understands the code could surely do it. If you would like error checking in your interpreter then I encourage you to add it.
If you are following along with my code you will see that I spent some time today refactoring. I touched code in all eleven previous branches. Maintaining eleven versions of code takes some time. Scheme comments are now considered whitespace, the
read function is a bit simpler, and the code is formatted a nicer in some places. I have a Bootstrap Scheme grand finale planned (hopefully only about a week away) and it requires comments.
Even with that teaser out there, I think tomorrow might be one of the most exciting days of the whole implementation. :-)
There is a v0.12 branch on github for this version.
Have something to write? Comment on this article.