A dumb lisp interpreter, made for practice.
- Features
- Compiling
- Built-in Functions
- A libreadline-based REPL
- Create
lambda
functions with closures - Create macros
- Garbage collection using the Boehm GC
- It's dumb
# Install dependencies.
sudo apt-get install libreadline-dev libgc-dev # On Linux
brew install bdw-gc # On Mac OSX
# Get the source.
git clone https://github.com/rameshvarun/dumblisp.git
cd dumblisp
make && make test
print
can be used to write to stdout. Print takes at least 1 argument.
(print "Hello World" 3)
(defun fib (n)
(if (or (= n 0) (= n 1))
1
(+ (fib (- n 1)) (fib (- n 2)))))
(defun create-counter ()
(print "Creating new counter...")
(let ((i 0)) ; Start counter at 0
(lambda ()
(set i (+ i 1))
i)))
Macros essentially act like functions that take in their arguments as literal expressions, returning an expression that is then immediately evaluated.
(defmacro ++ (x)
(list 'set x (list '+ x 1)))
For both macros and functions, you can take in variable arguments by simply providing a symbol instead of an argument list.
(defun print-wrapper args
(print (len args) "arguments supplied.")
(apply print args))
let
creates a new scope, binds identifiers to values in that scope (in order), and evaluates expressions. plet
binds all of the variables in parallel.
Set sets the value of the symbol, obeying lexical scope. If no binding exists in the scope stack, then set automatically creates it in the closes scope.
The false-expr
is optional.
A basic while loop.
The case statement itself is implemented as a macro on top of the if
builtin.
- Arithmatic Operators (
+
,-
,/
,*
) - Comparison Operators (
=
,<
,>
,<=
,>=
) - Boolean Operators (
OR
,AND
,NOT
) - List Manipulation Functions (
LIST
,LEN
,HEAD
,TAIL
,NTH
,CONS
) - String Manipulation Functions (
STRLEN
,STRCMP
,STRCAT
,SUBSTR
) TYPEOF
functionPROGN
macro