Skip to content

8dcc/sl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sl

Simple LISP interpreter.

Description

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:

Components

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.

  1. 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 to main().
  2. The raw user input is converted into an array of Token structures using the tokens_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 the Token array using token_print(), if needed. All these functions are defined in lexer.c.
  3. The Token array is converted into a linked list of Expr structures using the parse() function from parser.c. At this point, nothing has been evaluated, it should just be a different representation of the user input. Then, the Token array is freed. Note how all the values allocated by tokens_scan() have been copied instead of reused, so they can be freed safely.
  4. We evaluate the expression using eval(), which will call apply() if needed. Both of these are defined in eval.c. This function will return another linked list of Expr structures, but just like parse(), it will not reuse any data in the heap, so the old Expr* can be freed safely.
  5. After that, we print the evaluated expression using expr_print(), defined in expr.c.

Building

$ git clone https://github.com/8dcc/sl
$ cd sl
$ make
...

Usage

$ ./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