@thi.ng/lispy 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # Change Log
2
+
3
+ - **Last updated**: 2025-05-20T19:06:30Z
4
+ - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
+
6
+ All notable changes to this project will be documented in this file.
7
+ Only versions published since **2022-01-01** are listed here.
8
+ Please consult the Git history for older version information.
9
+ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelines.
10
+
11
+ **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
12
+ and/or version bumps of transitive dependencies.
13
+
14
+ ## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/lispy@0.1.0) (2025-05-20)
15
+
16
+ #### 🚀 Features
17
+
18
+ - import as new pkg ([5ddb591](https://github.com/thi-ng/umbrella/commit/5ddb591))
19
+ - add more builtins ([c010478](https://github.com/thi-ng/umbrella/commit/c010478))
20
+ - add string/regex builtins ([602c1b9](https://github.com/thi-ng/umbrella/commit/602c1b9))
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,313 @@
1
+ <!-- This file is generated - DO NOT EDIT! -->
2
+ <!-- Please see: https://github.com/thi-ng/umbrella/blob/develop/CONTRIBUTING.md#changes-to-readme-files -->
3
+ # ![@thi.ng/lispy](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/banners/thing-lispy.svg?a9b3f1f7)
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@thi.ng/lispy.svg)](https://www.npmjs.com/package/@thi.ng/lispy)
6
+ ![npm downloads](https://img.shields.io/npm/dm/@thi.ng/lispy.svg)
7
+ [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
+
9
+ > [!NOTE]
10
+ > This is one of 208 standalone projects, maintained as part
11
+ > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
+ > and anti-framework.
13
+ >
14
+ > 🚀 Please help me to work full-time on these projects by [sponsoring me on
15
+ > GitHub](https://github.com/sponsors/postspectacular). Thank you! ❤️
16
+
17
+ - [About](#about)
18
+ - [Status](#status)
19
+ - [Installation](#installation)
20
+ - [Dependencies](#dependencies)
21
+ - [Usage examples](#usage-examples)
22
+ - [API](#api)
23
+ - [Authors](#authors)
24
+ - [License](#license)
25
+
26
+ ## About
27
+
28
+ Lightweight, extensible, interpreted Lisp-style DSL for embedding in other projects.
29
+
30
+ > [!NOTE] This DSL implementation has been extracted as standalone package from
31
+ > existing work/examples bundled in the thi.ng/umbrella monorepo (i.e.
32
+ > [lispy-repl](https://github.com/thi-ng/umbrella/tree/develop/examples/lispy-repl))
33
+
34
+ The core language is intentionally kept extremely minimal and currently only
35
+ contains the following:
36
+
37
+ - basic math ops (multi-arity)
38
+ - logic operators (multi-arity)
39
+ - `and`
40
+ - `or`
41
+ - `not`
42
+ - bitwise operators
43
+ - `<<`
44
+ - `>>` / `>>>`
45
+ - `bit-and`
46
+ - `bit-or`
47
+ - `bit-xor`
48
+ - `bit-not`
49
+ - comparison operators
50
+ - constants & functions from JS-native `Math`
51
+ - selected utilities from [thi.ng/math](https://thi.ng/math) package
52
+ - `HALF_PI`
53
+ - `TAU`
54
+ - `clamp`: clamp value to interval
55
+ - `deg`: convert radians to degrees
56
+ - `fit`: fit value from one interval into another
57
+ - `mix`: linear interpolation (same as GLSL `mix()`)
58
+ - `rad`: convert degrees to radians
59
+ - `step`: same as GLSL `step()`
60
+ - `smoothstep`: same as GLSL `smoothstep()`
61
+ - basic FP programming constructs
62
+ - `comp`: functional composition
63
+ - `def`: define global symbol
64
+ - `defn`: define global function
65
+ - `let`: locally scope var bindings/expression
66
+ - `partial`: partial application (currying)
67
+ - basic list/array processing
68
+ - `aget`: get array index
69
+ - `aset`: set array index
70
+ - `count`: number of items
71
+ - `concat`: same as `Array.prototype.concat(...)`
72
+ - `filter`: list/array filtering
73
+ - `first`: first item of array
74
+ - `map`: list/array transformation
75
+ - `next`: remaining array items (after first)
76
+ - `push`: same as `Array.prototype.push(...)`
77
+ - `reduce`: list/array reduction
78
+ - string/regexp processing
79
+ - `capitalize`
80
+ - `lower`
81
+ - `upper`
82
+ - `pad-left`
83
+ - `pad-right`
84
+ - `substr`
85
+ - `trim`
86
+ - `regexp`
87
+ - `re-match`
88
+ - `re-test`
89
+ - `replace`
90
+ - misc utilities
91
+ - `print`: aka `console.log(...)`
92
+ - `env`: JSON stringified version of current root env
93
+
94
+ The core language can be easily customized/extended by defining new items in the
95
+ root environment `ENV` (see example below).
96
+
97
+ ## Status
98
+
99
+ **ALPHA** - bleeding edge / work-in-progress
100
+
101
+ [Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Blispy%5D+in%3Atitle)
102
+
103
+ ## Installation
104
+
105
+ ```bash
106
+ yarn add @thi.ng/lispy
107
+ ```
108
+
109
+ ESM import:
110
+
111
+ ```ts
112
+ import * as lispy from "@thi.ng/lispy";
113
+ ```
114
+
115
+ Browser ESM import:
116
+
117
+ ```html
118
+ <script type="module" src="https://esm.run/@thi.ng/lispy"></script>
119
+ ```
120
+
121
+ [JSDelivr documentation](https://www.jsdelivr.com/)
122
+
123
+ For Node.js REPL:
124
+
125
+ ```js
126
+ const lispy = await import("@thi.ng/lispy");
127
+ ```
128
+
129
+ Package sizes (brotli'd, pre-treeshake): ESM: 1.50 KB
130
+
131
+ ## Dependencies
132
+
133
+ - [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
134
+ - [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
135
+ - [@thi.ng/compare](https://github.com/thi-ng/umbrella/tree/develop/packages/compare)
136
+ - [@thi.ng/defmulti](https://github.com/thi-ng/umbrella/tree/develop/packages/defmulti)
137
+ - [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
138
+ - [@thi.ng/math](https://github.com/thi-ng/umbrella/tree/develop/packages/math)
139
+ - [@thi.ng/object-utils](https://github.com/thi-ng/umbrella/tree/develop/packages/object-utils)
140
+ - [@thi.ng/sexpr](https://github.com/thi-ng/umbrella/tree/develop/packages/sexpr)
141
+ - [@thi.ng/strings](https://github.com/thi-ng/umbrella/tree/develop/packages/strings)
142
+
143
+ Note: @thi.ng/api is in _most_ cases a type-only import (not used at runtime)
144
+
145
+ ## Usage examples
146
+
147
+ One project in this repo's
148
+ [/examples](https://github.com/thi-ng/umbrella/tree/develop/examples)
149
+ directory is using this package:
150
+
151
+ | Screenshot | Description | Live demo | Source |
152
+ |:------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------|:-------------------------------------------------|:------------------------------------------------------------------------------|
153
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/lispy-repl.png" width="240"/> | Browser REPL for a Lispy S-expression based mini language | [Demo](https://demo.thi.ng/umbrella/lispy-repl/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/lispy-repl) |
154
+
155
+ ## API
156
+
157
+ [Generated API docs](https://docs.thi.ng/umbrella/lispy/)
158
+
159
+ ```ts tangle:export/readme-1.ts
160
+ import { evalSource, ENV } from "@thi.ng/lispy";
161
+
162
+ // define custom FFI in the root environment
163
+ // (here it's actually the same as default print fn)
164
+ ENV.print = console.log;
165
+
166
+ const SRC = `
167
+ (print (+ 1 2 3 4))
168
+ ;; 10
169
+
170
+ ;; local variables
171
+ (let (a 23 b 42) (print (+ a b)))
172
+ ;; 65
173
+
174
+ ;; define global var/fn
175
+ ;; here, a curried version of the built-in print fn
176
+ (def greetings! (partial print "hello,"))
177
+
178
+ ;; print greeting (name will be provided externally)
179
+ (greetings! name)
180
+ ;; hello, lispy!
181
+
182
+ ;; print contents of default environment
183
+ (print (env))
184
+ `;
185
+
186
+ evalSource(SRC, {...ENV, name: "lispy"});
187
+
188
+ // output:
189
+ // 10
190
+ // 65
191
+ // hello, lispy!
192
+ //
193
+ // {
194
+ // "+": "<function>",
195
+ // "*": "<function>",
196
+ // "-": "<function>",
197
+ // "/": "<function>",
198
+ // "=": "<function>",
199
+ // "!=": "<function>",
200
+ // "<": "<function>",
201
+ // "<=": "<function>",
202
+ // ">=": "<function>",
203
+ // ">": "<function>",
204
+ // "and": "<function>",
205
+ // "or": "<function>",
206
+ // "not": "<function>",
207
+ // "<<": "<function>",
208
+ // ">>": "<function>",
209
+ // ">>>": "<function>",
210
+ // "bit-and": "<function>",
211
+ // "bit-or": "<function>",
212
+ // "bit-xor": "<function>",
213
+ // "bit-not": "<function>",
214
+ // "E": 2.718281828459045,
215
+ // "LN10": 2.302585092994046,
216
+ // "LN2": 0.6931471805599453,
217
+ // "LOG10E": 0.4342944819032518,
218
+ // "LOG2E": 1.4426950408889634,
219
+ // "PI": 3.141592653589793,
220
+ // "SQRT1_2": 0.7071067811865476,
221
+ // "SQRT2": 1.4142135623730951,
222
+ // "abs": "<function>",
223
+ // "acos": "<function>",
224
+ // "acosh": "<function>",
225
+ // "asin": "<function>",
226
+ // "asinh": "<function>",
227
+ // "atan": "<function>",
228
+ // "atan2": "<function>",
229
+ // "atanh": "<function>",
230
+ // "cbrt": "<function>",
231
+ // "ceil": "<function>",
232
+ // "clz32": "<function>",
233
+ // "cos": "<function>",
234
+ // "cosh": "<function>",
235
+ // "exp": "<function>",
236
+ // "expm1": "<function>",
237
+ // "floor": "<function>",
238
+ // "fround": "<function>",
239
+ // "hypot": "<function>",
240
+ // "imul": "<function>",
241
+ // "log": "<function>",
242
+ // "log10": "<function>",
243
+ // "log1p": "<function>",
244
+ // "log2": "<function>",
245
+ // "max": "<function>",
246
+ // "min": "<function>",
247
+ // "pow": "<function>",
248
+ // "random": "<function>",
249
+ // "round": "<function>",
250
+ // "sign": "<function>",
251
+ // "sin": "<function>",
252
+ // "sinh": "<function>",
253
+ // "sqrt": "<function>",
254
+ // "tan": "<function>",
255
+ // "tanh": "<function>",
256
+ // "trunc": "<function>",
257
+ // "HALF_PI": 1.5707963267948966,
258
+ // "TAU": 6.283185307179586,
259
+ // "clamp": "<function>",
260
+ // "deg": "<function>",
261
+ // "fit": "<function>",
262
+ // "mix": "<function>",
263
+ // "rad": "<function>",
264
+ // "step": "<function>",
265
+ // "smoothstep": "<function>",
266
+ // "aget": "<function>",
267
+ // "aset": "<function>",
268
+ // "push": "<function>",
269
+ // "concat": "<function>",
270
+ // "count": "<function>",
271
+ // "first": "<function>",
272
+ // "next": "<function>",
273
+ // "lower": "<function>",
274
+ // "upper": "<function>",
275
+ // "capitalize": "<function>",
276
+ // "pad-left": "<function>",
277
+ // "pad-right": "<function>",
278
+ // "substr": "<function>",
279
+ // "trim": "<function>",
280
+ // "regexp": "<function>",
281
+ // "re-test": "<function>",
282
+ // "re-match": "<function>",
283
+ // "replace": "<function>",
284
+ // "print": "<function>",
285
+ // "env": "<function>",
286
+ // "partial": "<function>",
287
+ // "partial2": "<function>",
288
+ // "comp": "<function>",
289
+ // "comp2": "<function>",
290
+ // "reduce": "<function>",
291
+ // "map": "<function>",
292
+ // "filter": "<function>"
293
+ // }
294
+ ```
295
+
296
+ ## Authors
297
+
298
+ - [Karsten Schmidt](https://thi.ng)
299
+
300
+ If this project contributes to an academic publication, please cite it as:
301
+
302
+ ```bibtex
303
+ @misc{thing-lispy,
304
+ title = "@thi.ng/lispy",
305
+ author = "Karsten Schmidt",
306
+ note = "https://thi.ng/lispy",
307
+ year = 2025
308
+ }
309
+ ```
310
+
311
+ ## License
312
+
313
+ &copy; 2025 Karsten Schmidt // Apache License 2.0
package/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type { ASTNode } from "@thi.ng/sexpr";
2
+ export type Env = Record<string, any>;
3
+ /**
4
+ * DSL built-ins as multi-method which directly operates on AST nodes. For
5
+ * userland extensions of the core language, it's likely more straightforward to
6
+ * define new constants/functions in {@link ENV}.
7
+ */
8
+ export declare const BUILTINS: import("@thi.ng/defmulti").MultiFn2<ASTNode[], Env, any>;
9
+ /**
10
+ * Root environment bindings. This is the default env used by
11
+ * {@link evalExpressions} and can be used to define extensions of the core
12
+ * language. Functions defined here can receive any number of arguments.
13
+ */
14
+ export declare const ENV: Env;
15
+ /**
16
+ * Parses & evaluates given S-expression(s) and returns result of last one. If
17
+ * no environment is provided, uses {@link ENV}.
18
+ *
19
+ * @param src
20
+ * @param env
21
+ */
22
+ export declare const evalSource: (src: string, env?: Env) => any;
23
+ export declare const evalArgs: (nodes: ASTNode[], env?: Env) => any[];
24
+ export declare const interpret: import("@thi.ng/defmulti").MultiFn2<ASTNode, Env, any>;
25
+ //# sourceMappingURL=index.d.ts.map
package/index.js ADDED
@@ -0,0 +1,207 @@
1
+ import { isFunction } from "@thi.ng/checks/is-function";
2
+ import { OPERATORS } from "@thi.ng/compare/ops";
3
+ import { DEFAULT, defmulti } from "@thi.ng/defmulti";
4
+ import { assert } from "@thi.ng/errors/assert";
5
+ import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
6
+ import { illegalArity } from "@thi.ng/errors/illegal-arity";
7
+ import { deg, rad } from "@thi.ng/math/angle";
8
+ import { HALF_PI, TAU } from "@thi.ng/math/api";
9
+ import { fit } from "@thi.ng/math/fit";
10
+ import { clamp } from "@thi.ng/math/interval";
11
+ import { mix } from "@thi.ng/math/mix";
12
+ import { smoothStep, step } from "@thi.ng/math/step";
13
+ import { selectKeysObj } from "@thi.ng/object-utils/select-keys";
14
+ import { parse } from "@thi.ng/sexpr/parse";
15
+ import { runtime } from "@thi.ng/sexpr/runtime";
16
+ import { capitalize, lower, upper } from "@thi.ng/strings/case";
17
+ import { KERNEL } from "./kernel.js";
18
+ const __mathOp = (fn, fn1) => (first, ...args) => {
19
+ return args.length > 0 ? (
20
+ // use a reduction for 2+ args
21
+ args.reduce((acc, x) => fn(acc, x), first)
22
+ ) : (
23
+ // apply special case unary function
24
+ fn1(first)
25
+ );
26
+ };
27
+ const __fnImpl = (args, body, env) => (...xs) => {
28
+ const $args = args.children;
29
+ if ($args.length !== xs.length) illegalArity($args.length);
30
+ const $env = $args.reduce(
31
+ (acc, a, i) => (acc[a.value] = xs[i], acc),
32
+ { ...env }
33
+ );
34
+ return body.reduce((_, x) => interpret(x, $env), void 0);
35
+ };
36
+ const BUILTINS = defmulti(
37
+ (x) => x[0].value,
38
+ {},
39
+ {
40
+ // defines as new symbol with given value, stores it in the environment and
41
+ // then returns the value, e.g. `(def magic 42)`
42
+ def: ([_, name, value], env) => env[name.value] = interpret(value, env),
43
+ // defines a new function with given name, args and body, stores it in the
44
+ // environment and returns it, e.g. `(defn madd (a b c) (+ (* a b) c))`
45
+ defn: ([_, name, args, ...body], env) => {
46
+ return env[name.value] = __fnImpl(args, body, env);
47
+ },
48
+ // anonymous function definition
49
+ fn: ([_, args, ...body], env) => __fnImpl(args, body, env),
50
+ // if/then conditional with optional else
51
+ if: ([_, test, truthy, falsy], env) => interpret(test, env) ? interpret(truthy, env) : falsy ? interpret(falsy, env) : void 0,
52
+ // create local symbol/variable bindings, e.g.
53
+ // `(let (a 1 b 2 c (+ a b)) (print a b c))`
54
+ let: ([_, args, ...body], env) => {
55
+ const pairs = args.children;
56
+ assert(
57
+ args.type === "expr" && !(pairs.length % 2),
58
+ `require pairs of key-val bindings`
59
+ );
60
+ let $env = { ...env };
61
+ for (let i = 0, n = pairs.length; i < n; i += 2) {
62
+ assert(
63
+ pairs[i].type === "sym",
64
+ `expected symbol, got: ${pairs[i].type}`
65
+ );
66
+ $env[pairs[i].value] = interpret(pairs[i + 1], $env);
67
+ }
68
+ return body.reduce((_2, x) => interpret(x, $env), void 0);
69
+ },
70
+ list: ([_, ...body], env) => evalArgs(body, env),
71
+ // add default/fallback implementation to allow calling functions defined in
72
+ // the environment (either externally or via `defn`)
73
+ [DEFAULT]: ([x, ...args], env) => {
74
+ const name = x.value;
75
+ const f = env[name];
76
+ if (f == null) illegalArgs(`${name} is not defined`);
77
+ if (!isFunction(f)) illegalArgs(`${name} is not a function`);
78
+ return f.apply(null, evalArgs(args, env));
79
+ }
80
+ }
81
+ );
82
+ const ENV = {
83
+ // prettier-ignore
84
+ "+": __mathOp((acc, x) => acc + x, (x) => x),
85
+ // prettier-ignore
86
+ "*": __mathOp((acc, x) => acc * x, (x) => x),
87
+ // prettier-ignore
88
+ "-": __mathOp((acc, x) => acc - x, (x) => -x),
89
+ // prettier-ignore
90
+ "/": __mathOp((acc, x) => acc / x, (x) => 1 / x),
91
+ // comparisons
92
+ ...OPERATORS,
93
+ // boolean logic
94
+ and: (...args) => args.every((x) => !!x),
95
+ or: (...args) => args.some((x) => !!x),
96
+ not: (x) => !x,
97
+ // binary
98
+ "<<": (x, y) => x << y,
99
+ ">>": (x, y) => x >> y,
100
+ ">>>": (x, y) => x >>> y,
101
+ "bit-and": (x, y) => x & y,
102
+ "bit-or": (x, y) => x | y,
103
+ "bit-xor": (x, y) => x ^ y,
104
+ "bit-not": (x) => ~x,
105
+ // JS-native Math
106
+ ...selectKeysObj(Math, [
107
+ "E",
108
+ "LN10",
109
+ "LN2",
110
+ "LOG10E",
111
+ "LOG2E",
112
+ "PI",
113
+ "SQRT1_2",
114
+ "SQRT2",
115
+ "abs",
116
+ "acos",
117
+ "acosh",
118
+ "asin",
119
+ "asinh",
120
+ "atan",
121
+ "atan2",
122
+ "atanh",
123
+ "cbrt",
124
+ "ceil",
125
+ "clz32",
126
+ "cos",
127
+ "cosh",
128
+ "exp",
129
+ "expm1",
130
+ "floor",
131
+ "fround",
132
+ "hypot",
133
+ "imul",
134
+ "log",
135
+ "log10",
136
+ "log1p",
137
+ "log2",
138
+ "max",
139
+ "min",
140
+ "pow",
141
+ "random",
142
+ "round",
143
+ "sign",
144
+ "sin",
145
+ "sinh",
146
+ "sqrt",
147
+ "tan",
148
+ "tanh",
149
+ "trunc"
150
+ ]),
151
+ HALF_PI,
152
+ TAU,
153
+ clamp,
154
+ deg,
155
+ fit,
156
+ mix,
157
+ rad,
158
+ step,
159
+ smoothstep: smoothStep,
160
+ aget: (arr, i) => arr[i],
161
+ aset: (arr, i, x) => arr[i] = x,
162
+ push: (list, ...x) => list.push(...x),
163
+ concat: (list, ...x) => list.concat(...x),
164
+ // returns length of first argument (presumably a list or string)
165
+ // (e.g. `(count "abc")` => 3)
166
+ count: (arg) => arg.length,
167
+ // returns first item of list/string
168
+ first: (arg) => arg[0],
169
+ // returns remaining list/string (from 2nd item onward) or undefined if
170
+ // there're no further items
171
+ next: (arg) => arg.length > 1 ? arg.slice(1) : void 0,
172
+ // strings
173
+ lower,
174
+ upper,
175
+ capitalize,
176
+ "pad-left": (x, width, fill) => x.padStart(width, fill),
177
+ "pad-right": (x, width, fill) => x.padEnd(width, fill),
178
+ substr: (x, from, to) => x.substring(from, to),
179
+ trim: (x) => x.trimEnd(),
180
+ regexp: (src, flags) => new RegExp(src, flags),
181
+ "re-test": (regexp, x) => regexp.test(x),
182
+ "re-match": (regexp, x) => regexp.exec(x),
183
+ replace: (x, regexp, replacement) => x.replace(regexp, replacement),
184
+ // misc
185
+ print: console.log,
186
+ env: () => JSON.stringify(
187
+ ENV,
188
+ (_, val) => isFunction(val) ? "<function>" : val,
189
+ 2
190
+ )
191
+ };
192
+ const evalSource = (src, env = ENV) => parse(src).children.reduce((_, x) => interpret(x, env), void 0);
193
+ const evalArgs = (nodes, env = ENV) => nodes.map((a) => interpret(a, env));
194
+ const interpret = runtime({
195
+ expr: (x, env) => BUILTINS(x.children, env),
196
+ sym: (x, env) => env[x.value],
197
+ str: (x) => x.value,
198
+ num: (x) => x.value
199
+ });
200
+ evalSource(KERNEL);
201
+ export {
202
+ BUILTINS,
203
+ ENV,
204
+ evalArgs,
205
+ evalSource,
206
+ interpret
207
+ };
package/kernel.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const KERNEL = "\n; partial functional application (currying for 1 arg)\n(defn partial (f x) (fn (y) (f x y)))\n\n; partial functional application (currying for 2 args)\n(defn partial2 (f x y) (fn (z) (f x y z)))\n\n; functional composition for 1-arg fns\n(defn comp (f g) (fn (x) (f (g x))))\n\n; functional composition for 2-arg fns\n(defn comp2 (f g) (fn (x y z) (f (g x y) z)))\n\n; list reduction\n(defn reduce (f acc xs) (if xs (reduce f (f acc (first xs)) (next xs)) acc))\n\n; list transformation (expressed as reduction)\n(defn map (f xs) (reduce (fn (acc x) (concat acc (f x))) (list) xs))\n\n; filter list with predicate function\n(defn filter (f xs) (reduce (fn (acc x) (if (f x) (concat acc x) acc)) (list) xs))\n";
2
+ //# sourceMappingURL=kernel.d.ts.map
package/kernel.js ADDED
@@ -0,0 +1,25 @@
1
+ const KERNEL = `
2
+ ; partial functional application (currying for 1 arg)
3
+ (defn partial (f x) (fn (y) (f x y)))
4
+
5
+ ; partial functional application (currying for 2 args)
6
+ (defn partial2 (f x y) (fn (z) (f x y z)))
7
+
8
+ ; functional composition for 1-arg fns
9
+ (defn comp (f g) (fn (x) (f (g x))))
10
+
11
+ ; functional composition for 2-arg fns
12
+ (defn comp2 (f g) (fn (x y z) (f (g x y) z)))
13
+
14
+ ; list reduction
15
+ (defn reduce (f acc xs) (if xs (reduce f (f acc (first xs)) (next xs)) acc))
16
+
17
+ ; list transformation (expressed as reduction)
18
+ (defn map (f xs) (reduce (fn (acc x) (concat acc (f x))) (list) xs))
19
+
20
+ ; filter list with predicate function
21
+ (defn filter (f xs) (reduce (fn (acc x) (if (f x) (concat acc x) acc)) (list) xs))
22
+ `;
23
+ export {
24
+ KERNEL
25
+ };
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@thi.ng/lispy",
3
+ "version": "0.1.0",
4
+ "description": "Lightweight, extensible, interpreted Lisp-style DSL for embedding in other projects",
5
+ "type": "module",
6
+ "module": "./index.js",
7
+ "typings": "./index.d.ts",
8
+ "sideEffects": false,
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/thi-ng/umbrella.git"
12
+ },
13
+ "homepage": "https://thi.ng/lispy",
14
+ "funding": [
15
+ {
16
+ "type": "github",
17
+ "url": "https://github.com/sponsors/postspectacular"
18
+ },
19
+ {
20
+ "type": "patreon",
21
+ "url": "https://patreon.com/thing_umbrella"
22
+ },
23
+ {
24
+ "type": "liberapay",
25
+ "url": "https://liberapay.com/thi.ng"
26
+ }
27
+ ],
28
+ "author": "Karsten Schmidt (https://thi.ng)",
29
+ "license": "Apache-2.0",
30
+ "scripts": {
31
+ "build": "yarn build:esbuild && yarn build:decl",
32
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
33
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
34
+ "clean": "bun ../../tools/src/clean-package.ts",
35
+ "doc": "typedoc --options ../../typedoc.json --out doc src/index.ts",
36
+ "doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
37
+ "pub": "yarn npm publish --access public",
38
+ "test": "bun test",
39
+ "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
40
+ },
41
+ "dependencies": {
42
+ "@thi.ng/api": "^8.11.27",
43
+ "@thi.ng/checks": "^3.7.7",
44
+ "@thi.ng/compare": "^2.4.19",
45
+ "@thi.ng/defmulti": "^3.0.67",
46
+ "@thi.ng/errors": "^2.5.33",
47
+ "@thi.ng/math": "^5.11.27",
48
+ "@thi.ng/object-utils": "^1.1.23",
49
+ "@thi.ng/sexpr": "^1.0.16",
50
+ "@thi.ng/strings": "^3.9.12"
51
+ },
52
+ "devDependencies": {
53
+ "esbuild": "^0.25.3",
54
+ "typedoc": "^0.28.3",
55
+ "typescript": "^5.8.3"
56
+ },
57
+ "keywords": [
58
+ "typescript"
59
+ ],
60
+ "publishConfig": {
61
+ "access": "public"
62
+ },
63
+ "browser": {
64
+ "process": false,
65
+ "setTimeout": false
66
+ },
67
+ "engines": {
68
+ "node": ">=18"
69
+ },
70
+ "files": [
71
+ "./*.js",
72
+ "./*.d.ts"
73
+ ],
74
+ "exports": {
75
+ ".": {
76
+ "default": "./index.js"
77
+ },
78
+ "./kernel": {
79
+ "default": "./kernel.js"
80
+ }
81
+ },
82
+ "thi.ng": {
83
+ "status": "alpha",
84
+ "year": 2025
85
+ },
86
+ "gitHead": "90ae29324123738dde721193785a6743cdf4181e\n"
87
+ }