Serval – Simple Scheme interpreter in Python

by Ruslan Spivak on December 12, 2010

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:

  1. Self-education (For me there is no better way of gaining and retaining information than by (re)implementing something on my own)
  2. To serve as a potential example for other people interested in interpreter implementation, particularly Scheme interpreter.
  3. 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:

  1. Defining simple function

    serval> (define add1 (lambda (x) (+ x 1)))
    ok
    serval> (add1 9)
    10
  2. 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:

  1. SICP
  2. The Little Schemer
  3. The Scheme Programming Language
  4. Language Implementation Patterns
  5. Scheme from Scratch
  6. Bob Scheme
If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately. You can also follow me on GitHub or Twitter.

{ 3 comments… read them below or add one }

Nachik February 12, 2011 at 8:04 PM

Does it do call/cc ?

Reply

Ruslan Spivak February 12, 2011 at 8:36 PM

Hi Nachik,

Not at this moment. I plan to have call/cc in Serval compiler(if I ever get to that point :)

Cheers,
Ruslan

Reply

Nachik February 12, 2011 at 8:05 PM

(call-with-current-continuation)

Sorry, forgot to tick on the notification boxes.

Reply

Speak your mind

Previous post:

Next post: