Skip to content

miselin/quanta

Repository files navigation

Quanta, a Lisp

Quanta is a lightweight Lisp interpreter implemented as an exercise in building interpreters.

Useful discoveries and lessons that I've learned here will benefit ongoing development on my Haven programming language, although Haven and Quanta have different overall goals.

Features

  • Core Lisp Semantics:

    • Supports conses and atoms.
    • Implements special forms like quote, lambda, let, cond, and define.
    • Provides primitives for arithmetic (+, -, *, /), equality (eq?), and list manipulation (cons, car, cdr).
  • Interactive REPL:

    • Run Quanta interactively to evaluate expressions and explore its features.

Syntax Overview

Quanta uses a Lisp syntax where programs are composed of expressions. Here are some examples:

Atoms

5          ; Integer
"hello"    ; String
t          ; True
nil        ; False
:keyword   ; Keyword

Conses

()         ; Empty list
(1 2)      ; List with two elements
(1 (2))    ; Nested list
(1 (2 3))  ; Nested list with multiple elements
(1 (2 (3))) ; Deeply nested list
(1 . 2)    ; Explicit construction from CAR/CDR pair

Functions

(defun sqr (x) (* x x)) ; Defines a function `sqr`
(define squared (let ((x 2)) (sqr x))) ; Uses `let` to bind and call `sqr`
(print squared) ; Prints the value of `squared`
(cond ((eq? squared 4) 0) (t 1)) ; Conditional expression

REPL

(defun repl ()
  (begin
    (print "lisp> ")
    (let ((input (read-line)))
      (begin
        (print (eval (read input)))
        (repl)))))
(repl) ; Starts the REPL

Tail Call Optimization

(define sum (lambda (n acc)
    (cond ((eq? n 0) acc)
          (t (sum (- n 1) (+ n acc))))))
(print (sum 10000 0)) ; Ensures TCO works

Getting Started

Building the Project

Quanta uses CMake for building. To build the project, run:

mkdir build
cd build
cmake ..
make

Running the Interpreter

After building, you can run the interpreter:

./build/quanta

Running Tests

Quanta includes a suite of tests using GoogleTest. To run the tests:

cd build
ctest

License

Quanta is licensed under the MIT License. See the LICENSE file for details.

About

Quanta, a rudimentary Lisp

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published