eliscript 0.0.1
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 +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,1630 @@
|
|
|
1
|
+
;;; eliscript-expander.el --- Compile-time macros for Eliscript -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; The seed expander evaluates macro bodies through a deterministic language
|
|
6
|
+
;; subset, then walks returned forms until no user macro remains at the call
|
|
7
|
+
;; site. Macro environments are local to one compilation.
|
|
8
|
+
|
|
9
|
+
;;; Code:
|
|
10
|
+
|
|
11
|
+
(require 'cl-lib)
|
|
12
|
+
(require 'eliscript-diagnostic)
|
|
13
|
+
(require 'eliscript-form)
|
|
14
|
+
(require 'eliscript-macro-eval)
|
|
15
|
+
|
|
16
|
+
(cl-defstruct (eliscript-expander--macro
|
|
17
|
+
(:constructor eliscript-expander--macro-create))
|
|
18
|
+
name
|
|
19
|
+
parameters
|
|
20
|
+
body)
|
|
21
|
+
|
|
22
|
+
(defconst eliscript-expander--maximum-depth 100
|
|
23
|
+
"Maximum number of recursive macro expansions at one call site.")
|
|
24
|
+
|
|
25
|
+
(defvar eliscript-expander--filename nil)
|
|
26
|
+
(defvar eliscript-expander--current-span nil)
|
|
27
|
+
(defvar eliscript-expander--macro-context nil)
|
|
28
|
+
|
|
29
|
+
(defun eliscript-expander--fail (format-string &rest arguments)
|
|
30
|
+
"Signal an expansion error using FORMAT-STRING and ARGUMENTS."
|
|
31
|
+
(apply #'eliscript-diagnostic-signal
|
|
32
|
+
'eliscript-expand-error "ELI-X0001" "expansion"
|
|
33
|
+
eliscript-expander--filename eliscript-expander--current-span
|
|
34
|
+
format-string arguments))
|
|
35
|
+
|
|
36
|
+
(defun eliscript-expander--collect-symbol-names (form names)
|
|
37
|
+
"Add every source symbol reachable from FORM to NAMES."
|
|
38
|
+
(let ((value (eliscript-form-value form)))
|
|
39
|
+
(cond
|
|
40
|
+
((symbolp value) (puthash (symbol-name value) t names))
|
|
41
|
+
((consp value)
|
|
42
|
+
(eliscript-expander--collect-symbol-names (car value) names)
|
|
43
|
+
(eliscript-expander--collect-symbol-names (cdr value) names))
|
|
44
|
+
((vectorp value)
|
|
45
|
+
(mapc (lambda (item)
|
|
46
|
+
(eliscript-expander--collect-symbol-names item names))
|
|
47
|
+
(append value nil)))))
|
|
48
|
+
names)
|
|
49
|
+
|
|
50
|
+
(defun eliscript-expander--register (form environment)
|
|
51
|
+
"Validate macro definition FORM and add it to ENVIRONMENT."
|
|
52
|
+
(let* ((items (eliscript-form-value form))
|
|
53
|
+
(arguments (cdr items))
|
|
54
|
+
(eliscript-expander--current-span (eliscript-form-span form)))
|
|
55
|
+
(unless (>= (length arguments) 2)
|
|
56
|
+
(eliscript-expander--fail "defmacro expects a name and parameter list"))
|
|
57
|
+
(let ((name (eliscript-form-value (nth 0 arguments)))
|
|
58
|
+
(parameters (eliscript-form-strip (nth 1 arguments)))
|
|
59
|
+
(body (mapcar #'eliscript-form-strip (nthcdr 2 arguments))))
|
|
60
|
+
(unless (eliscript-macro-eval-symbol-p name)
|
|
61
|
+
(eliscript-expander--fail "macro name must be a symbol: %S" name))
|
|
62
|
+
(when (gethash name environment)
|
|
63
|
+
(eliscript-expander--fail "duplicate macro definition: %s" name))
|
|
64
|
+
(condition-case error-data
|
|
65
|
+
(puthash
|
|
66
|
+
name
|
|
67
|
+
(eliscript-expander--macro-create
|
|
68
|
+
:name name
|
|
69
|
+
:parameters (eliscript-macro-eval-parse-parameters parameters)
|
|
70
|
+
:body body)
|
|
71
|
+
environment)
|
|
72
|
+
(eliscript-macro-eval-error
|
|
73
|
+
(eliscript-expander--fail "%s" (cadr error-data)))))))
|
|
74
|
+
|
|
75
|
+
(defun eliscript-expander--invoke (definition arguments depth)
|
|
76
|
+
"Invoke macro DEFINITION with raw ARGUMENTS at expansion DEPTH."
|
|
77
|
+
(when (>= depth eliscript-expander--maximum-depth)
|
|
78
|
+
(eliscript-expander--fail
|
|
79
|
+
"macro expansion exceeded %d steps near %s"
|
|
80
|
+
eliscript-expander--maximum-depth
|
|
81
|
+
(eliscript-expander--macro-name definition)))
|
|
82
|
+
(condition-case error-data
|
|
83
|
+
(eliscript-macro-eval-run
|
|
84
|
+
(eliscript-expander--macro-name definition)
|
|
85
|
+
(eliscript-expander--macro-parameters definition)
|
|
86
|
+
(eliscript-expander--macro-body definition)
|
|
87
|
+
(mapcar #'eliscript-form-strip arguments)
|
|
88
|
+
eliscript-expander--macro-context)
|
|
89
|
+
(eliscript-macro-eval-error
|
|
90
|
+
(eliscript-expander--fail
|
|
91
|
+
"macro %s failed: %s"
|
|
92
|
+
(eliscript-expander--macro-name definition)
|
|
93
|
+
(cadr error-data)))
|
|
94
|
+
(error
|
|
95
|
+
(eliscript-expander--fail
|
|
96
|
+
"macro %s failed: %s"
|
|
97
|
+
(eliscript-expander--macro-name definition)
|
|
98
|
+
(error-message-string error-data)))))
|
|
99
|
+
|
|
100
|
+
(defun eliscript-expander--expand-sequence (forms environment depth)
|
|
101
|
+
"Expand expression FORMS in ENVIRONMENT at DEPTH."
|
|
102
|
+
(mapcar (lambda (form)
|
|
103
|
+
(eliscript-expander--expand-expression form environment depth))
|
|
104
|
+
forms))
|
|
105
|
+
|
|
106
|
+
(defun eliscript-expander--expand-binding (binding environment depth)
|
|
107
|
+
"Expand initializer in one let BINDING using ENVIRONMENT at DEPTH."
|
|
108
|
+
(let ((value (eliscript-form-value binding)))
|
|
109
|
+
(if (and (consp value) (proper-list-p value))
|
|
110
|
+
(eliscript-form-inherit
|
|
111
|
+
(cons (car value)
|
|
112
|
+
(eliscript-expander--expand-sequence
|
|
113
|
+
(cdr value) environment depth))
|
|
114
|
+
binding)
|
|
115
|
+
binding)))
|
|
116
|
+
|
|
117
|
+
(defun eliscript-expander--expand-bindings (arguments environment depth)
|
|
118
|
+
"Expand let ARGUMENTS in ENVIRONMENT at DEPTH."
|
|
119
|
+
(if (null arguments)
|
|
120
|
+
arguments
|
|
121
|
+
(let* ((bindings-form (car arguments))
|
|
122
|
+
(bindings (eliscript-form-value bindings-form)))
|
|
123
|
+
(cons
|
|
124
|
+
(eliscript-form-inherit
|
|
125
|
+
(if (proper-list-p bindings)
|
|
126
|
+
(mapcar (lambda (binding)
|
|
127
|
+
(eliscript-expander--expand-binding
|
|
128
|
+
binding environment depth))
|
|
129
|
+
bindings)
|
|
130
|
+
bindings)
|
|
131
|
+
bindings-form)
|
|
132
|
+
(eliscript-expander--expand-sequence
|
|
133
|
+
(cdr arguments) environment depth)))))
|
|
134
|
+
|
|
135
|
+
(defun eliscript-expander--expand-assignment (arguments environment depth)
|
|
136
|
+
"Expand assignment values in ARGUMENTS using ENVIRONMENT at DEPTH."
|
|
137
|
+
(cl-loop for argument in arguments
|
|
138
|
+
for index from 0
|
|
139
|
+
collect (if (cl-oddp index)
|
|
140
|
+
(eliscript-expander--expand-expression
|
|
141
|
+
argument environment depth)
|
|
142
|
+
argument)))
|
|
143
|
+
|
|
144
|
+
(defun eliscript-expander--expand-object (arguments environment depth)
|
|
145
|
+
"Expand object ARGUMENTS in ENVIRONMENT at DEPTH, preserving literal keys."
|
|
146
|
+
(cl-loop for argument in arguments
|
|
147
|
+
for index from 0
|
|
148
|
+
collect
|
|
149
|
+
(if (let ((value (eliscript-form-value argument)))
|
|
150
|
+
(or (cl-oddp index)
|
|
151
|
+
(not (or (keywordp value)
|
|
152
|
+
(stringp value)
|
|
153
|
+
(symbolp value)))))
|
|
154
|
+
(eliscript-expander--expand-expression
|
|
155
|
+
argument environment depth)
|
|
156
|
+
argument)))
|
|
157
|
+
|
|
158
|
+
(defun eliscript-expander--expand-property-call (arguments environment depth)
|
|
159
|
+
"Expand property call ARGUMENTS in ENVIRONMENT at DEPTH."
|
|
160
|
+
(cl-loop for argument in arguments
|
|
161
|
+
for index from 0
|
|
162
|
+
collect
|
|
163
|
+
(if (and (= index 1)
|
|
164
|
+
(let ((value (eliscript-form-value argument)))
|
|
165
|
+
(or (keywordp value) (stringp value))))
|
|
166
|
+
argument
|
|
167
|
+
(eliscript-expander--expand-expression
|
|
168
|
+
argument environment depth))))
|
|
169
|
+
|
|
170
|
+
(defun eliscript-expander--expand-cond (clauses environment depth)
|
|
171
|
+
"Expand COND CLAUSES in ENVIRONMENT at DEPTH."
|
|
172
|
+
(mapcar
|
|
173
|
+
(lambda (clause)
|
|
174
|
+
(let ((value (eliscript-form-value clause)))
|
|
175
|
+
(if (proper-list-p value)
|
|
176
|
+
(eliscript-form-inherit
|
|
177
|
+
(eliscript-expander--expand-sequence value environment depth)
|
|
178
|
+
clause)
|
|
179
|
+
clause)))
|
|
180
|
+
clauses))
|
|
181
|
+
|
|
182
|
+
(defun eliscript-expander--expand-try-clause (clause environment depth)
|
|
183
|
+
"Expand one try CLAUSE in ENVIRONMENT at DEPTH."
|
|
184
|
+
(let* ((value (eliscript-form-value clause))
|
|
185
|
+
(operator (and (proper-list-p value)
|
|
186
|
+
value
|
|
187
|
+
(eliscript-form-value (car value))))
|
|
188
|
+
(arguments (and (proper-list-p value) (cdr value))))
|
|
189
|
+
(pcase operator
|
|
190
|
+
('catch
|
|
191
|
+
(if arguments
|
|
192
|
+
(eliscript-form-inherit
|
|
193
|
+
(cons (car value)
|
|
194
|
+
(cons (car arguments)
|
|
195
|
+
(eliscript-expander--expand-sequence
|
|
196
|
+
(cdr arguments) environment depth)))
|
|
197
|
+
clause)
|
|
198
|
+
clause))
|
|
199
|
+
('finally
|
|
200
|
+
(eliscript-form-inherit
|
|
201
|
+
(cons (car value)
|
|
202
|
+
(eliscript-expander--expand-sequence
|
|
203
|
+
arguments environment depth))
|
|
204
|
+
clause))
|
|
205
|
+
(_ (eliscript-expander--expand-expression
|
|
206
|
+
clause environment depth)))))
|
|
207
|
+
|
|
208
|
+
(defun eliscript-expander--generated-form (value)
|
|
209
|
+
"Wrap generated VALUE at the current declaration location."
|
|
210
|
+
(eliscript-form-wrap value eliscript-expander--current-span))
|
|
211
|
+
|
|
212
|
+
(defun eliscript-expander--generated-form-at (value form)
|
|
213
|
+
"Wrap generated VALUE at FORM's source location."
|
|
214
|
+
(eliscript-form-wrap
|
|
215
|
+
value
|
|
216
|
+
(or (eliscript-form-span form) eliscript-expander--current-span)))
|
|
217
|
+
|
|
218
|
+
(defun eliscript-expander--thread-step (value-form step-form position)
|
|
219
|
+
"Insert VALUE-FORM into STEP-FORM at thread POSITION."
|
|
220
|
+
(let ((step (eliscript-form-value step-form)))
|
|
221
|
+
(cond
|
|
222
|
+
((eliscript-macro-eval-symbol-p step)
|
|
223
|
+
(eliscript-expander--generated-form-at
|
|
224
|
+
(list step-form value-form) step-form))
|
|
225
|
+
((and (proper-list-p step) step)
|
|
226
|
+
(eliscript-expander--generated-form-at
|
|
227
|
+
(if (eq position 'first)
|
|
228
|
+
(cons (car step) (cons value-form (cdr step)))
|
|
229
|
+
(append step (list value-form)))
|
|
230
|
+
step-form))
|
|
231
|
+
(t
|
|
232
|
+
(eliscript-expander--fail
|
|
233
|
+
"thread step must be a symbol or non-empty list: %S"
|
|
234
|
+
(eliscript-form-strip step-form))))))
|
|
235
|
+
|
|
236
|
+
(defun eliscript-expander--desugar-thread (form-name arguments position)
|
|
237
|
+
"Return FORM-NAME ARGUMENTS threaded at POSITION."
|
|
238
|
+
(unless arguments
|
|
239
|
+
(eliscript-expander--fail "%s expects an initial expression" form-name))
|
|
240
|
+
(let ((result (car arguments)))
|
|
241
|
+
(dolist (step-form (cdr arguments) result)
|
|
242
|
+
(setq result
|
|
243
|
+
(eliscript-expander--thread-step
|
|
244
|
+
result step-form position)))))
|
|
245
|
+
|
|
246
|
+
(defun eliscript-expander--fresh-name-form (prefix)
|
|
247
|
+
"Return a capture-safe generated symbol using PREFIX."
|
|
248
|
+
(eliscript-expander--generated-form
|
|
249
|
+
(eliscript-macro-eval--allocate-name
|
|
250
|
+
(eliscript-macro-eval--make-scope
|
|
251
|
+
nil eliscript-expander--macro-context)
|
|
252
|
+
prefix)))
|
|
253
|
+
|
|
254
|
+
(defun eliscript-expander--let-form (name-form value-form body-form)
|
|
255
|
+
"Return a generated let binding NAME-FORM to VALUE-FORM around BODY-FORM."
|
|
256
|
+
(let* ((binding
|
|
257
|
+
(eliscript-expander--generated-form-at
|
|
258
|
+
(list name-form value-form) value-form))
|
|
259
|
+
(bindings
|
|
260
|
+
(eliscript-expander--generated-form-at
|
|
261
|
+
(list binding) value-form)))
|
|
262
|
+
(eliscript-expander--generated-form-at
|
|
263
|
+
(list
|
|
264
|
+
(eliscript-expander--generated-form-at 'let value-form)
|
|
265
|
+
bindings
|
|
266
|
+
body-form)
|
|
267
|
+
value-form)))
|
|
268
|
+
|
|
269
|
+
(defun eliscript-expander--desugar-as-thread (arguments)
|
|
270
|
+
"Return the sequential binding represented by as-> ARGUMENTS."
|
|
271
|
+
(when (< (length arguments) 3)
|
|
272
|
+
(eliscript-expander--fail
|
|
273
|
+
"as-> expects an initial expression, binding name, and at least one form"))
|
|
274
|
+
(let ((initial-form (nth 0 arguments))
|
|
275
|
+
(name-form (nth 1 arguments))
|
|
276
|
+
(step-forms (nthcdr 2 arguments)))
|
|
277
|
+
(unless (eliscript-macro-eval-symbol-p
|
|
278
|
+
(eliscript-form-value name-form))
|
|
279
|
+
(eliscript-expander--fail
|
|
280
|
+
"as-> binding name must be a symbol: %S"
|
|
281
|
+
(eliscript-form-strip name-form)))
|
|
282
|
+
(let ((body (car (last step-forms))))
|
|
283
|
+
(dolist (step-form (reverse (butlast step-forms)))
|
|
284
|
+
(setq body
|
|
285
|
+
(eliscript-expander--let-form
|
|
286
|
+
name-form step-form body)))
|
|
287
|
+
(eliscript-expander--let-form name-form initial-form body))))
|
|
288
|
+
|
|
289
|
+
(defun eliscript-expander--desugar-cond-thread
|
|
290
|
+
(form-name arguments position)
|
|
291
|
+
"Return conditional FORM-NAME ARGUMENTS threaded at POSITION."
|
|
292
|
+
(unless arguments
|
|
293
|
+
(eliscript-expander--fail "%s expects an initial expression" form-name))
|
|
294
|
+
(let ((clauses (cdr arguments)))
|
|
295
|
+
(when (= (% (length clauses) 2) 1)
|
|
296
|
+
(eliscript-expander--fail
|
|
297
|
+
"%s expects test and step pairs" form-name))
|
|
298
|
+
(if (null clauses)
|
|
299
|
+
(car arguments)
|
|
300
|
+
(let ((name-form (eliscript-expander--fresh-name-form "thread"))
|
|
301
|
+
(body nil)
|
|
302
|
+
(index (- (length arguments) 2)))
|
|
303
|
+
(setq body name-form)
|
|
304
|
+
(while (>= index 1)
|
|
305
|
+
(let ((test-form (nth index arguments))
|
|
306
|
+
(step-form (nth (1+ index) arguments)))
|
|
307
|
+
(setq body
|
|
308
|
+
(eliscript-expander--let-form
|
|
309
|
+
name-form
|
|
310
|
+
(eliscript-expander--generated-form
|
|
311
|
+
(list
|
|
312
|
+
(eliscript-expander--generated-form 'if)
|
|
313
|
+
test-form
|
|
314
|
+
(eliscript-expander--thread-step
|
|
315
|
+
name-form step-form position)
|
|
316
|
+
name-form))
|
|
317
|
+
body))
|
|
318
|
+
(setq index (- index 2))))
|
|
319
|
+
(eliscript-expander--let-form
|
|
320
|
+
name-form (car arguments) body)))))
|
|
321
|
+
|
|
322
|
+
(defun eliscript-expander--desugar-some-thread
|
|
323
|
+
(form-name arguments position)
|
|
324
|
+
"Return nil-short-circuiting FORM-NAME ARGUMENTS threaded at POSITION."
|
|
325
|
+
(unless arguments
|
|
326
|
+
(eliscript-expander--fail "%s expects an initial expression" form-name))
|
|
327
|
+
(if (null (cdr arguments))
|
|
328
|
+
(car arguments)
|
|
329
|
+
(let ((name-form (eliscript-expander--fresh-name-form "thread"))
|
|
330
|
+
(body nil))
|
|
331
|
+
(setq body name-form)
|
|
332
|
+
(dolist (step-form (reverse (cdr arguments)))
|
|
333
|
+
(setq body
|
|
334
|
+
(eliscript-expander--generated-form
|
|
335
|
+
(list
|
|
336
|
+
(eliscript-expander--generated-form 'if)
|
|
337
|
+
(eliscript-expander--conditional-test name-form t)
|
|
338
|
+
(eliscript-expander--let-form
|
|
339
|
+
name-form
|
|
340
|
+
(eliscript-expander--thread-step
|
|
341
|
+
name-form step-form position)
|
|
342
|
+
body)
|
|
343
|
+
(eliscript-expander--generated-form nil)))))
|
|
344
|
+
(eliscript-expander--let-form
|
|
345
|
+
name-form (car arguments) body))))
|
|
346
|
+
|
|
347
|
+
(defun eliscript-expander--conditional-binding (form-name binding-form)
|
|
348
|
+
"Validate FORM-NAME BINDING-FORM and return its name and initializer."
|
|
349
|
+
(let ((binding (eliscript-form-value binding-form)))
|
|
350
|
+
(unless (and (proper-list-p binding) (= (length binding) 2))
|
|
351
|
+
(eliscript-expander--fail
|
|
352
|
+
"%s binding must contain a name and initializer: %S"
|
|
353
|
+
form-name (eliscript-form-strip binding-form)))
|
|
354
|
+
(unless (eliscript-macro-eval-symbol-p
|
|
355
|
+
(eliscript-form-value (car binding)))
|
|
356
|
+
(eliscript-expander--fail
|
|
357
|
+
"%s binding name must be a symbol: %S"
|
|
358
|
+
form-name (eliscript-form-strip (car binding))))
|
|
359
|
+
binding))
|
|
360
|
+
|
|
361
|
+
(defun eliscript-expander--conditional-test (name-form some-p)
|
|
362
|
+
"Return the generated truth test for NAME-FORM and SOME-P."
|
|
363
|
+
(if some-p
|
|
364
|
+
(eliscript-expander--generated-form
|
|
365
|
+
(list
|
|
366
|
+
(eliscript-expander--generated-form 'not)
|
|
367
|
+
(eliscript-expander--generated-form
|
|
368
|
+
(list
|
|
369
|
+
(eliscript-expander--generated-form 'nil?)
|
|
370
|
+
name-form))))
|
|
371
|
+
name-form))
|
|
372
|
+
|
|
373
|
+
(defun eliscript-expander--desugar-if-binding
|
|
374
|
+
(form-name arguments some-p)
|
|
375
|
+
"Return the if binding FORM-NAME represented by ARGUMENTS and SOME-P."
|
|
376
|
+
(unless (<= 2 (length arguments) 3)
|
|
377
|
+
(eliscript-expander--fail
|
|
378
|
+
"%s expects a binding, then form, and optional else form" form-name))
|
|
379
|
+
(let* ((binding
|
|
380
|
+
(eliscript-expander--conditional-binding
|
|
381
|
+
form-name (nth 0 arguments)))
|
|
382
|
+
(name-form (nth 0 binding))
|
|
383
|
+
(initializer-form (nth 1 binding))
|
|
384
|
+
(then-form (nth 1 arguments))
|
|
385
|
+
(else-form
|
|
386
|
+
(or (nth 2 arguments)
|
|
387
|
+
(eliscript-expander--generated-form nil)))
|
|
388
|
+
(if-form
|
|
389
|
+
(eliscript-expander--generated-form
|
|
390
|
+
(list
|
|
391
|
+
(eliscript-expander--generated-form 'if)
|
|
392
|
+
(eliscript-expander--conditional-test name-form some-p)
|
|
393
|
+
then-form
|
|
394
|
+
else-form))))
|
|
395
|
+
(eliscript-expander--let-form name-form initializer-form if-form)))
|
|
396
|
+
|
|
397
|
+
(defun eliscript-expander--desugar-when-binding
|
|
398
|
+
(form-name arguments some-p)
|
|
399
|
+
"Return the conditional body FORM-NAME represented by ARGUMENTS and SOME-P."
|
|
400
|
+
(when (< (length arguments) 2)
|
|
401
|
+
(eliscript-expander--fail
|
|
402
|
+
"%s expects a binding and at least one body form" form-name))
|
|
403
|
+
(let* ((binding
|
|
404
|
+
(eliscript-expander--conditional-binding
|
|
405
|
+
form-name (car arguments)))
|
|
406
|
+
(name-form (nth 0 binding))
|
|
407
|
+
(initializer-form (nth 1 binding))
|
|
408
|
+
(body-form
|
|
409
|
+
(eliscript-expander--generated-form
|
|
410
|
+
(cons
|
|
411
|
+
(eliscript-expander--generated-form 'progn)
|
|
412
|
+
(cdr arguments))))
|
|
413
|
+
(if-form
|
|
414
|
+
(eliscript-expander--generated-form
|
|
415
|
+
(list
|
|
416
|
+
(eliscript-expander--generated-form 'if)
|
|
417
|
+
(eliscript-expander--conditional-test name-form some-p)
|
|
418
|
+
body-form
|
|
419
|
+
(eliscript-expander--generated-form nil)))))
|
|
420
|
+
(eliscript-expander--let-form name-form initializer-form if-form)))
|
|
421
|
+
|
|
422
|
+
(defun eliscript-expander--quoted-value (form)
|
|
423
|
+
"Return FORM as generated quoted data."
|
|
424
|
+
(eliscript-expander--generated-form-at
|
|
425
|
+
(list (eliscript-expander--generated-form-at 'quote form) form)
|
|
426
|
+
form))
|
|
427
|
+
|
|
428
|
+
(defun eliscript-expander--case-constants (match-form)
|
|
429
|
+
"Return the constant forms represented by MATCH-FORM."
|
|
430
|
+
(let ((value (eliscript-form-value match-form)))
|
|
431
|
+
(if (and (consp value) (proper-list-p value)) value (list match-form))))
|
|
432
|
+
|
|
433
|
+
(defun eliscript-expander--desugar-case (arguments)
|
|
434
|
+
"Return value-dispatch code represented by case ARGUMENTS."
|
|
435
|
+
(when (< (length arguments) 3)
|
|
436
|
+
(eliscript-expander--fail
|
|
437
|
+
"case expects a dispatch expression and at least one match/result pair"))
|
|
438
|
+
(let* ((dispatch-form (car arguments))
|
|
439
|
+
(remaining (cdr arguments))
|
|
440
|
+
(default-form
|
|
441
|
+
(when (= (% (length remaining) 2) 1)
|
|
442
|
+
(car (last remaining))))
|
|
443
|
+
(pairs
|
|
444
|
+
(if default-form (butlast remaining) remaining))
|
|
445
|
+
(dispatch-name (eliscript-expander--fresh-name-form "case-value"))
|
|
446
|
+
(seen (make-hash-table :test #'equal))
|
|
447
|
+
cond-items)
|
|
448
|
+
(while pairs
|
|
449
|
+
(let* ((match-form (pop pairs))
|
|
450
|
+
(result-form (pop pairs))
|
|
451
|
+
(constants (eliscript-expander--case-constants match-form))
|
|
452
|
+
comparisons)
|
|
453
|
+
(dolist (constant constants)
|
|
454
|
+
(let ((identity (prin1-to-string (eliscript-form-strip constant))))
|
|
455
|
+
(when (gethash identity seen)
|
|
456
|
+
(eliscript-expander--fail
|
|
457
|
+
"case declares duplicate match constant: %s" identity))
|
|
458
|
+
(puthash identity t seen))
|
|
459
|
+
(push
|
|
460
|
+
(eliscript-expander--generated-form-at
|
|
461
|
+
(list
|
|
462
|
+
(eliscript-expander--generated-form-at 'equal constant)
|
|
463
|
+
dispatch-name
|
|
464
|
+
(eliscript-expander--quoted-value constant))
|
|
465
|
+
constant)
|
|
466
|
+
comparisons))
|
|
467
|
+
(setq comparisons (nreverse comparisons))
|
|
468
|
+
(setq cond-items
|
|
469
|
+
(append
|
|
470
|
+
cond-items
|
|
471
|
+
(list
|
|
472
|
+
(eliscript-expander--generated-form-at
|
|
473
|
+
(list
|
|
474
|
+
(if (cdr comparisons)
|
|
475
|
+
(eliscript-expander--generated-form-at
|
|
476
|
+
(cons
|
|
477
|
+
(eliscript-expander--generated-form-at 'or match-form)
|
|
478
|
+
comparisons)
|
|
479
|
+
match-form)
|
|
480
|
+
(car comparisons))
|
|
481
|
+
result-form)
|
|
482
|
+
match-form))))))
|
|
483
|
+
(setq cond-items
|
|
484
|
+
(append
|
|
485
|
+
cond-items
|
|
486
|
+
(list
|
|
487
|
+
(eliscript-expander--generated-form
|
|
488
|
+
(list
|
|
489
|
+
(eliscript-expander--generated-form t)
|
|
490
|
+
(or default-form
|
|
491
|
+
(eliscript-expander--generated-form nil)))))))
|
|
492
|
+
(eliscript-expander--let-form
|
|
493
|
+
dispatch-name
|
|
494
|
+
dispatch-form
|
|
495
|
+
(eliscript-expander--generated-form
|
|
496
|
+
(cons (eliscript-expander--generated-form 'cond) cond-items)))))
|
|
497
|
+
|
|
498
|
+
(defun eliscript-expander--condp-redirect-p (form)
|
|
499
|
+
"Return non-nil when FORM is the condp :>> marker."
|
|
500
|
+
(eq (eliscript-form-value form) :>>))
|
|
501
|
+
|
|
502
|
+
(defun eliscript-expander--condp-no-match-form ()
|
|
503
|
+
"Return the generated condp no-match failure."
|
|
504
|
+
(eliscript-expander--generated-form
|
|
505
|
+
(list
|
|
506
|
+
(eliscript-expander--generated-form 'throw)
|
|
507
|
+
(eliscript-expander--generated-form
|
|
508
|
+
(list
|
|
509
|
+
(eliscript-expander--generated-form 'new)
|
|
510
|
+
(eliscript-expander--generated-form 'globalThis/TypeError)
|
|
511
|
+
(eliscript-expander--generated-form
|
|
512
|
+
"condp found no matching clause"))))))
|
|
513
|
+
|
|
514
|
+
(defun eliscript-expander--desugar-condp (arguments)
|
|
515
|
+
"Return predicate-dispatch code represented by condp ARGUMENTS."
|
|
516
|
+
(when (< (length arguments) 4)
|
|
517
|
+
(eliscript-expander--fail
|
|
518
|
+
"condp expects a predicate, dispatch expression, and at least one clause"))
|
|
519
|
+
(let ((predicate-form (nth 0 arguments))
|
|
520
|
+
(dispatch-form (nth 1 arguments))
|
|
521
|
+
(remaining (nthcdr 2 arguments))
|
|
522
|
+
clauses
|
|
523
|
+
default-form)
|
|
524
|
+
(while (> (length remaining) 1)
|
|
525
|
+
(let ((test-form (pop remaining))
|
|
526
|
+
(result-or-marker (pop remaining)))
|
|
527
|
+
(if (eliscript-expander--condp-redirect-p result-or-marker)
|
|
528
|
+
(progn
|
|
529
|
+
(unless remaining
|
|
530
|
+
(eliscript-expander--fail
|
|
531
|
+
"condp :>> clause requires a result function"))
|
|
532
|
+
(push (list test-form (pop remaining) t) clauses))
|
|
533
|
+
(push (list test-form result-or-marker nil) clauses))))
|
|
534
|
+
(setq default-form (car remaining))
|
|
535
|
+
(setq clauses (nreverse clauses))
|
|
536
|
+
(unless clauses
|
|
537
|
+
(eliscript-expander--fail
|
|
538
|
+
"condp expects a predicate, dispatch expression, and at least one clause"))
|
|
539
|
+
(let ((predicate-name
|
|
540
|
+
(eliscript-expander--fresh-name-form "condp-predicate"))
|
|
541
|
+
(dispatch-name
|
|
542
|
+
(eliscript-expander--fresh-name-form "condp-value"))
|
|
543
|
+
(body (or default-form
|
|
544
|
+
(eliscript-expander--condp-no-match-form))))
|
|
545
|
+
(dolist (clause (reverse clauses))
|
|
546
|
+
(let* ((test-form (nth 0 clause))
|
|
547
|
+
(result-form (nth 1 clause))
|
|
548
|
+
(redirect-p (nth 2 clause))
|
|
549
|
+
(match-name
|
|
550
|
+
(eliscript-expander--fresh-name-form "condp-result"))
|
|
551
|
+
(predicate-call
|
|
552
|
+
(eliscript-expander--generated-form-at
|
|
553
|
+
(list
|
|
554
|
+
(eliscript-expander--generated-form-at 'funcall test-form)
|
|
555
|
+
predicate-name
|
|
556
|
+
test-form
|
|
557
|
+
dispatch-name)
|
|
558
|
+
test-form))
|
|
559
|
+
(selected
|
|
560
|
+
(if redirect-p
|
|
561
|
+
(eliscript-expander--generated-form-at
|
|
562
|
+
(list
|
|
563
|
+
(eliscript-expander--generated-form-at
|
|
564
|
+
'funcall result-form)
|
|
565
|
+
result-form
|
|
566
|
+
match-name)
|
|
567
|
+
result-form)
|
|
568
|
+
result-form))
|
|
569
|
+
(if-form
|
|
570
|
+
(eliscript-expander--generated-form-at
|
|
571
|
+
(list
|
|
572
|
+
(eliscript-expander--generated-form-at 'if test-form)
|
|
573
|
+
match-name
|
|
574
|
+
selected
|
|
575
|
+
body)
|
|
576
|
+
test-form)))
|
|
577
|
+
(setq body
|
|
578
|
+
(eliscript-expander--let-form
|
|
579
|
+
match-name predicate-call if-form))))
|
|
580
|
+
(eliscript-expander--let-form
|
|
581
|
+
predicate-name
|
|
582
|
+
predicate-form
|
|
583
|
+
(eliscript-expander--let-form
|
|
584
|
+
dispatch-name dispatch-form body)))))
|
|
585
|
+
|
|
586
|
+
(defun eliscript-expander--multi-arity-clause-p (form)
|
|
587
|
+
"Return non-nil when FORM starts with a function parameter list."
|
|
588
|
+
(let* ((items (eliscript-form-value form))
|
|
589
|
+
(parameters (and (consp items)
|
|
590
|
+
(eliscript-form-value (car items)))))
|
|
591
|
+
(and (consp items)
|
|
592
|
+
(proper-list-p items)
|
|
593
|
+
(proper-list-p parameters)
|
|
594
|
+
(not (and parameters
|
|
595
|
+
(eq (eliscript-form-value (car parameters))
|
|
596
|
+
'hash-map))))))
|
|
597
|
+
|
|
598
|
+
(defun eliscript-expander--multi-arity-layout (parameters)
|
|
599
|
+
"Return required and variadic arity metadata for PARAMETERS."
|
|
600
|
+
(let ((items (eliscript-form-value parameters))
|
|
601
|
+
(required 0)
|
|
602
|
+
variadic)
|
|
603
|
+
(unless (proper-list-p items)
|
|
604
|
+
(eliscript-expander--fail
|
|
605
|
+
"multi-arity clause parameters must be a list"))
|
|
606
|
+
(while items
|
|
607
|
+
(let* ((parameter (pop items))
|
|
608
|
+
(eliscript-expander--current-span
|
|
609
|
+
(or (eliscript-form-span parameter)
|
|
610
|
+
eliscript-expander--current-span))
|
|
611
|
+
(name (eliscript-form-value parameter)))
|
|
612
|
+
(cond
|
|
613
|
+
((memq name '(&optional &body))
|
|
614
|
+
(eliscript-expander--fail
|
|
615
|
+
"multi-arity clauses do not support %s" name))
|
|
616
|
+
((eq name '&rest)
|
|
617
|
+
(unless (and (= (length items) 1)
|
|
618
|
+
(eliscript-macro-eval-symbol-p
|
|
619
|
+
(eliscript-form-value (car items))))
|
|
620
|
+
(eliscript-expander--fail
|
|
621
|
+
"multi-arity clause requires one final &rest symbol"))
|
|
622
|
+
(setq variadic t
|
|
623
|
+
items nil))
|
|
624
|
+
(t (setq required (1+ required))))))
|
|
625
|
+
(list :required required :variadic variadic)))
|
|
626
|
+
|
|
627
|
+
(defun eliscript-expander--multi-arity-clauses (forms)
|
|
628
|
+
"Validate multi-arity clause FORMS and return normalized metadata."
|
|
629
|
+
(when (< (length forms) 2)
|
|
630
|
+
(eliscript-expander--fail
|
|
631
|
+
"multi-arity function requires at least two clauses"))
|
|
632
|
+
(let ((fixed (make-hash-table :test #'eql))
|
|
633
|
+
fixed-arities
|
|
634
|
+
variadic
|
|
635
|
+
clauses)
|
|
636
|
+
(dolist (form forms)
|
|
637
|
+
(let* ((eliscript-expander--current-span
|
|
638
|
+
(or (eliscript-form-span form)
|
|
639
|
+
eliscript-expander--current-span))
|
|
640
|
+
(items (eliscript-form-value form)))
|
|
641
|
+
(unless (and (consp items)
|
|
642
|
+
(proper-list-p items)
|
|
643
|
+
(cdr items)
|
|
644
|
+
(proper-list-p (eliscript-form-value (car items))))
|
|
645
|
+
(eliscript-expander--fail
|
|
646
|
+
"multi-arity clause requires a parameter list and body"))
|
|
647
|
+
(let* ((layout
|
|
648
|
+
(eliscript-expander--multi-arity-layout (car items)))
|
|
649
|
+
(required (plist-get layout :required))
|
|
650
|
+
(variadic-p (plist-get layout :variadic))
|
|
651
|
+
(clause
|
|
652
|
+
(list :form form
|
|
653
|
+
:parameters (car items)
|
|
654
|
+
:body (cdr items)
|
|
655
|
+
:required required
|
|
656
|
+
:variadic variadic-p)))
|
|
657
|
+
(if variadic-p
|
|
658
|
+
(if variadic
|
|
659
|
+
(eliscript-expander--fail
|
|
660
|
+
"multi-arity function declares more than one variadic clause")
|
|
661
|
+
(setq variadic clause))
|
|
662
|
+
(when (gethash required fixed)
|
|
663
|
+
(eliscript-expander--fail
|
|
664
|
+
"multi-arity function declares duplicate fixed arity: %d"
|
|
665
|
+
required))
|
|
666
|
+
(puthash required t fixed)
|
|
667
|
+
(push required fixed-arities))
|
|
668
|
+
(push clause clauses))))
|
|
669
|
+
(when variadic
|
|
670
|
+
(let ((minimum (plist-get variadic :required)))
|
|
671
|
+
(dolist (arity (nreverse fixed-arities))
|
|
672
|
+
(when (>= arity minimum)
|
|
673
|
+
(eliscript-expander--fail
|
|
674
|
+
"multi-arity fixed arity %d is unreachable behind variadic arity %d"
|
|
675
|
+
arity minimum)))))
|
|
676
|
+
(nreverse clauses)))
|
|
677
|
+
|
|
678
|
+
(defun eliscript-expander--desugar-multi-arity
|
|
679
|
+
(operator-form name-form clause-forms source-form)
|
|
680
|
+
"Return one function dispatching CLAUSE-FORMS by argument count."
|
|
681
|
+
(let* ((operator (eliscript-form-value operator-form))
|
|
682
|
+
(clauses
|
|
683
|
+
(eliscript-expander--multi-arity-clauses clause-forms))
|
|
684
|
+
(arguments-name
|
|
685
|
+
(eliscript-expander--fresh-name-form "arity-arguments"))
|
|
686
|
+
(arity-name
|
|
687
|
+
(eliscript-expander--fresh-name-form "arity-count"))
|
|
688
|
+
(branch-operator (if (memq operator '(async defasync)) 'async 'lambda))
|
|
689
|
+
(label
|
|
690
|
+
(if name-form
|
|
691
|
+
(format "%s" (eliscript-form-strip name-form))
|
|
692
|
+
"anonymous function"))
|
|
693
|
+
cond-clauses)
|
|
694
|
+
(dolist (clause clauses)
|
|
695
|
+
(let* ((clause-form (plist-get clause :form))
|
|
696
|
+
(required (plist-get clause :required))
|
|
697
|
+
(comparison
|
|
698
|
+
(eliscript-expander--generated-form-at
|
|
699
|
+
(list
|
|
700
|
+
(eliscript-expander--generated-form-at
|
|
701
|
+
(if (plist-get clause :variadic) '>= '=) clause-form)
|
|
702
|
+
arity-name
|
|
703
|
+
(eliscript-expander--generated-form-at required clause-form))
|
|
704
|
+
clause-form))
|
|
705
|
+
(branch
|
|
706
|
+
(eliscript-expander--generated-form-at
|
|
707
|
+
(append
|
|
708
|
+
(list
|
|
709
|
+
(eliscript-expander--generated-form-at
|
|
710
|
+
branch-operator clause-form)
|
|
711
|
+
(plist-get clause :parameters))
|
|
712
|
+
(plist-get clause :body))
|
|
713
|
+
clause-form))
|
|
714
|
+
(selected
|
|
715
|
+
(eliscript-expander--generated-form-at
|
|
716
|
+
(list
|
|
717
|
+
(eliscript-expander--generated-form-at 'apply clause-form)
|
|
718
|
+
branch
|
|
719
|
+
arguments-name)
|
|
720
|
+
clause-form)))
|
|
721
|
+
(push
|
|
722
|
+
(eliscript-expander--generated-form-at
|
|
723
|
+
(list comparison selected) clause-form)
|
|
724
|
+
cond-clauses)))
|
|
725
|
+
(setq cond-clauses (nreverse cond-clauses))
|
|
726
|
+
(setq cond-clauses
|
|
727
|
+
(append
|
|
728
|
+
cond-clauses
|
|
729
|
+
(list
|
|
730
|
+
(eliscript-expander--generated-form-at
|
|
731
|
+
(list
|
|
732
|
+
(eliscript-expander--generated-form-at t source-form)
|
|
733
|
+
(eliscript-expander--generated-form-at
|
|
734
|
+
(list
|
|
735
|
+
(eliscript-expander--generated-form-at
|
|
736
|
+
'__eliscript_arity_error source-form)
|
|
737
|
+
(eliscript-expander--generated-form-at label source-form)
|
|
738
|
+
arity-name)
|
|
739
|
+
source-form))
|
|
740
|
+
source-form))))
|
|
741
|
+
(let* ((arity-value
|
|
742
|
+
(eliscript-expander--generated-form-at
|
|
743
|
+
(list
|
|
744
|
+
(eliscript-expander--generated-form-at 'js-length source-form)
|
|
745
|
+
arguments-name)
|
|
746
|
+
source-form))
|
|
747
|
+
(dispatch
|
|
748
|
+
(eliscript-expander--let-form
|
|
749
|
+
arity-name
|
|
750
|
+
arity-value
|
|
751
|
+
(eliscript-expander--generated-form-at
|
|
752
|
+
(cons
|
|
753
|
+
(eliscript-expander--generated-form-at 'cond source-form)
|
|
754
|
+
cond-clauses)
|
|
755
|
+
source-form)))
|
|
756
|
+
(parameters
|
|
757
|
+
(eliscript-expander--generated-form-at
|
|
758
|
+
(list
|
|
759
|
+
(eliscript-expander--generated-form-at '&rest source-form)
|
|
760
|
+
arguments-name)
|
|
761
|
+
source-form)))
|
|
762
|
+
(eliscript-expander--generated-form-at
|
|
763
|
+
(append
|
|
764
|
+
(list operator-form)
|
|
765
|
+
(when name-form (list name-form))
|
|
766
|
+
(list parameters dispatch))
|
|
767
|
+
source-form))))
|
|
768
|
+
|
|
769
|
+
(defun eliscript-expander--desugar-letfn (arguments source-form)
|
|
770
|
+
"Return recursive local function ARGUMENTS as ordinary lexical forms."
|
|
771
|
+
(unless arguments
|
|
772
|
+
(eliscript-expander--fail "letfn requires a binding list"))
|
|
773
|
+
(unless (cdr arguments)
|
|
774
|
+
(eliscript-expander--fail "letfn requires at least one body form"))
|
|
775
|
+
(let* ((bindings-form (car arguments))
|
|
776
|
+
(declarations (eliscript-form-value bindings-form))
|
|
777
|
+
bindings assignments)
|
|
778
|
+
(unless (proper-list-p declarations)
|
|
779
|
+
(eliscript-expander--fail "letfn bindings must be a list"))
|
|
780
|
+
(dolist (declaration declarations)
|
|
781
|
+
(let* ((eliscript-expander--current-span
|
|
782
|
+
(or (eliscript-form-span declaration)
|
|
783
|
+
eliscript-expander--current-span))
|
|
784
|
+
(items (eliscript-form-value declaration)))
|
|
785
|
+
(unless (and (proper-list-p items) (>= (length items) 3))
|
|
786
|
+
(eliscript-expander--fail
|
|
787
|
+
"letfn declaration requires a name, parameter list, and body"))
|
|
788
|
+
(let* ((name-form (car items))
|
|
789
|
+
(name (eliscript-form-value name-form))
|
|
790
|
+
(async-p (eq (eliscript-form-value (cadr items)) 'async))
|
|
791
|
+
(function-forms (if async-p (cddr items) (cdr items)))
|
|
792
|
+
(operator-form
|
|
793
|
+
(eliscript-expander--generated-form-at
|
|
794
|
+
(if async-p 'async 'lambda) declaration))
|
|
795
|
+
function-form)
|
|
796
|
+
(unless (eliscript-macro-eval-symbol-p name)
|
|
797
|
+
(eliscript-expander--fail
|
|
798
|
+
"letfn name must be a symbol: %S" name))
|
|
799
|
+
(unless (and function-forms
|
|
800
|
+
(proper-list-p
|
|
801
|
+
(eliscript-form-value (car function-forms))))
|
|
802
|
+
(eliscript-expander--fail
|
|
803
|
+
"letfn declaration requires a parameter list"))
|
|
804
|
+
(setq function-form
|
|
805
|
+
(if (eliscript-expander--multi-arity-clause-p
|
|
806
|
+
(car function-forms))
|
|
807
|
+
(let* ((named
|
|
808
|
+
(eliscript-expander--desugar-multi-arity
|
|
809
|
+
operator-form name-form function-forms declaration))
|
|
810
|
+
(named-items (eliscript-form-value named)))
|
|
811
|
+
(eliscript-form-inherit
|
|
812
|
+
(cons (car named-items) (cddr named-items))
|
|
813
|
+
named))
|
|
814
|
+
(progn
|
|
815
|
+
(unless (cdr function-forms)
|
|
816
|
+
(eliscript-expander--fail
|
|
817
|
+
"letfn declaration requires a function body"))
|
|
818
|
+
(eliscript-expander--generated-form-at
|
|
819
|
+
(cons operator-form function-forms) declaration))))
|
|
820
|
+
(push
|
|
821
|
+
(eliscript-expander--generated-form-at
|
|
822
|
+
(list name-form
|
|
823
|
+
(eliscript-expander--generated-form-at nil declaration))
|
|
824
|
+
declaration)
|
|
825
|
+
bindings)
|
|
826
|
+
(push
|
|
827
|
+
(eliscript-expander--generated-form-at
|
|
828
|
+
(list
|
|
829
|
+
(eliscript-expander--generated-form-at 'set! declaration)
|
|
830
|
+
name-form
|
|
831
|
+
function-form)
|
|
832
|
+
declaration)
|
|
833
|
+
assignments))))
|
|
834
|
+
(eliscript-expander--generated-form-at
|
|
835
|
+
(append
|
|
836
|
+
(list
|
|
837
|
+
(eliscript-expander--generated-form-at 'let source-form)
|
|
838
|
+
(eliscript-expander--generated-form-at
|
|
839
|
+
(nreverse bindings) bindings-form))
|
|
840
|
+
(nreverse assignments)
|
|
841
|
+
(cdr arguments))
|
|
842
|
+
source-form)))
|
|
843
|
+
|
|
844
|
+
(defun eliscript-expander--desugar-defprotocol (arguments)
|
|
845
|
+
"Return core declarations represented by defprotocol ARGUMENTS."
|
|
846
|
+
(when (< (length arguments) 2)
|
|
847
|
+
(eliscript-expander--fail
|
|
848
|
+
"defprotocol expects a name and at least one operation"))
|
|
849
|
+
(let* ((name-form (car arguments))
|
|
850
|
+
(name (eliscript-form-value name-form))
|
|
851
|
+
(operation-forms (cdr arguments))
|
|
852
|
+
(seen (make-hash-table :test #'equal))
|
|
853
|
+
operation-names)
|
|
854
|
+
(unless (eliscript-macro-eval-symbol-p name)
|
|
855
|
+
(eliscript-expander--fail
|
|
856
|
+
"defprotocol name must be a symbol: %S"
|
|
857
|
+
(eliscript-form-strip name-form)))
|
|
858
|
+
(dolist (operation-form operation-forms)
|
|
859
|
+
(let ((operation (eliscript-form-value operation-form)))
|
|
860
|
+
(unless (eliscript-macro-eval-symbol-p operation)
|
|
861
|
+
(eliscript-expander--fail
|
|
862
|
+
"defprotocol operation must be a symbol: %S"
|
|
863
|
+
(eliscript-form-strip operation-form)))
|
|
864
|
+
(let ((operation-name (symbol-name operation)))
|
|
865
|
+
(when (gethash operation-name seen)
|
|
866
|
+
(eliscript-expander--fail
|
|
867
|
+
"defprotocol declares duplicate operation: %s" operation-name))
|
|
868
|
+
(puthash operation-name t seen)
|
|
869
|
+
(push (eliscript-expander--generated-form-at
|
|
870
|
+
operation-name operation-form)
|
|
871
|
+
operation-names))))
|
|
872
|
+
(setq operation-names (nreverse operation-names))
|
|
873
|
+
(cons
|
|
874
|
+
(eliscript-expander--generated-form
|
|
875
|
+
(list
|
|
876
|
+
(eliscript-expander--generated-form 'defconst)
|
|
877
|
+
name-form
|
|
878
|
+
(eliscript-expander--generated-form
|
|
879
|
+
(list
|
|
880
|
+
(eliscript-expander--generated-form 'define-protocol)
|
|
881
|
+
(eliscript-expander--generated-form-at
|
|
882
|
+
(symbol-name name) name-form)
|
|
883
|
+
(eliscript-expander--generated-form
|
|
884
|
+
(cons (eliscript-expander--generated-form 'js-array)
|
|
885
|
+
operation-names))))))
|
|
886
|
+
(mapcar
|
|
887
|
+
(lambda (operation-form)
|
|
888
|
+
(eliscript-expander--generated-form
|
|
889
|
+
(list
|
|
890
|
+
(eliscript-expander--generated-form 'defconst)
|
|
891
|
+
operation-form
|
|
892
|
+
(eliscript-expander--generated-form
|
|
893
|
+
(list
|
|
894
|
+
(eliscript-expander--generated-form 'protocol-method)
|
|
895
|
+
name-form
|
|
896
|
+
(eliscript-expander--generated-form-at
|
|
897
|
+
(symbol-name (eliscript-form-value operation-form))
|
|
898
|
+
operation-form))))))
|
|
899
|
+
operation-forms))))
|
|
900
|
+
|
|
901
|
+
(defun eliscript-expander--desugar-defrecord (arguments)
|
|
902
|
+
"Return core declarations represented by defrecord ARGUMENTS."
|
|
903
|
+
(unless (= (length arguments) 2)
|
|
904
|
+
(eliscript-expander--fail
|
|
905
|
+
"defrecord expects a name and field vector"))
|
|
906
|
+
(let* ((name-form (nth 0 arguments))
|
|
907
|
+
(name (eliscript-form-value name-form))
|
|
908
|
+
(fields-form (nth 1 arguments))
|
|
909
|
+
(fields (eliscript-form-value fields-form))
|
|
910
|
+
(seen (make-hash-table :test #'equal))
|
|
911
|
+
field-names)
|
|
912
|
+
(unless (and (eliscript-macro-eval-symbol-p name)
|
|
913
|
+
(not (string-match-p "/" (symbol-name name))))
|
|
914
|
+
(eliscript-expander--fail
|
|
915
|
+
"defrecord name must be an unqualified symbol: %S"
|
|
916
|
+
(eliscript-form-strip name-form)))
|
|
917
|
+
(unless (vectorp fields)
|
|
918
|
+
(eliscript-expander--fail
|
|
919
|
+
"defrecord fields must be a vector: %S"
|
|
920
|
+
(eliscript-form-strip fields-form)))
|
|
921
|
+
(dolist (field-form (append fields nil))
|
|
922
|
+
(let ((field (eliscript-form-value field-form)))
|
|
923
|
+
(unless (and (eliscript-macro-eval-symbol-p field)
|
|
924
|
+
(not (string-match-p "/" (symbol-name field))))
|
|
925
|
+
(eliscript-expander--fail
|
|
926
|
+
"defrecord fields must be unqualified symbols: %S"
|
|
927
|
+
(eliscript-form-strip field-form)))
|
|
928
|
+
(let ((field-name (symbol-name field)))
|
|
929
|
+
(when (gethash field-name seen)
|
|
930
|
+
(eliscript-expander--fail
|
|
931
|
+
"defrecord declares duplicate field: %s" field-name))
|
|
932
|
+
(puthash field-name t seen)
|
|
933
|
+
(push (eliscript-expander--generated-form-at
|
|
934
|
+
field-name field-form)
|
|
935
|
+
field-names))))
|
|
936
|
+
(setq field-names (nreverse field-names))
|
|
937
|
+
(let* ((name-string (symbol-name name))
|
|
938
|
+
(field-forms (append fields nil))
|
|
939
|
+
(parameter-form
|
|
940
|
+
(eliscript-expander--generated-form-at field-forms fields-form))
|
|
941
|
+
(constructor-form
|
|
942
|
+
(eliscript-expander--generated-form-at
|
|
943
|
+
(intern (concat "->" name-string)) name-form))
|
|
944
|
+
(map-constructor-form
|
|
945
|
+
(eliscript-expander--generated-form-at
|
|
946
|
+
(intern (concat "map->" name-string)) name-form))
|
|
947
|
+
(predicate-form
|
|
948
|
+
(eliscript-expander--generated-form-at
|
|
949
|
+
(intern (concat name-string "?")) name-form))
|
|
950
|
+
(source-form
|
|
951
|
+
(eliscript-expander--generated-form-at 'source fields-form))
|
|
952
|
+
(value-form
|
|
953
|
+
(eliscript-expander--generated-form-at 'value name-form)))
|
|
954
|
+
(list
|
|
955
|
+
(eliscript-expander--generated-form
|
|
956
|
+
(list
|
|
957
|
+
(eliscript-expander--generated-form 'defconst)
|
|
958
|
+
name-form
|
|
959
|
+
(eliscript-expander--generated-form
|
|
960
|
+
(list
|
|
961
|
+
(eliscript-expander--generated-form 'define-record-type)
|
|
962
|
+
(eliscript-expander--generated-form-at name-string name-form)
|
|
963
|
+
(eliscript-expander--generated-form
|
|
964
|
+
(cons (eliscript-expander--generated-form 'js-array)
|
|
965
|
+
field-names))))))
|
|
966
|
+
(eliscript-expander--generated-form
|
|
967
|
+
(list
|
|
968
|
+
(eliscript-expander--generated-form 'defconst)
|
|
969
|
+
constructor-form
|
|
970
|
+
(eliscript-expander--generated-form
|
|
971
|
+
(list
|
|
972
|
+
(eliscript-expander--generated-form 'lambda)
|
|
973
|
+
parameter-form
|
|
974
|
+
(eliscript-expander--generated-form
|
|
975
|
+
(append
|
|
976
|
+
(list
|
|
977
|
+
(eliscript-expander--generated-form 'js-call)
|
|
978
|
+
name-form
|
|
979
|
+
(eliscript-expander--generated-form ':create))
|
|
980
|
+
field-forms))))))
|
|
981
|
+
(eliscript-expander--generated-form
|
|
982
|
+
(list
|
|
983
|
+
(eliscript-expander--generated-form 'defconst)
|
|
984
|
+
map-constructor-form
|
|
985
|
+
(eliscript-expander--generated-form
|
|
986
|
+
(list
|
|
987
|
+
(eliscript-expander--generated-form 'lambda)
|
|
988
|
+
(eliscript-expander--generated-form (list source-form))
|
|
989
|
+
(eliscript-expander--generated-form
|
|
990
|
+
(list
|
|
991
|
+
(eliscript-expander--generated-form 'js-call)
|
|
992
|
+
name-form
|
|
993
|
+
(eliscript-expander--generated-form ':fromMap)
|
|
994
|
+
source-form))))))
|
|
995
|
+
(eliscript-expander--generated-form
|
|
996
|
+
(list
|
|
997
|
+
(eliscript-expander--generated-form 'defconst)
|
|
998
|
+
predicate-form
|
|
999
|
+
(eliscript-expander--generated-form
|
|
1000
|
+
(list
|
|
1001
|
+
(eliscript-expander--generated-form 'lambda)
|
|
1002
|
+
(eliscript-expander--generated-form (list value-form))
|
|
1003
|
+
(eliscript-expander--generated-form
|
|
1004
|
+
(list
|
|
1005
|
+
(eliscript-expander--generated-form 'js-call)
|
|
1006
|
+
name-form
|
|
1007
|
+
(eliscript-expander--generated-form ':isInstance)
|
|
1008
|
+
value-form))))))))))
|
|
1009
|
+
|
|
1010
|
+
(defun eliscript-expander--protocol-method-object
|
|
1011
|
+
(form-name method-forms &optional require-receiver)
|
|
1012
|
+
"Build an implementation object for FORM-NAME from METHOD-FORMS."
|
|
1013
|
+
(unless method-forms
|
|
1014
|
+
(eliscript-expander--fail
|
|
1015
|
+
"%s expects at least one method" form-name))
|
|
1016
|
+
(let ((seen (make-hash-table :test #'equal))
|
|
1017
|
+
object-items)
|
|
1018
|
+
(dolist (method-form method-forms)
|
|
1019
|
+
(let ((method (eliscript-form-value method-form)))
|
|
1020
|
+
(unless (and (proper-list-p method) (>= (length method) 2))
|
|
1021
|
+
(eliscript-expander--fail
|
|
1022
|
+
"%s method must contain an operation and parameter list: %S"
|
|
1023
|
+
form-name (eliscript-form-strip method-form)))
|
|
1024
|
+
(when require-receiver
|
|
1025
|
+
(let ((parameters (eliscript-form-value (nth 1 method))))
|
|
1026
|
+
(unless (and (proper-list-p parameters) parameters)
|
|
1027
|
+
(eliscript-expander--fail
|
|
1028
|
+
"%s method must declare a receiver parameter: %S"
|
|
1029
|
+
form-name (eliscript-form-strip method-form)))))
|
|
1030
|
+
(let* ((operation-form (car method))
|
|
1031
|
+
(operation (eliscript-form-value operation-form)))
|
|
1032
|
+
(unless (eliscript-macro-eval-symbol-p operation)
|
|
1033
|
+
(eliscript-expander--fail
|
|
1034
|
+
"%s method operation must be a symbol: %S"
|
|
1035
|
+
form-name (eliscript-form-strip operation-form)))
|
|
1036
|
+
(let ((operation-name (symbol-name operation)))
|
|
1037
|
+
(when (gethash operation-name seen)
|
|
1038
|
+
(eliscript-expander--fail
|
|
1039
|
+
"%s declares duplicate method: %s" form-name operation-name))
|
|
1040
|
+
(puthash operation-name t seen)
|
|
1041
|
+
(setq object-items
|
|
1042
|
+
(append
|
|
1043
|
+
object-items
|
|
1044
|
+
(list
|
|
1045
|
+
(eliscript-expander--generated-form-at
|
|
1046
|
+
operation-name operation-form)
|
|
1047
|
+
(eliscript-expander--generated-form-at
|
|
1048
|
+
(cons
|
|
1049
|
+
(eliscript-expander--generated-form-at
|
|
1050
|
+
'lambda method-form)
|
|
1051
|
+
(cdr method))
|
|
1052
|
+
method-form))))))))
|
|
1053
|
+
(eliscript-expander--generated-form
|
|
1054
|
+
(cons (eliscript-expander--generated-form 'js-object) object-items))))
|
|
1055
|
+
|
|
1056
|
+
(defun eliscript-expander--protocol-implementation-plans
|
|
1057
|
+
(form-name implementation-forms &optional allow-empty)
|
|
1058
|
+
"Build protocol implementation plans for FORM-NAME."
|
|
1059
|
+
(unless (or allow-empty implementation-forms)
|
|
1060
|
+
(eliscript-expander--fail
|
|
1061
|
+
"%s expects at least one protocol implementation" form-name))
|
|
1062
|
+
(let ((remaining implementation-forms)
|
|
1063
|
+
(seen (make-hash-table :test #'equal))
|
|
1064
|
+
plans)
|
|
1065
|
+
(while remaining
|
|
1066
|
+
(let* ((protocol-form (pop remaining))
|
|
1067
|
+
(protocol (eliscript-form-value protocol-form)))
|
|
1068
|
+
(unless (eliscript-macro-eval-symbol-p protocol)
|
|
1069
|
+
(eliscript-expander--fail
|
|
1070
|
+
"%s protocol must be a symbol: %S"
|
|
1071
|
+
form-name (eliscript-form-strip protocol-form)))
|
|
1072
|
+
(let ((protocol-name (symbol-name protocol))
|
|
1073
|
+
methods)
|
|
1074
|
+
(when (gethash protocol-name seen)
|
|
1075
|
+
(eliscript-expander--fail
|
|
1076
|
+
"%s declares duplicate protocol: %s" form-name protocol-name))
|
|
1077
|
+
(puthash protocol-name t seen)
|
|
1078
|
+
(while (and remaining
|
|
1079
|
+
(consp (eliscript-form-value (car remaining)))
|
|
1080
|
+
(proper-list-p (eliscript-form-value (car remaining))))
|
|
1081
|
+
(push (pop remaining) methods))
|
|
1082
|
+
(setq methods (nreverse methods))
|
|
1083
|
+
(unless methods
|
|
1084
|
+
(eliscript-expander--fail
|
|
1085
|
+
"%s protocol %s expects at least one method"
|
|
1086
|
+
form-name protocol-name))
|
|
1087
|
+
(push
|
|
1088
|
+
(eliscript-expander--generated-form
|
|
1089
|
+
(list
|
|
1090
|
+
(eliscript-expander--generated-form 'js-array)
|
|
1091
|
+
protocol-form
|
|
1092
|
+
(eliscript-expander--protocol-method-object
|
|
1093
|
+
form-name methods t)))
|
|
1094
|
+
plans))))
|
|
1095
|
+
(eliscript-expander--generated-form
|
|
1096
|
+
(cons (eliscript-expander--generated-form 'js-array)
|
|
1097
|
+
(nreverse plans)))))
|
|
1098
|
+
|
|
1099
|
+
(defun eliscript-expander--desugar-deftype (arguments)
|
|
1100
|
+
"Return core declarations represented by deftype ARGUMENTS."
|
|
1101
|
+
(when (< (length arguments) 2)
|
|
1102
|
+
(eliscript-expander--fail
|
|
1103
|
+
"deftype expects a name, field vector, and optional protocol implementations"))
|
|
1104
|
+
(let* ((name-form (nth 0 arguments))
|
|
1105
|
+
(name (eliscript-form-value name-form))
|
|
1106
|
+
(fields-form (nth 1 arguments))
|
|
1107
|
+
(fields (eliscript-form-value fields-form))
|
|
1108
|
+
(seen (make-hash-table :test #'equal))
|
|
1109
|
+
field-names)
|
|
1110
|
+
(unless (and (eliscript-macro-eval-symbol-p name)
|
|
1111
|
+
(not (string-match-p "/" (symbol-name name))))
|
|
1112
|
+
(eliscript-expander--fail
|
|
1113
|
+
"deftype name must be an unqualified symbol: %S"
|
|
1114
|
+
(eliscript-form-strip name-form)))
|
|
1115
|
+
(unless (vectorp fields)
|
|
1116
|
+
(eliscript-expander--fail
|
|
1117
|
+
"deftype fields must be a vector: %S"
|
|
1118
|
+
(eliscript-form-strip fields-form)))
|
|
1119
|
+
(dolist (field-form (append fields nil))
|
|
1120
|
+
(let ((field (eliscript-form-value field-form)))
|
|
1121
|
+
(unless (and (eliscript-macro-eval-symbol-p field)
|
|
1122
|
+
(not (string-match-p "/" (symbol-name field))))
|
|
1123
|
+
(eliscript-expander--fail
|
|
1124
|
+
"deftype fields must be unqualified symbols: %S"
|
|
1125
|
+
(eliscript-form-strip field-form)))
|
|
1126
|
+
(let ((field-name (symbol-name field)))
|
|
1127
|
+
(when (gethash field-name seen)
|
|
1128
|
+
(eliscript-expander--fail
|
|
1129
|
+
"deftype declares duplicate field: %s" field-name))
|
|
1130
|
+
(puthash field-name t seen)
|
|
1131
|
+
(push (eliscript-expander--generated-form-at
|
|
1132
|
+
field-name field-form)
|
|
1133
|
+
field-names))))
|
|
1134
|
+
(setq field-names (nreverse field-names))
|
|
1135
|
+
(let* ((name-string (symbol-name name))
|
|
1136
|
+
(field-forms (append fields nil))
|
|
1137
|
+
(parameter-form
|
|
1138
|
+
(eliscript-expander--generated-form-at field-forms fields-form))
|
|
1139
|
+
(constructor-form
|
|
1140
|
+
(eliscript-expander--generated-form-at
|
|
1141
|
+
(intern (concat "->" name-string)) name-form))
|
|
1142
|
+
(predicate-form
|
|
1143
|
+
(eliscript-expander--generated-form-at
|
|
1144
|
+
(intern (concat name-string "?")) name-form))
|
|
1145
|
+
(value-form
|
|
1146
|
+
(eliscript-expander--generated-form-at 'value name-form))
|
|
1147
|
+
(plans-form
|
|
1148
|
+
(eliscript-expander--protocol-implementation-plans
|
|
1149
|
+
"deftype" (nthcdr 2 arguments) t)))
|
|
1150
|
+
(list
|
|
1151
|
+
(eliscript-expander--generated-form
|
|
1152
|
+
(list
|
|
1153
|
+
(eliscript-expander--generated-form 'defconst)
|
|
1154
|
+
name-form
|
|
1155
|
+
(eliscript-expander--generated-form
|
|
1156
|
+
(list
|
|
1157
|
+
(eliscript-expander--generated-form 'define-type)
|
|
1158
|
+
(eliscript-expander--generated-form-at name-string name-form)
|
|
1159
|
+
(eliscript-expander--generated-form
|
|
1160
|
+
(cons (eliscript-expander--generated-form 'js-array)
|
|
1161
|
+
field-names))
|
|
1162
|
+
plans-form))))
|
|
1163
|
+
(eliscript-expander--generated-form
|
|
1164
|
+
(list
|
|
1165
|
+
(eliscript-expander--generated-form 'defconst)
|
|
1166
|
+
constructor-form
|
|
1167
|
+
(eliscript-expander--generated-form
|
|
1168
|
+
(list
|
|
1169
|
+
(eliscript-expander--generated-form 'lambda)
|
|
1170
|
+
parameter-form
|
|
1171
|
+
(eliscript-expander--generated-form
|
|
1172
|
+
(append
|
|
1173
|
+
(list
|
|
1174
|
+
(eliscript-expander--generated-form 'js-call)
|
|
1175
|
+
name-form
|
|
1176
|
+
(eliscript-expander--generated-form ':create))
|
|
1177
|
+
field-forms))))))
|
|
1178
|
+
(eliscript-expander--generated-form
|
|
1179
|
+
(list
|
|
1180
|
+
(eliscript-expander--generated-form 'defconst)
|
|
1181
|
+
predicate-form
|
|
1182
|
+
(eliscript-expander--generated-form
|
|
1183
|
+
(list
|
|
1184
|
+
(eliscript-expander--generated-form 'lambda)
|
|
1185
|
+
(eliscript-expander--generated-form (list value-form))
|
|
1186
|
+
(eliscript-expander--generated-form
|
|
1187
|
+
(list
|
|
1188
|
+
(eliscript-expander--generated-form 'js-call)
|
|
1189
|
+
name-form
|
|
1190
|
+
(eliscript-expander--generated-form ':isInstance)
|
|
1191
|
+
value-form))))))))))
|
|
1192
|
+
|
|
1193
|
+
(defun eliscript-expander--desugar-reify (arguments)
|
|
1194
|
+
"Return the protocol-backed anonymous value represented by ARGUMENTS."
|
|
1195
|
+
(eliscript-expander--generated-form
|
|
1196
|
+
(list
|
|
1197
|
+
(eliscript-expander--generated-form 'reify-protocols)
|
|
1198
|
+
(eliscript-expander--protocol-implementation-plans
|
|
1199
|
+
"reify" arguments))))
|
|
1200
|
+
|
|
1201
|
+
(defun eliscript-expander--desugar-extend-type (arguments)
|
|
1202
|
+
"Return the core exact-type extension represented by ARGUMENTS."
|
|
1203
|
+
(when (< (length arguments) 3)
|
|
1204
|
+
(eliscript-expander--fail
|
|
1205
|
+
"extend-type expects a target, protocol, and at least one method"))
|
|
1206
|
+
(let ((target-form (nth 0 arguments))
|
|
1207
|
+
(protocol-form (nth 1 arguments)))
|
|
1208
|
+
(unless (eliscript-macro-eval-symbol-p
|
|
1209
|
+
(eliscript-form-value target-form))
|
|
1210
|
+
(eliscript-expander--fail
|
|
1211
|
+
"extend-type target must be a symbol: %S"
|
|
1212
|
+
(eliscript-form-strip target-form)))
|
|
1213
|
+
(unless (eliscript-macro-eval-symbol-p
|
|
1214
|
+
(eliscript-form-value protocol-form))
|
|
1215
|
+
(eliscript-expander--fail
|
|
1216
|
+
"extend-type protocol must be a symbol: %S"
|
|
1217
|
+
(eliscript-form-strip protocol-form)))
|
|
1218
|
+
(eliscript-expander--generated-form
|
|
1219
|
+
(list
|
|
1220
|
+
(eliscript-expander--generated-form 'extend-protocol-type)
|
|
1221
|
+
protocol-form
|
|
1222
|
+
target-form
|
|
1223
|
+
(eliscript-expander--protocol-method-object
|
|
1224
|
+
"extend-type" (nthcdr 2 arguments))))))
|
|
1225
|
+
|
|
1226
|
+
(defun eliscript-expander--desugar-extend-category (arguments)
|
|
1227
|
+
"Return the core host-category extension represented by ARGUMENTS."
|
|
1228
|
+
(when (< (length arguments) 3)
|
|
1229
|
+
(eliscript-expander--fail
|
|
1230
|
+
"extend-category expects a category, protocol, and at least one method"))
|
|
1231
|
+
(let ((category-form (nth 0 arguments))
|
|
1232
|
+
(protocol-form (nth 1 arguments)))
|
|
1233
|
+
(unless (and (stringp (eliscript-form-value category-form))
|
|
1234
|
+
(> (length (eliscript-form-value category-form)) 0))
|
|
1235
|
+
(eliscript-expander--fail
|
|
1236
|
+
"extend-category category must be a non-empty string: %S"
|
|
1237
|
+
(eliscript-form-strip category-form)))
|
|
1238
|
+
(unless (eliscript-macro-eval-symbol-p
|
|
1239
|
+
(eliscript-form-value protocol-form))
|
|
1240
|
+
(eliscript-expander--fail
|
|
1241
|
+
"extend-category protocol must be a symbol: %S"
|
|
1242
|
+
(eliscript-form-strip protocol-form)))
|
|
1243
|
+
(eliscript-expander--generated-form
|
|
1244
|
+
(list
|
|
1245
|
+
(eliscript-expander--generated-form 'extend-protocol-category)
|
|
1246
|
+
protocol-form
|
|
1247
|
+
category-form
|
|
1248
|
+
(eliscript-expander--protocol-method-object
|
|
1249
|
+
"extend-category" (nthcdr 2 arguments))))))
|
|
1250
|
+
|
|
1251
|
+
(defun eliscript-expander--desugar-extend-default (arguments)
|
|
1252
|
+
"Return the core default extension represented by ARGUMENTS."
|
|
1253
|
+
(when (< (length arguments) 2)
|
|
1254
|
+
(eliscript-expander--fail
|
|
1255
|
+
"extend-default expects a protocol and at least one method"))
|
|
1256
|
+
(let ((protocol-form (car arguments)))
|
|
1257
|
+
(unless (eliscript-macro-eval-symbol-p
|
|
1258
|
+
(eliscript-form-value protocol-form))
|
|
1259
|
+
(eliscript-expander--fail
|
|
1260
|
+
"extend-default protocol must be a symbol: %S"
|
|
1261
|
+
(eliscript-form-strip protocol-form)))
|
|
1262
|
+
(eliscript-expander--generated-form
|
|
1263
|
+
(list
|
|
1264
|
+
(eliscript-expander--generated-form 'extend-protocol-default)
|
|
1265
|
+
protocol-form
|
|
1266
|
+
(eliscript-expander--protocol-method-object
|
|
1267
|
+
"extend-default" (cdr arguments))))))
|
|
1268
|
+
|
|
1269
|
+
(defun eliscript-expander--desugar-defmulti (arguments)
|
|
1270
|
+
"Return the core declaration represented by defmulti ARGUMENTS."
|
|
1271
|
+
(unless (<= 2 (length arguments) 3)
|
|
1272
|
+
(eliscript-expander--fail
|
|
1273
|
+
"defmulti expects a name, dispatch function, and optional default value"))
|
|
1274
|
+
(let* ((name-form (nth 0 arguments))
|
|
1275
|
+
(name (eliscript-form-value name-form)))
|
|
1276
|
+
(unless (eliscript-macro-eval-symbol-p name)
|
|
1277
|
+
(eliscript-expander--fail
|
|
1278
|
+
"defmulti name must be a symbol: %S"
|
|
1279
|
+
(eliscript-form-strip name-form)))
|
|
1280
|
+
(eliscript-expander--generated-form
|
|
1281
|
+
(list
|
|
1282
|
+
(eliscript-expander--generated-form 'defconst)
|
|
1283
|
+
name-form
|
|
1284
|
+
(eliscript-expander--generated-form
|
|
1285
|
+
(append
|
|
1286
|
+
(list
|
|
1287
|
+
(eliscript-expander--generated-form 'multi-fn)
|
|
1288
|
+
(eliscript-expander--generated-form (symbol-name name))
|
|
1289
|
+
(nth 1 arguments))
|
|
1290
|
+
(when (= (length arguments) 3)
|
|
1291
|
+
(list (nth 2 arguments)))))))))
|
|
1292
|
+
|
|
1293
|
+
(defun eliscript-expander--desugar-defmethod (arguments)
|
|
1294
|
+
"Return the method registration represented by defmethod ARGUMENTS."
|
|
1295
|
+
(unless (>= (length arguments) 3)
|
|
1296
|
+
(eliscript-expander--fail
|
|
1297
|
+
"defmethod expects a multimethod, dispatch value, and parameter list"))
|
|
1298
|
+
(let* ((target-form (nth 0 arguments))
|
|
1299
|
+
(target (eliscript-form-value target-form)))
|
|
1300
|
+
(unless (eliscript-macro-eval-symbol-p target)
|
|
1301
|
+
(eliscript-expander--fail
|
|
1302
|
+
"defmethod target must be a symbol: %S"
|
|
1303
|
+
(eliscript-form-strip target-form)))
|
|
1304
|
+
(eliscript-expander--generated-form
|
|
1305
|
+
(list
|
|
1306
|
+
(eliscript-expander--generated-form 'add-method!)
|
|
1307
|
+
target-form
|
|
1308
|
+
(nth 1 arguments)
|
|
1309
|
+
(eliscript-expander--generated-form
|
|
1310
|
+
(append
|
|
1311
|
+
(list
|
|
1312
|
+
(eliscript-expander--generated-form 'lambda)
|
|
1313
|
+
(nth 2 arguments))
|
|
1314
|
+
(nthcdr 3 arguments)))))))
|
|
1315
|
+
|
|
1316
|
+
(defun eliscript-expander--expand-expression (form environment depth)
|
|
1317
|
+
"Expand expression FORM in macro ENVIRONMENT at DEPTH."
|
|
1318
|
+
(let* ((value (eliscript-form-value form))
|
|
1319
|
+
(eliscript-expander--current-span
|
|
1320
|
+
(or (eliscript-form-span form) eliscript-expander--current-span)))
|
|
1321
|
+
(cond
|
|
1322
|
+
((vectorp value)
|
|
1323
|
+
(eliscript-form-inherit
|
|
1324
|
+
(apply #'vector
|
|
1325
|
+
(eliscript-expander--expand-sequence
|
|
1326
|
+
(append value nil) environment depth))
|
|
1327
|
+
form))
|
|
1328
|
+
((not (consp value)) form)
|
|
1329
|
+
((not (proper-list-p value))
|
|
1330
|
+
(eliscript-expander--fail
|
|
1331
|
+
"dotted call forms are not supported: %S"
|
|
1332
|
+
(eliscript-form-strip form)))
|
|
1333
|
+
(t
|
|
1334
|
+
(let* ((operator-form (car value))
|
|
1335
|
+
(operator (eliscript-form-value operator-form))
|
|
1336
|
+
(arguments (cdr value)))
|
|
1337
|
+
(cond
|
|
1338
|
+
((eq operator 'quote) form)
|
|
1339
|
+
((eq operator 'defmacro)
|
|
1340
|
+
(eliscript-expander--fail
|
|
1341
|
+
"defmacro is only valid at module top level"))
|
|
1342
|
+
((eq operator 'defportable)
|
|
1343
|
+
(eliscript-expander--fail
|
|
1344
|
+
"defportable is only valid at module top level"))
|
|
1345
|
+
((eq operator 'defasync)
|
|
1346
|
+
(eliscript-expander--fail
|
|
1347
|
+
"defasync is only valid at module top level"))
|
|
1348
|
+
((eq operator 'defmulti)
|
|
1349
|
+
(eliscript-expander--fail
|
|
1350
|
+
"defmulti is only valid at module top level"))
|
|
1351
|
+
((eq operator 'defmethod)
|
|
1352
|
+
(eliscript-expander--fail
|
|
1353
|
+
"defmethod is only valid at module top level"))
|
|
1354
|
+
((memq operator
|
|
1355
|
+
'(deftype defrecord defprotocol extend-type extend-category
|
|
1356
|
+
extend-default))
|
|
1357
|
+
(eliscript-expander--fail
|
|
1358
|
+
"%s is only valid at module top level" operator))
|
|
1359
|
+
((and (symbolp operator) (gethash operator environment))
|
|
1360
|
+
(eliscript-expander--expand-expression
|
|
1361
|
+
(eliscript-form-locate-generated
|
|
1362
|
+
(eliscript-expander--invoke
|
|
1363
|
+
(gethash operator environment) arguments depth)
|
|
1364
|
+
eliscript-expander--current-span)
|
|
1365
|
+
environment
|
|
1366
|
+
(1+ depth)))
|
|
1367
|
+
((eq operator '->)
|
|
1368
|
+
(eliscript-expander--expand-expression
|
|
1369
|
+
(eliscript-expander--desugar-thread "->" arguments 'first)
|
|
1370
|
+
environment depth))
|
|
1371
|
+
((eq operator '->>)
|
|
1372
|
+
(eliscript-expander--expand-expression
|
|
1373
|
+
(eliscript-expander--desugar-thread "->>" arguments 'last)
|
|
1374
|
+
environment depth))
|
|
1375
|
+
((eq operator 'as->)
|
|
1376
|
+
(eliscript-expander--expand-expression
|
|
1377
|
+
(eliscript-expander--desugar-as-thread arguments)
|
|
1378
|
+
environment depth))
|
|
1379
|
+
((eq operator 'cond->)
|
|
1380
|
+
(eliscript-expander--expand-expression
|
|
1381
|
+
(eliscript-expander--desugar-cond-thread
|
|
1382
|
+
"cond->" arguments 'first)
|
|
1383
|
+
environment depth))
|
|
1384
|
+
((eq operator 'cond->>)
|
|
1385
|
+
(eliscript-expander--expand-expression
|
|
1386
|
+
(eliscript-expander--desugar-cond-thread
|
|
1387
|
+
"cond->>" arguments 'last)
|
|
1388
|
+
environment depth))
|
|
1389
|
+
((eq operator 'some->)
|
|
1390
|
+
(eliscript-expander--expand-expression
|
|
1391
|
+
(eliscript-expander--desugar-some-thread
|
|
1392
|
+
"some->" arguments 'first)
|
|
1393
|
+
environment depth))
|
|
1394
|
+
((eq operator 'some->>)
|
|
1395
|
+
(eliscript-expander--expand-expression
|
|
1396
|
+
(eliscript-expander--desugar-some-thread
|
|
1397
|
+
"some->>" arguments 'last)
|
|
1398
|
+
environment depth))
|
|
1399
|
+
((eq operator 'if-let)
|
|
1400
|
+
(eliscript-expander--expand-expression
|
|
1401
|
+
(eliscript-expander--desugar-if-binding
|
|
1402
|
+
"if-let" arguments nil)
|
|
1403
|
+
environment depth))
|
|
1404
|
+
((eq operator 'when-let)
|
|
1405
|
+
(eliscript-expander--expand-expression
|
|
1406
|
+
(eliscript-expander--desugar-when-binding
|
|
1407
|
+
"when-let" arguments nil)
|
|
1408
|
+
environment depth))
|
|
1409
|
+
((eq operator 'if-some)
|
|
1410
|
+
(eliscript-expander--expand-expression
|
|
1411
|
+
(eliscript-expander--desugar-if-binding
|
|
1412
|
+
"if-some" arguments t)
|
|
1413
|
+
environment depth))
|
|
1414
|
+
((eq operator 'when-some)
|
|
1415
|
+
(eliscript-expander--expand-expression
|
|
1416
|
+
(eliscript-expander--desugar-when-binding
|
|
1417
|
+
"when-some" arguments t)
|
|
1418
|
+
environment depth))
|
|
1419
|
+
((eq operator 'case)
|
|
1420
|
+
(eliscript-expander--expand-expression
|
|
1421
|
+
(eliscript-expander--desugar-case arguments)
|
|
1422
|
+
environment depth))
|
|
1423
|
+
((eq operator 'condp)
|
|
1424
|
+
(eliscript-expander--expand-expression
|
|
1425
|
+
(eliscript-expander--desugar-condp arguments)
|
|
1426
|
+
environment depth))
|
|
1427
|
+
((and (memq operator '(lambda fn async))
|
|
1428
|
+
arguments
|
|
1429
|
+
(eliscript-expander--multi-arity-clause-p (car arguments)))
|
|
1430
|
+
(eliscript-expander--expand-expression
|
|
1431
|
+
(eliscript-expander--desugar-multi-arity
|
|
1432
|
+
operator-form nil arguments form)
|
|
1433
|
+
environment depth))
|
|
1434
|
+
((eq operator 'reify)
|
|
1435
|
+
(eliscript-expander--expand-expression
|
|
1436
|
+
(eliscript-expander--desugar-reify arguments)
|
|
1437
|
+
environment depth))
|
|
1438
|
+
(t
|
|
1439
|
+
(eliscript-form-inherit
|
|
1440
|
+
(pcase operator
|
|
1441
|
+
((or 'lambda 'fn 'async)
|
|
1442
|
+
(if arguments
|
|
1443
|
+
(cons operator-form
|
|
1444
|
+
(cons (car arguments)
|
|
1445
|
+
(eliscript-expander--expand-sequence
|
|
1446
|
+
(cdr arguments) environment depth)))
|
|
1447
|
+
value))
|
|
1448
|
+
((or 'let 'let*)
|
|
1449
|
+
(cons operator-form
|
|
1450
|
+
(eliscript-expander--expand-bindings
|
|
1451
|
+
arguments environment depth)))
|
|
1452
|
+
('letfn
|
|
1453
|
+
(eliscript-form-value
|
|
1454
|
+
(eliscript-expander--expand-expression
|
|
1455
|
+
(eliscript-expander--desugar-letfn arguments form)
|
|
1456
|
+
environment depth)))
|
|
1457
|
+
('setq
|
|
1458
|
+
(cons operator-form
|
|
1459
|
+
(eliscript-expander--expand-assignment
|
|
1460
|
+
arguments environment depth)))
|
|
1461
|
+
('set!
|
|
1462
|
+
(cons operator-form
|
|
1463
|
+
(if arguments
|
|
1464
|
+
(cons (car arguments)
|
|
1465
|
+
(eliscript-expander--expand-sequence
|
|
1466
|
+
(cdr arguments) environment depth))
|
|
1467
|
+
nil)))
|
|
1468
|
+
('cond
|
|
1469
|
+
(cons operator-form
|
|
1470
|
+
(eliscript-expander--expand-cond
|
|
1471
|
+
arguments environment depth)))
|
|
1472
|
+
('try
|
|
1473
|
+
(cons operator-form
|
|
1474
|
+
(mapcar
|
|
1475
|
+
(lambda (clause)
|
|
1476
|
+
(eliscript-expander--expand-try-clause
|
|
1477
|
+
clause environment depth))
|
|
1478
|
+
arguments)))
|
|
1479
|
+
('js-object
|
|
1480
|
+
(cons operator-form
|
|
1481
|
+
(eliscript-expander--expand-object
|
|
1482
|
+
arguments environment depth)))
|
|
1483
|
+
((or 'get 'put 'js-call)
|
|
1484
|
+
(cons operator-form
|
|
1485
|
+
(eliscript-expander--expand-property-call
|
|
1486
|
+
arguments environment depth)))
|
|
1487
|
+
(_
|
|
1488
|
+
(cons (if (symbolp operator)
|
|
1489
|
+
operator-form
|
|
1490
|
+
(eliscript-expander--expand-expression
|
|
1491
|
+
operator-form environment depth))
|
|
1492
|
+
(eliscript-expander--expand-sequence
|
|
1493
|
+
arguments environment depth))))
|
|
1494
|
+
form))))))))
|
|
1495
|
+
|
|
1496
|
+
(defun eliscript-expander--expand-top-level (form environment depth)
|
|
1497
|
+
"Expand top-level FORM in ENVIRONMENT at DEPTH and return zero or one forms."
|
|
1498
|
+
(let* ((value (eliscript-form-value form))
|
|
1499
|
+
(eliscript-expander--current-span
|
|
1500
|
+
(or (eliscript-form-span form) eliscript-expander--current-span)))
|
|
1501
|
+
(cond
|
|
1502
|
+
((not (consp value))
|
|
1503
|
+
(list (eliscript-expander--expand-expression form environment depth)))
|
|
1504
|
+
((not (proper-list-p value))
|
|
1505
|
+
(eliscript-expander--fail
|
|
1506
|
+
"dotted top-level form is not supported: %S"
|
|
1507
|
+
(eliscript-form-strip form)))
|
|
1508
|
+
(t
|
|
1509
|
+
(let* ((operator-form (car value))
|
|
1510
|
+
(operator (eliscript-form-value operator-form))
|
|
1511
|
+
(arguments (cdr value)))
|
|
1512
|
+
(cond
|
|
1513
|
+
((eq operator 'defmacro)
|
|
1514
|
+
(eliscript-expander--register form environment)
|
|
1515
|
+
nil)
|
|
1516
|
+
((eq operator 'defmulti)
|
|
1517
|
+
(eliscript-expander--expand-top-level
|
|
1518
|
+
(eliscript-expander--desugar-defmulti arguments)
|
|
1519
|
+
environment depth))
|
|
1520
|
+
((eq operator 'defmethod)
|
|
1521
|
+
(eliscript-expander--expand-top-level
|
|
1522
|
+
(eliscript-expander--desugar-defmethod arguments)
|
|
1523
|
+
environment depth))
|
|
1524
|
+
((eq operator 'defprotocol)
|
|
1525
|
+
(eliscript-expander--expand-top-level-sequence
|
|
1526
|
+
(eliscript-expander--desugar-defprotocol arguments)
|
|
1527
|
+
environment depth))
|
|
1528
|
+
((eq operator 'deftype)
|
|
1529
|
+
(eliscript-expander--expand-top-level-sequence
|
|
1530
|
+
(eliscript-expander--desugar-deftype arguments)
|
|
1531
|
+
environment depth))
|
|
1532
|
+
((eq operator 'defrecord)
|
|
1533
|
+
(eliscript-expander--expand-top-level-sequence
|
|
1534
|
+
(eliscript-expander--desugar-defrecord arguments)
|
|
1535
|
+
environment depth))
|
|
1536
|
+
((eq operator 'extend-type)
|
|
1537
|
+
(eliscript-expander--expand-top-level
|
|
1538
|
+
(eliscript-expander--desugar-extend-type arguments)
|
|
1539
|
+
environment depth))
|
|
1540
|
+
((eq operator 'extend-category)
|
|
1541
|
+
(eliscript-expander--expand-top-level
|
|
1542
|
+
(eliscript-expander--desugar-extend-category arguments)
|
|
1543
|
+
environment depth))
|
|
1544
|
+
((eq operator 'extend-default)
|
|
1545
|
+
(eliscript-expander--expand-top-level
|
|
1546
|
+
(eliscript-expander--desugar-extend-default arguments)
|
|
1547
|
+
environment depth))
|
|
1548
|
+
((and (memq operator '(defun defn defportable defasync))
|
|
1549
|
+
(>= (length arguments) 2)
|
|
1550
|
+
(eliscript-expander--multi-arity-clause-p
|
|
1551
|
+
(nth 1 arguments)))
|
|
1552
|
+
(eliscript-expander--expand-top-level
|
|
1553
|
+
(eliscript-expander--desugar-multi-arity
|
|
1554
|
+
operator-form (car arguments) (cdr arguments) form)
|
|
1555
|
+
environment depth))
|
|
1556
|
+
((and (symbolp operator) (gethash operator environment))
|
|
1557
|
+
(eliscript-expander--expand-top-level
|
|
1558
|
+
(eliscript-form-locate-generated
|
|
1559
|
+
(eliscript-expander--invoke
|
|
1560
|
+
(gethash operator environment) arguments depth)
|
|
1561
|
+
eliscript-expander--current-span)
|
|
1562
|
+
environment
|
|
1563
|
+
(1+ depth)))
|
|
1564
|
+
(t
|
|
1565
|
+
(list
|
|
1566
|
+
(eliscript-form-inherit
|
|
1567
|
+
(pcase operator
|
|
1568
|
+
('module
|
|
1569
|
+
(if arguments
|
|
1570
|
+
(cons operator-form
|
|
1571
|
+
(cons (car arguments)
|
|
1572
|
+
(eliscript-expander--expand-top-level-sequence
|
|
1573
|
+
(cdr arguments) environment depth)))
|
|
1574
|
+
value))
|
|
1575
|
+
((or 'defun 'defn 'defportable 'defasync)
|
|
1576
|
+
(if (>= (length arguments) 2)
|
|
1577
|
+
(cons operator-form
|
|
1578
|
+
(cons (nth 0 arguments)
|
|
1579
|
+
(cons (nth 1 arguments)
|
|
1580
|
+
(eliscript-expander--expand-sequence
|
|
1581
|
+
(nthcdr 2 arguments)
|
|
1582
|
+
environment depth))))
|
|
1583
|
+
value))
|
|
1584
|
+
((or 'defvar 'defconst)
|
|
1585
|
+
(cons operator-form
|
|
1586
|
+
(if arguments
|
|
1587
|
+
(cons (car arguments)
|
|
1588
|
+
(eliscript-expander--expand-sequence
|
|
1589
|
+
(cdr arguments) environment depth))
|
|
1590
|
+
nil)))
|
|
1591
|
+
((or 'import 'import-portable 'export) value)
|
|
1592
|
+
('export-default
|
|
1593
|
+
(cons operator-form
|
|
1594
|
+
(eliscript-expander--expand-sequence
|
|
1595
|
+
arguments environment depth)))
|
|
1596
|
+
(_
|
|
1597
|
+
(eliscript-form-value
|
|
1598
|
+
(eliscript-expander--expand-expression
|
|
1599
|
+
form environment depth))))
|
|
1600
|
+
form)))))))))
|
|
1601
|
+
|
|
1602
|
+
(defun eliscript-expander--expand-top-level-sequence
|
|
1603
|
+
(forms environment depth)
|
|
1604
|
+
"Expand top-level FORMS sequentially in ENVIRONMENT at DEPTH."
|
|
1605
|
+
(apply #'append
|
|
1606
|
+
(mapcar (lambda (form)
|
|
1607
|
+
(eliscript-expander--expand-top-level
|
|
1608
|
+
form environment depth))
|
|
1609
|
+
forms)))
|
|
1610
|
+
|
|
1611
|
+
(defun eliscript-expand-module (forms &optional filename macro-context)
|
|
1612
|
+
"Expand compile-time macros in module FORMS read from FILENAME.
|
|
1613
|
+
|
|
1614
|
+
MACRO-CONTEXT is a plist containing string-keyed `:capabilities' and `:files'
|
|
1615
|
+
hash tables. An omitted context exposes no host capabilities."
|
|
1616
|
+
(let ((reserved-names (make-hash-table :test #'equal)))
|
|
1617
|
+
(dolist (form forms)
|
|
1618
|
+
(eliscript-expander--collect-symbol-names form reserved-names))
|
|
1619
|
+
(let ((eliscript-expander--filename filename)
|
|
1620
|
+
(eliscript-expander--macro-context
|
|
1621
|
+
(eliscript-macro-eval--make-context
|
|
1622
|
+
reserved-names
|
|
1623
|
+
(plist-get macro-context :capabilities)
|
|
1624
|
+
(plist-get macro-context :files)))
|
|
1625
|
+
(environment (make-hash-table :test #'eq)))
|
|
1626
|
+
(eliscript-expander--expand-top-level-sequence forms environment 0))))
|
|
1627
|
+
|
|
1628
|
+
(provide 'eliscript-expander)
|
|
1629
|
+
|
|
1630
|
+
;;; eliscript-expander.el ends here
|