aboutsummaryrefslogtreecommitdiff
path: root/Readme.md
blob: c10512bbe96964b90d9a28dcc388a664bedf915f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Bamboo Lisp

Embeddable & Hackable Lisp-2 Interpreter

There is a WebAssembly build, you can [try it online](https://mistivia.github.io/bamboo-lisp/).

## Features

- Lisp-2 (more like Common Lisp or Emacs Lisp)
- Lexical scoping
- The interpreter part is ~2500 LOC (excluding built-in functions)
- Tail call optimization
- Any C99 compiler should work
- Depends only on C standard library
- A simple mark-sweep GC
- Writing macro is easy with quasiquote, unquote, and slicing-unquote
- No global state, you can run multiple interpreters in multiple threads
- Exception and try-catch
- Stacktrace for debugging
- Support C-like control flow statements
    - return
    - break
    - continue

## Drawbacks

To keep simplicity, Bamboo Lisp is a VERY SLOW tree-walking interpreter. The performance is similar to other small Lisp interpreters like TinyScheme or very early Emacs Lisp, which is only 1/5 to 1/10 that of modern Python.

## Build

Init submodule:

```bash
git submodule update --init --recursive
```

Debug:

```bash
make
```

Release:

```bash
make clean
make mode=release
```

## Usage

After building, you can run the Bamboo Lisp interpreter using:

```bash
./bamboo-lisp # To enter the REPL (if applicable)
./bamboo-lisp <filename.lisp> # To run a Lisp file
```

You can use `load` to load a lisp script into the interpreter:

```lisp
(load "my-script.lisp")
```

## Documents (WIP)

- [Primitives](https://github.com/mistivia/bamboo-lisp/blob/master/docs/primitives.md)

## Example

See `tests/` for more examples.

### 1. Y Combinator

```lisp
(defun Y (f)
  (funcall
    (lambda (g) (funcall g g))
    (lambda (h)
       (funcall f (lambda args (apply (funcall h h) args))))))

(defun fibo-impl (self)
  (lambda (n)
    (if (<= n 2)
        1
        (+ (funcall self (- n 1)) (funcall self (- n 2))))))

(defvar fibo (Y #'fibo-impl))

(funcall fibo 10)
;; Expected output: 55
```

### 2. Macro

```lisp
(defmacro inc (x)
  `(setq ,x (+ ,x 1)))

(defmacro for (start pred inc . body)
  `(let (,start)
     (while ,pred
       ,@body
       ,inc)))

(for (i 0) (< i 10) (inc i)
  (princ "meow\n"))

;; Expected output:
;; meow
;; meow
;; ... (10 times)
```