Dr. Robert van Engelen Shrinks Lisp Down to a Mere 99 Lines of "Lisp-like" Compact C Code
Tinylisp delivers 21 primitives, garbage collection, and a fully-functional interactive REPL in two-digit lines of code.
Developer and Lisp enthusiast Dr. Robert van Engelen has released the very appropriately-named tinylisp — an implementation of the classic language in just 99 lines of C code, and easily expanded to add more features.
"In honor of the contributions made by [Alonzo] Church and [John] 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," van Engelen explains. "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."
Originally styled LISP, an abbreviation of "list processor," the Lisp programming language was created by John McCarthy, Steve Russell, Timothy P. Hart, and Mike Levin in the 1960s as a language for mathematical notation inspired by the lambda calculus of Alonzo Church some 20 years prior. It's a simple enough language to implement that interpreters can be found for a range of resource-constrained devices — but van Engelen's tinylisp takes things to an extreme.
Tinylisp takes up, in its standard form, a mere 99 lines of C code, yet delivers 21 Lisp primitives — expandable, should you desire and don't mind going into triple-figure lines-of-code — a simple garbage collection system, and a working read-evaluate-print-loop (REPL). This, van Engelen points out, makes it considerably more usable than rival "toy example" implementations — and, to prove it, has shown tinylisp running interactively on a Sharp Pocket Computer PC-G850VS, a turn-of-the-century calculator-slash-computer with BASIC, CASL, Z80 assembly, and C programming language support.
"[Tinylisp] 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," van Engelen explains. "It supports static scoping, double precision floating point, has 21 built-in Lisp primitives, a REPL, and a simple garbage collector. Tail-call optimized versions are included for speed and reduced memory use."
The project is documented in full on GitHub, along with the source code under the permissive BSD three-clause license.
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.