I’ve been always fascinated by and interested in interpreters, compilers, and language design. But there is a huge gap between being interested and actually knowing how to implement one whether it’s a simple interpreter, compiler or a programming language.
So couple weeks ago I decided to reduce the gap I had and start with a simple high-level interpreter for a subset of Scheme. Thus after several weekend evenings of code tweaking Serval was born.
The goals of the project are pretty simple:
- Self-education (For me there is no better way of gaining and retaining information than by (re)implementing something on my own)
- To serve as a potential example for other people interested in interpreter implementation, particularly Scheme interpreter.
- Not a goal per se, but I wanted Serval to be able to run all examples from “The Little Schemer” book (that was my test target). The project has a test module test_the_little_schemer.py which runs simple meta-circular evaluator from Ch.10 of the book. Yeah, now I can evaluate Scheme code using Scheme evaluator that is interpreted by Scheme interpreter written in Python
To whet your appetite here are some examples of Serval REPL:
- Defining simple function
serval> (define add1 (lambda (x) (+ x 1)))
serval> (add1 9)
- Loading representation from a file
serval> (load "/home/alienoid/scheme/the_little_schemer/ch10.ss")
serval> (value '((lambda (x) (cons x x)) 5))
(5 . 5)
To install the interpreter use either pip install
$ sudo pip install serval $ serval serval>
or clone Git repo and run buildout:
$ git clone git://github.com/rspivak/serval.git $ cd serval/ $ python bootstrap.py $ bin/buildout $ bin/serval serval>
Resources that I found useful when working on Serval:
- The Little Schemer
- The Scheme Programming Language
- Language Implementation Patterns
- Scheme from Scratch
- Bob Scheme