Martin Karlsson / prog.re

Notes to self


Make A Lisp (step 1)

Continued from step 0. This step has you expand the stub READ and PRINT from the previous version, and is supposedly the hardest part of this whole project. The READ should take a string of lisp code and turn it into an integer, string, symbol or a list, array or hash map that in turn could hold other integers, lists etc... The first step of this is to split the string into a list of relevant parts (still strings). This is called tokenization. The guide provides a regular expression to help with this but I opted not to use it. Reason number one for this is that I couldn't get the regex to work right away (it needs escaping to be included as a string literal in php). But I also wanted to keep track of where in the string the tokens came from to maybe provide some nice error messages later on.

So instead I just read the string character by character and collect tokens as I go along. Next we take the list of tokens and turn them into a tree. The guide has a nice suggestion of a structure with a statefull reader for this and I found that it works well.

After this comes the print. This is just the tree creation in reverse. After the list type and and atom type was implemented into the reader and the corresponding print parts were done I had enough to run the test suite. About half the test passed on the first go for me. After this the programming turns into a puzzle game with the goal of getting all the tests to pass. Pretty straightforward.


Get in touch: martin [at] prog.re