Monday, January 19, 2026

Building a tiny Lisp interpreter with C++

I made a very simple Lisp interpreter using C++. I decided to focus on Lisp after a failed experience of trying to write a tiny BASIC interpreter for a cyberdeck build I'm working on putting together. I ended up shifting to Lisp for a variety of reasons. It is an interpreted language, meaning it relies on a program to act as a sort of middle man to execute the code versus compiling it to machine language. It also is one of the oldest computer languages with an extremely simple syntax. I decided to use C++ rather than Python for this task. C++ runs extremely fast compared to Python (which is also an interpreted language), and interpreters really require that speed provided by the program running as native machine code. This project could have been done using Python, but my guess is that running the program would have suffered from the increased headroom necessary of running in interpreter within an interpreter.

The interpreter is very basic by any standard. It currently can process the following mathematical operations: addition, subtraction, multiplication, division, absolute value, square root, and calculating powers. It can define and set variables, as well as print both numbers and strings to the console. It can handle floating point numbers. Essentially, it's an extremely simple programmable calculator!

So what did I learn from this? The first thing is, under the hood, there is nothing basic about BASIC. I was shocked at how challenging it is to write a simple interpreter for such as simple language. The second is the robustness and honestly the beauty of Lisp. The language, even in it's extremely simplified form is a joy to work with. I've always thought that there is magic to be found when working with limitations, and while Lisp seems to be extremely limited compared to more modern interpreted languages like Python, the structure of the language lends itself to some really creative solutions. 

Here is an example of code that runs in my interpreter to solve quadratic equations:

> (define a 1)
=> 1
> (define b -5)
=> -5
> (define c 6)
=> 6
> (define discriminant (- (* b b) (* 4 (* a c))))
=> 1
> (define x1 (/ (+ (- 0 b) (sqrt discriminant)) (* 2 a)))
=> 3
> (define x2 (/ (- (- 0 b) (sqrt discriminant)) (* 2 a)))
=> 2
> (print "Solutions: x1 =" x1 ", x2 =" x2)
Solutions: x1 = 3 , x2 = 2

 Below is a video of me running a demo of the interpreter to show the basic syntax for some of the operations and showing how to set up a simple program that converts Celsius to Fahrenheit.

 



The robotic voice in the video was created by a separate program I built with Python that converts text to speech and then saves the output as a .WAV file. Still working out some of the kinks with that, but it's fine for now.

 

No comments:

Post a Comment

-=CYBERDECKS=- (and why build one?)

A cyberdeck is a personal, portable computer inspired by classic cyberpunk fiction—think Neuromancer , Ghost in the Shell , or Blade Runner ...