kei-lisp 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Keisuke Ikeda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,189 @@
1
+ # kei-lisp
2
+
3
+ [![npm version](https://img.shields.io/npm/v/kei-lisp.svg)](https://www.npmjs.com/package/kei-lisp)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)
5
+ [![Node.js](https://img.shields.io/badge/node-%3E%3D24.0.0-brightgreen.svg)](https://nodejs.org/)
6
+
7
+ A Lisp interpreter implemented in TypeScript. Use it from the command line as
8
+ an interactive REPL, or embed it in your application as a library.
9
+
10
+ ## Features
11
+
12
+ - Common Lisp-inspired syntax (`setq`, `defun`, `let`, `cond`, ...)
13
+ - CLI tool **and** embeddable library
14
+ - ESM and CommonJS dual output with TypeScript types
15
+ - Zero runtime dependencies
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ # Use as a CLI tool
21
+ npm install -g kei-lisp
22
+
23
+ # Use as a library
24
+ npm install kei-lisp
25
+ ```
26
+
27
+ Requires **Node.js >= 24**.
28
+
29
+ ## Quick start
30
+
31
+ ### CLI
32
+
33
+ ```sh
34
+ $ kei-lisp
35
+ >> (+ 1 2 3)
36
+ 6
37
+ >> (defun square (x) (* x x))
38
+ square
39
+ >> (square 7)
40
+ 49
41
+ >> (exit)
42
+ Bye!
43
+ ```
44
+
45
+ ```sh
46
+ kei-lisp --version # Show version
47
+ kei-lisp --help # Show help
48
+ ```
49
+
50
+ ### Library
51
+
52
+ ```ts
53
+ import { LispInterpreter, Cons } from 'kei-lisp';
54
+
55
+ const interpreter = new LispInterpreter();
56
+ const result = interpreter.evalString('(+ 1 2 3)');
57
+ console.log(Cons.toString(result)); // "6"
58
+ ```
59
+
60
+ CommonJS is also supported:
61
+
62
+ ```js
63
+ const { LispInterpreter, Cons } = require('kei-lisp');
64
+ ```
65
+
66
+ ## API
67
+
68
+ | Export | Description |
69
+ | ------------------- | ------------------------------------------------------- |
70
+ | `LispInterpreter` | Main interpreter class (REPL + programmatic evaluation) |
71
+ | `Cons` | Cons cell (pair) data type with type predicates |
72
+ | `InterpretedSymbol` | Lisp symbol (interned) |
73
+ | `ExitError` | Thrown when `(exit)` is evaluated; catch to handle exit |
74
+
75
+ ### `LispInterpreter`
76
+
77
+ ```ts
78
+ const interpreter = new LispInterpreter();
79
+
80
+ // Start an interactive REPL on stdin/stdout
81
+ interpreter.run();
82
+
83
+ // Evaluate source and return the last expression's result
84
+ interpreter.evalString('(+ 1 2)'); // 3
85
+
86
+ // Evaluate multiple expressions and return all results
87
+ interpreter.evalAll('(setq x 10) (* x x)'); // [10, 100]
88
+ ```
89
+
90
+ ### Handling `(exit)` gracefully
91
+
92
+ When user-supplied Lisp code calls `(exit)`, an `ExitError` is thrown so the
93
+ host application can clean up instead of being terminated by `process.exit`:
94
+
95
+ ```ts
96
+ import { LispInterpreter, ExitError } from 'kei-lisp';
97
+
98
+ const interpreter = new LispInterpreter();
99
+ try {
100
+ interpreter.evalString(userInput);
101
+ } catch (error) {
102
+ if (error instanceof ExitError) {
103
+ // Lisp program requested exit — handle gracefully
104
+ return;
105
+ }
106
+ throw error;
107
+ }
108
+ ```
109
+
110
+ ## Examples
111
+
112
+ ### Arithmetic
113
+
114
+ ```lisp
115
+ (+ 1 2 3) ;; => 6
116
+ (- 10 3) ;; => 7
117
+ (* 4 5) ;; => 20
118
+ (/ 100 4) ;; => 25
119
+ (mod 10 3) ;; => 1
120
+ ```
121
+
122
+ ### Lists
123
+
124
+ ```lisp
125
+ (list 1 2 3) ;; => (1 2 3)
126
+ (car (list 1 2 3)) ;; => 1
127
+ (cdr (list 1 2 3)) ;; => (2 3)
128
+ (cons 0 (list 1 2 3)) ;; => (0 1 2 3)
129
+ (length (list 1 2 3)) ;; => 3
130
+ ```
131
+
132
+ ### Defining functions
133
+
134
+ ```lisp
135
+ (defun factorial (n)
136
+ (if (= n 0) 1 (* n (factorial (- n 1)))))
137
+
138
+ (factorial 10) ;; => 3628800
139
+ ```
140
+
141
+ ### Conditionals and bindings
142
+
143
+ ```lisp
144
+ (if (= 1 1) "yes" "no") ;; => "yes"
145
+ (cond ((= 1 2) "a") ((= 1 1) "b") (t "c")) ;; => "b"
146
+ (let ((x 10) (y 20)) (+ x y)) ;; => 30
147
+ ```
148
+
149
+ Runnable TypeScript examples live in [`examples/`](./examples/):
150
+
151
+ ```sh
152
+ pnpm build # build the package once
153
+ pnpm exec tsx examples/basic-eval.ts
154
+ pnpm exec tsx examples/exit-handling.ts
155
+ ```
156
+
157
+ ## Reference
158
+
159
+ In-depth documentation of each language area:
160
+
161
+ - [API Reference](./docs/api.md) — TypeScript / JavaScript library API
162
+ - [Atoms](./docs/atoms.md) — numbers, symbols, strings, nil
163
+ - [Cons](./docs/cons.md) — pairs and lists
164
+ - [Built-in Functions](./docs/built-in-functions.md) — full Lisp reference
165
+
166
+ ## Development
167
+
168
+ ```sh
169
+ git clone https://github.com/ike-keichan/kei-lisp.git
170
+ cd kei-lisp
171
+ pnpm install
172
+ pnpm start
173
+ ```
174
+
175
+ Requires [pnpm](https://pnpm.io/) and Node.js 24+
176
+ (see [`.node-version`](./.node-version) for the exact version).
177
+
178
+ | Command | Description |
179
+ | ----------------- | ----------------------------------------- |
180
+ | `pnpm build` | Build for distribution |
181
+ | `pnpm start` | Run the built CLI |
182
+ | `pnpm test` | Run tests |
183
+ | `pnpm test:watch` | Run tests in watch mode |
184
+ | `pnpm check` | Run all checks (format, lint, spell, ...) |
185
+ | `pnpm fix` | Auto-fix format and lint issues |
186
+
187
+ ## License
188
+
189
+ [MIT](./LICENSE)