a small Lisp interpreter written in C, built following: Build Your Own Lisp.
Current implementation includes:
- numbers, symbols, strings
- S-expressions
(...)and Q-expressions{...} - variables (
def,=) - lambdas (
\) with partial application - variable arguments with
& - conditionals and comparisons (
if,==,!=,>,<,>=,<=) - file loading (
load) - printing and user errors (
print,error) - line comments starting with
;
main.c- interpreter implementationmpc.c,mpc.h- parser combinator library used by the interpreter
On macOS/Linux:
cc -std=c99 -Wall -Wextra main.c mpc.c -ledit -lm -o a.outIf -ledit is missing on your machine, install/editline first, then rebuild.
- Expressions are separated by whitespace/newlines, not commas.
- Anything non-zero is true in
if. ;starts a comment until end of line.
"hello"
"hello\n"
(print "Hello" "world")"hello", "world"The comma is not a token in this grammar.
Core list/eval:
list,head,tail,join,eval
Math:
+,-,*,/
Variables/functions:
def,=,\
Conditionals/comparison:
if,==,!=,>,<,>=,<=
Strings/IO:
load,print,error
def {add-mul} (\ {x y} {+ x (* x y)})
add-mul 10 20
; => 210
def {add-mul-ten} (add-mul 10)
add-mul-ten 50
; => 510
if (== 1 1) {print "yes"} {print "no"}; demo script
(print "Hello from file")
(def {x y} 100 200)
(print x y)
(if (== x y) {print "equal"} {print "not equal"})Run it:
./a.out hello.lspy