https://github.com/carld/micro-lisp
πA very small Lisp programming language πthat used to be under 200 lines of Cπ
https://github.com/carld/micro-lisp
c lisp micro scheme small tiny
Last synced: about 1 month ago
JSON representation
πA very small Lisp programming language πthat used to be under 200 lines of Cπ
- Host: GitHub
- URL: https://github.com/carld/micro-lisp
- Owner: carld
- License: mit
- Archived: true
- Created: 2017-11-05T07:01:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-13T08:59:32.000Z (over 5 years ago)
- Last Synced: 2024-08-01T03:29:54.843Z (9 months ago)
- Topics: c, lisp, micro, scheme, small, tiny
- Language: C
- Homepage:
- Size: 74.2 KB
- Stars: 792
- Watchers: 26
- Forks: 72
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Micro Lisp
Objective: implement a small Lisp/Scheme language in as little C code as possible.
_This is a hobby project for educational purposes, it has bugs and may fail without warning. Pull requests and improvements welcome_
The interpreter supports `lambda`, e.g.
```lisp
((lambda (x) (cons x (quote 1))) (quote 7))
(7 . 1)
```Note that `lambda` does not capture free variables (variables that are not passed as arguments and refer to an outer scope). Free variables will resolve to their assigned values in the environment when the body of the lambda is evaluated.
The special forms `if` and `quote` behave in a typical way:
```lisp
(if (quote t) (quote 7) (quote 0))
7
```The only types are symbols and pairs.
Non-quoted symbols are looked up in the environment. If they have no associated
value the result is `null`; in fact, zero. Because there is no numeric type a
number e.g. `7` will be treated like any other symbol and looked up in the environment.
Note in the examples above how numbers are quoted to prevent that.The built-in primitives in the environment are: `car`, `cdr`, `cons`, `eq?`,
`pair?`, `read`, `write`.Also provided is `apply` which takes a function and a single list argument:
```lisp
(apply write (quote ((hello world))))
(hello world)
(quote t)
```Lists can be built up by `cons`ing:
```lisp
(apply write (cons (cons (quote hello) (cons (quote world) null)) null))
(hello world)
(quote t)
```### Read Eval Print Loop
A REPL is implemented in micro-lisp itself. To try it out in a terminal:
```
cat repl.lisp - | ./micro-lisp
```To exit, press 'control c' to terminate the process.
Note the `-` argument to `cat` to pipe stdin through, otherwise micro-lisp will receive end-of-file.
The source code for the REPL is in `repl.lisp`. It implements `eval` and provides an environment which resolves symbols to the primitive functions in the underlying micro-lisp interpreter.
### Debugging with GDB
A `.gdbinit` file sets the target, breakpoints and runs the executable. Simply run `gdb`.
Pull requests welcome.
[](https://asciinema.org/a/HbNmch4GVH9jXpeY3I6yvfqPF?autoplay=1)