I just came across this wonderful demonstration of writing a complete Lisp interpreter in Python! This is by none other than Peter Norvig. He is rightly proud of the fact that his system is 116 non-comment lines.

Obviously I too am excited by this topic. In 2005 I too did essentially the same thing. I did not have the benefit of a computer science PhD from Berkeley nor had I ever studied compiler design. I had to just think out what would be required and implement it. I think it’s pretty cool how close my independently conceived system is to Norvig’s cannonical design. I too was struck by the elegance and beauty of this system.

His demonstration made me wonder how big my core system was.

:->[raven][/home/xed/X/P/prog/py/geogad]$ cat \
> interpreter.py \
> tokenizer.py \
> objectifier.py \
> evaluator.py \
> | grep -v '^ *#\|^ *$' | wc -l
486

That’s less than 500 lines with no thought at all towards minimalism beyond "good taste" (as Linus Torvalds calls it). My system supports command line interpreting, reading from programs, etc. I’m sure it could be radically shortened. Norvig’s version critically omits any native support for text types. My system handles a native text string type in a very comprehensive way. And if you think comprehensive text parsing is easy, you are very lucky.

Of course my system is not only 500 lines. That is just the core Lisp-like interpreter. With this system, I can easily implement functions. I have so far implemented over 200 functions and continually add more covering topics from computer science to engineering to statistics. I sometimes think of the system as a Python-to-interactive-Lisp converter.

I am not certain, but I will guess that there is another important difference between my system and Norvig’s - I actually use mine every day! Mine was written to be used interactively in a human readable way. It was, in fact, based on the beloved HP48 series of calculators. It is a blessing to me in many ways that one of my favorite and most used pieces of software was written by me.