Lisp interpreter written in C
- Clone:
git clone https://github.com/mmniazi/lisp.git - Build (required for run or repl):
make - Run:
lisp my_file.lisp - Repl:
lisp
- Primitive data types and strings
- Common operators (
+,-,*,/,>,<=,==...) - Comments
- Variables
- Flow control (
if/else,while,switch) - Functions & lambdas
- Higher order functions
- Scopes
- S-expressions
- Q-expressions/lists and list operators
- Varargs
- Importing code
- Error reporting and stack traces
; Fibonacci (fun {fib n} { select { (== n 0) 0 } { (== n 1) 1 } { otherwise (+ (fib (- n 1)) (fib (- n 2))) } }) Call function with given list of args
= {params} {1 2 3} fun {plus x y z} {+ x y z} (curry plus params) > 6 Call function with single list created from args
uncurry print 1 2 3 > {1 2 3} Perform no of actions in sequence
do (= {x} 2) (+ x 4) > 6 Create a local scope
let {do (= {x} 100) (x)} > 100 x > Error: Unbound Symbol 'x' Logical functions
or (and true false) true > 1 Flip arguments for currying
flip / 1 2 > 2 apply functions in sequence (comp f g x) -> f(g(x))
comp head last {1 2 {3 4 5}} Return first, second or third element of list
fst {1 2 3 4} > 1 snd {1 2 3 4} > 2 trd {1 2 3 4} > 3 Return element on nth index
nth 2 {1 2 3 4} > 3 Return last element of list
last {1 2 3} > 3 Length of list
len {1 2 3 5} > 4 Take N items from list
take 2 {1 2 3 5} > {1 2} Drop N items from list
drop 2 {1 2 3 5} > {3 5} Split list at Nth element
split 1 {1 2 3 5} > {{1 2} {3 5}} Check for presence of element
elem 5 {1 2 3 5} > 1 elem 9 {1 2 3 5} > 0 Create new list by applying function to every elem of list
fun {square x} {* x x} map square {1 2 3} > {1 4 9} Create a new list of items which match the condition
fun {even x} {== (% x 2) 0} filter even {1 2 3 4} > {2 4} Fold left
foldl * 1 {2 2} > 4 switch statement, takes zero or more (cond, body) pairs
(fun {numbers x} { case x {0 "Zero"} {1 "One"} }) numbers 0 > Zero - Most of this repo is direct implementation of this amazing book with major difference being that I have written my own tokenizer and parser, instead of using parser-combinator library.