/tinylisp

Lisp in 99 lines of C and how to write one yourself. Includes 20 Lisp primitives, garbage collection and REPL. Includes tail-call optimized versions for speed and reduced memory use.

Primary LanguageCBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Lisp in 99 lines of C and how to write one yourself

In honor of the contributions made by Church and McCarthy, I wrote this project and the accompanying article to show how anyone can write a tiny Lisp interpreter in a few lines of C or in any "C-like" programming language for that matter. I attempted to preserve the original meaning and flavor of Lisp as much as possible. As a result, the C code in this project is strongly Lisp-like in compact form. Despite being small, these tiny Lisp interpreters in C include 20 built-in Lisp primitives, garbage collection and REPL, which makes them a bit more practical than a toy example. If desired, more Lisp features can be easily added with a few more lines of C as explained in my article with examples that are ready for you to try.

Tinylisp running on a vintage Sharp PC-G850VS pocket computer

PC-G850

A cool pocket computer with a built-in C compiler. Tinylisp compiles and runs on this machine too with its native C!

How is tinylisp so small?

Using NaN boxing and BCD boxing and some programming tricks in C. See my article for details, examples and detailed instructions how to extend tinylisp with more features.

Project code

Lisp in 99 lines is written in a Lisp-like functional style of structured C, lines are 55 columns wide on average and never wider than 120 columns for convenient editing. It supports double precision floating point, has 20 built-in Lisp primitives, a REPL and a simple garbage collector. Tail-call optimized versions are included for speed and reduced memory use.

To compile tinylisp:

$ cc -o tinylisp tinylisp-opt.c

The number of cells allocated is N=1024 by default, which is only 8K of memory. To increase memory size, increase the value of N in the code. Then recompile tinylisp.

To install one or more optional Lisp libraries to run tinylisp, use Linux/Unix cat:

$ cat common.lisp list.lisp math.lisp | ./tinylisp

But before you can do this, change the look function to reopen /dev/tty as explained in Section 7 of the article.

PC-G850

On the Sharp PC-G850(V)(S) use SIO or PocketTools to load via audio cassette interface (CE-124 or CE-126p):

PC:   bas2img --pc=G850VS --type=asm -l0x408 lisp850-opt.c
PC:   bin2wav --pc=G850VS lisp850-opt.img
G850: BASIC (PRO MODE)
G850: BLOAD
PC:   play lisp850-opt.wav
G850: TEXT
G850: Basic
G850: Text<-Basic
G850: BASIC (2x PRO MODE)
G850: NEW
G850: 2ndF TEXT (C)
G840: G (go)

The bas2img option -l0x400 adds line numbers to the C source automatically.

Spoiler alert!

In "Lisp in 1k lines of C, explained" I introduce another small Lisp interpreter that is largely based on tinylisp. It shares many similarities, but has over 40 built-in Lisp primitives, strings, macros, exceptions, execution tracing, file loading, a mark-sweep/compacting garbage collector and REPL.