Simple LISP interpreter.
Simple LISP interpreter made in C. This project is still WIP, but I plan on writing some detailed documentation once the project becomes stable. It will probably be published in my blog.
For more information, see:
- LISP (Wikipedia)
- Scheme (Wikipedia)
- Official Scheme website
- Scheme R5RS Specification
- Build Your Own Lisp website (Although this project doesn’t follow the book)
- SICP website
Note: This project is still WIP, so this section might be a bit outdated.
This LISP has a few components that are responsible for their own data. This is the basic process of parsing and evaluating the user input.
- The user input is read using
input_read()
, defined in main.c. This function will read across lines making sure that all opened expressions are closed before sending the data back tomain()
. - The raw user input is converted into an array of
Token
structures using thetokens_scan()
function. This step might be redundant for such a simple language as LISP, but I decided to do it anyway. After calling this function, it frees the input. You could print theToken
array usingtoken_print()
, if needed. All these functions are defined in lexer.c. - The
Token
array is converted into a linked list ofExpr
structures using theparse()
function from parser.c. At this point, nothing has been evaluated, it should just be a different representation of the user input. Then, theToken
array is freed. Note how all the values allocated bytokens_scan()
have been copied instead of reused, so they can be freed safely. - We evaluate the expression using
eval()
, which will callapply()
if needed. Both of these are defined in eval.c. This function will return another linked list ofExpr
structures, but just likeparse()
, it will not reuse any data in the heap, so the oldExpr*
can be freed safely. - After that, we print the evaluated expression using
expr_print()
, defined in expr.c.
$ git clone https://github.com/8dcc/sl
$ cd sl
$ make
...
$ ./sl
sl> (+ 1 2 (- 5 4) (* 3 4))
16.000000
sl> define
<primitive 0x55d31f51b900>
sl> (define 'my-var 'Testing)
'Testing
sl> (define 'my-add +)
<primitive 0x55d31f51ba21>
sl> (+ 1 2 3)
6.000000