Simple Scheme interpreter in Python
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src/serval Formatted imports Dec 12, 2010
.gitignore More notes Dec 13, 2010 Initial interpreter implementation Dec 12, 2010
buildout.cfg Initial interpreter implementation Dec 12, 2010
versions.cfg Fixed issue with buildout and Distribute. Mar 21, 2011

Serval - Simple Scheme interpreter in Python


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 goal of the project

  1. Self-education (I've been always fascinated by language design and implementation, it was about time I started learning about interpreters and compilers by actually writing something).

  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 the Serval to be able to run all examples from "The Little Schemer" book (that was my test target). The project has a test module which runs simple meta-circular evaluator from Ch.10 of the book.

This is a current high-level overview of the interpreter:

      |    Scheme      |
      |  source code   |
      |                |
      |  High-Level    |
      |  Interpreter   |
      |                |


Session examples (REPL)

  1. Defining simple function

     serval> (define add1 (lambda (x) (+ x 1)))
     serval> (add1 9)
  2. Loading representation from a file

     serval> (load "/home/alienoid/scheme/the_little_schemer/")
     serval> (value '((lambda (x) (cons x x)) 5))
     (5 . 5)


  1. Using buildout (useful for local development and testing)

     $ cd serval
     $ python
     $ bin/buildout
     Run the interpreter's REPL
     $ bin/serval
  2. Using pip or easy_install (no need for sudo if using virtualenv)

     $ sudo pip install serval
     (or run alternatively easy_install: `$ sudo easy_install serval`)
     Run the interpreter's REPL
     $ serval

Technical details

Serval has a lexer which uses regular expressions to get a next token and a recursive-descent parser implementation.

Some code parts are not idiomatic Python because I tried to follow SICP implementation as close as possible for this interpeter.

There is no multiline editing support for the moment. If you need one, you'd be better off by saving the code into a file and loading it with load builtin function provided by REPL.


Install 'enscript' utility (optional). If you are on Ubuntu::

$ sudo apt-get install enscript

Boostrap the buildout and run it:

$ cd serval
$ python
$ bin/buildout

Run tests, test coverage and produce coverage reports::

$ bin/test
$ bin/coverage-test
$ bin/coveragereport

Check ./var/report/serval.html out for coverage results.

Run pep8 and pylint to check code style and search for potential bugs:

$ bin/pep8
$ bin/pylint


  1. Scheme translator to high-level assembly language
  2. Bytecode assembler
  3. Register based bytecode interpretator (VM)