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.
Serval is a high-level Scheme interpreter written in Python. It implements a subset of R5RS. The code closely follows Scheme meta-circular evaluator implementation from Ch.4 of the SICP book.
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)))
ok
serval> (add1 9)
10
- 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:
- SICP
- The Little Schemer
- The Scheme Programming Language
- Language Implementation Patterns
- Scheme from Scratch
- Bob Scheme



{ 3 comments… read them below or add one }
Does it do call/cc ?
Hi Nachik,
Not at this moment. I plan to have call/cc in Serval compiler(if I ever get to that point
Cheers,
Ruslan
(call-with-current-continuation)
Sorry, forgot to tick on the notification boxes.