Skip to content

tantaouibahaa/lisp_in_c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lispy (Build Your Own Lisp in C)

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 ;

Files

  • main.c - interpreter implementation
  • mpc.c, mpc.h - parser combinator library used by the interpreter

Build

On macOS/Linux:

cc -std=c99 -Wall -Wextra main.c mpc.c -ledit -lm -o a.out

If -ledit is missing on your machine, install/editline first, then rebuild.

Language Notes

  • Expressions are separated by whitespace/newlines, not commas.
  • Anything non-zero is true in if.
  • ; starts a comment until end of line.

Valid

"hello"
"hello\n"
(print "Hello" "world")

Invalid

"hello", "world"

The comma is not a token in this grammar.

Builtins

Core list/eval:

  • list, head, tail, join, eval

Math:

  • +, -, *, /

Variables/functions:

  • def, =, \

Conditionals/comparison:

  • if, ==, !=, >, <, >=, <=

Strings/IO:

  • load, print, error

Example Session

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"}

Example Script (hello.lspy)

; 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

About

A Lisp in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages