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,811 @@
|
|
|
1
|
+
;;; eliscript-macro-eval.el --- Deterministic macro evaluation -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; Macro bodies run over native, location-free Eliscript data. This evaluator
|
|
6
|
+
;; intentionally exposes a small pure language instead of the Emacs runtime.
|
|
7
|
+
|
|
8
|
+
;;; Code:
|
|
9
|
+
|
|
10
|
+
(require 'cl-lib)
|
|
11
|
+
(require 'subr-x)
|
|
12
|
+
(require 'eliscript-diagnostic)
|
|
13
|
+
|
|
14
|
+
(define-error 'eliscript-macro-eval-error
|
|
15
|
+
"Eliscript macro evaluation error")
|
|
16
|
+
|
|
17
|
+
(cl-defstruct (eliscript-macro-eval-parameters
|
|
18
|
+
(:constructor eliscript-macro-eval-parameters-create))
|
|
19
|
+
required
|
|
20
|
+
optional
|
|
21
|
+
rest)
|
|
22
|
+
|
|
23
|
+
(cl-defstruct (eliscript-macro-eval--scope
|
|
24
|
+
(:constructor eliscript-macro-eval--scope-create))
|
|
25
|
+
parent
|
|
26
|
+
values
|
|
27
|
+
context)
|
|
28
|
+
|
|
29
|
+
(cl-defstruct (eliscript-macro-eval--context
|
|
30
|
+
(:constructor eliscript-macro-eval--context-create))
|
|
31
|
+
counter
|
|
32
|
+
reserved-names
|
|
33
|
+
capabilities
|
|
34
|
+
files)
|
|
35
|
+
|
|
36
|
+
(defconst eliscript-macro-eval--missing (make-symbol "missing")
|
|
37
|
+
"Sentinel that distinguishes an absent value from source `undefined'.")
|
|
38
|
+
|
|
39
|
+
(defconst eliscript-macro-eval--quasiquote-symbol (intern "`")
|
|
40
|
+
"Reader symbol used for quasiquote forms.")
|
|
41
|
+
|
|
42
|
+
(defconst eliscript-macro-eval--unquote-symbol (intern ",")
|
|
43
|
+
"Reader symbol used for unquote forms.")
|
|
44
|
+
|
|
45
|
+
(defconst eliscript-macro-eval--splice-symbol (intern ",@")
|
|
46
|
+
"Reader symbol used for unquote-splicing forms.")
|
|
47
|
+
|
|
48
|
+
(defun eliscript-macro-eval--fail (format-string &rest arguments)
|
|
49
|
+
"Signal a macro evaluation error using FORMAT-STRING and ARGUMENTS."
|
|
50
|
+
(apply #'eliscript-diagnostic-signal
|
|
51
|
+
'eliscript-macro-eval-error "ELI-X0002" "macro-evaluation"
|
|
52
|
+
nil nil format-string arguments))
|
|
53
|
+
|
|
54
|
+
(defun eliscript-macro-eval--symbol-p (value)
|
|
55
|
+
"Return non-nil when VALUE is an ordinary Eliscript symbol."
|
|
56
|
+
(and (symbolp value)
|
|
57
|
+
value
|
|
58
|
+
(not (keywordp value))
|
|
59
|
+
(not (memq value '(t false undefined)))))
|
|
60
|
+
|
|
61
|
+
(defun eliscript-macro-eval-symbol-p (value)
|
|
62
|
+
"Return non-nil when VALUE is an ordinary Eliscript symbol."
|
|
63
|
+
(eliscript-macro-eval--symbol-p value))
|
|
64
|
+
|
|
65
|
+
(defun eliscript-macro-eval--list-p (value)
|
|
66
|
+
"Return non-nil when VALUE is a proper Eliscript list."
|
|
67
|
+
(or (null value) (and (consp value) (proper-list-p value))))
|
|
68
|
+
|
|
69
|
+
(defun eliscript-macro-eval--sequence-items (value)
|
|
70
|
+
"Return VALUE as list items, or the missing sentinel when not a sequence."
|
|
71
|
+
(cond
|
|
72
|
+
((null value) nil)
|
|
73
|
+
((and (consp value) (proper-list-p value)) value)
|
|
74
|
+
((vectorp value) (append value nil))
|
|
75
|
+
(t eliscript-macro-eval--missing)))
|
|
76
|
+
|
|
77
|
+
(defun eliscript-macro-eval--boolean (value)
|
|
78
|
+
"Return an Eliscript boolean for host predicate VALUE."
|
|
79
|
+
(if value t 'false))
|
|
80
|
+
|
|
81
|
+
(defun eliscript-macro-eval--truthy-p (value)
|
|
82
|
+
"Return non-nil when VALUE is true under Eliscript truthiness."
|
|
83
|
+
(not (memq value '(nil false undefined))))
|
|
84
|
+
|
|
85
|
+
(defun eliscript-macro-eval--display (value)
|
|
86
|
+
"Return a stable source-like display string for VALUE."
|
|
87
|
+
(cond
|
|
88
|
+
((null value) "nil")
|
|
89
|
+
((eq value t) "t")
|
|
90
|
+
((eq value 'false) "false")
|
|
91
|
+
((eq value 'undefined) "undefined")
|
|
92
|
+
((stringp value) (prin1-to-string value))
|
|
93
|
+
((symbolp value) (symbol-name value))
|
|
94
|
+
((numberp value) (number-to-string value))
|
|
95
|
+
(t (prin1-to-string value))))
|
|
96
|
+
|
|
97
|
+
(defun eliscript-macro-eval--value-string (value)
|
|
98
|
+
"Return VALUE converted by the macro `str' contract."
|
|
99
|
+
(cond
|
|
100
|
+
((stringp value) value)
|
|
101
|
+
((null value) "nil")
|
|
102
|
+
((eq value t) "true")
|
|
103
|
+
((symbolp value) (symbol-name value))
|
|
104
|
+
((numberp value) (number-to-string value))
|
|
105
|
+
(t (eliscript-macro-eval--display value))))
|
|
106
|
+
|
|
107
|
+
(defun eliscript-macro-eval--require-number (value operator)
|
|
108
|
+
"Return numeric VALUE or report that OPERATOR received another type."
|
|
109
|
+
(unless (numberp value)
|
|
110
|
+
(eliscript-macro-eval--fail
|
|
111
|
+
"%s expects numbers, got %s"
|
|
112
|
+
operator (eliscript-macro-eval--display value)))
|
|
113
|
+
value)
|
|
114
|
+
|
|
115
|
+
(defun eliscript-macro-eval--make-context
|
|
116
|
+
(&optional reserved-names capabilities files)
|
|
117
|
+
"Return a deterministic macro context.
|
|
118
|
+
|
|
119
|
+
RESERVED-NAMES controls generated-name allocation. CAPABILITIES and FILES
|
|
120
|
+
are string-keyed hash tables supplied by the compiler host."
|
|
121
|
+
(eliscript-macro-eval--context-create
|
|
122
|
+
:counter 0
|
|
123
|
+
:reserved-names
|
|
124
|
+
(or reserved-names (make-hash-table :test #'equal))
|
|
125
|
+
:capabilities
|
|
126
|
+
(or capabilities (make-hash-table :test #'equal))
|
|
127
|
+
:files
|
|
128
|
+
(or files (make-hash-table :test #'equal))))
|
|
129
|
+
|
|
130
|
+
(defun eliscript-macro-eval--read-file (arguments scope)
|
|
131
|
+
"Read one declared macro file from ARGUMENTS in SCOPE."
|
|
132
|
+
(unless (= (length arguments) 1)
|
|
133
|
+
(eliscript-macro-eval--fail "macro-read-file expects 1 argument"))
|
|
134
|
+
(let* ((specifier (eliscript-macro-eval--eval (car arguments) scope))
|
|
135
|
+
(context (eliscript-macro-eval--scope-context scope)))
|
|
136
|
+
(unless (and (stringp specifier) (not (string-empty-p specifier)))
|
|
137
|
+
(eliscript-macro-eval--fail
|
|
138
|
+
"macro-read-file expects a non-empty string"))
|
|
139
|
+
(unless (gethash "read-file"
|
|
140
|
+
(eliscript-macro-eval--context-capabilities context))
|
|
141
|
+
(eliscript-macro-eval--fail
|
|
142
|
+
"macro capability is not enabled: read-file"))
|
|
143
|
+
(let ((content
|
|
144
|
+
(gethash specifier
|
|
145
|
+
(eliscript-macro-eval--context-files context)
|
|
146
|
+
eliscript-macro-eval--missing)))
|
|
147
|
+
(when (eq content eliscript-macro-eval--missing)
|
|
148
|
+
(eliscript-macro-eval--fail
|
|
149
|
+
"macro file dependency is not declared: %s" specifier))
|
|
150
|
+
content)))
|
|
151
|
+
|
|
152
|
+
(defun eliscript-macro-eval--make-scope (&optional parent context)
|
|
153
|
+
"Return an empty evaluator scope with optional PARENT and CONTEXT."
|
|
154
|
+
(eliscript-macro-eval--scope-create
|
|
155
|
+
:parent parent
|
|
156
|
+
:values (make-hash-table :test #'eq)
|
|
157
|
+
:context (or context
|
|
158
|
+
(and parent (eliscript-macro-eval--scope-context parent))
|
|
159
|
+
(eliscript-macro-eval--make-context))))
|
|
160
|
+
|
|
161
|
+
(defun eliscript-macro-eval--reserve-name (context name)
|
|
162
|
+
"Reserve source symbol NAME in generated-name CONTEXT."
|
|
163
|
+
(puthash name t (eliscript-macro-eval--context-reserved-names context))
|
|
164
|
+
name)
|
|
165
|
+
|
|
166
|
+
(defun eliscript-macro-eval--gensym-prefix (value)
|
|
167
|
+
"Return a validated generated-name prefix represented by VALUE."
|
|
168
|
+
(let ((prefix
|
|
169
|
+
(cond
|
|
170
|
+
((stringp value) value)
|
|
171
|
+
((eliscript-macro-eval--symbol-p value) (symbol-name value))
|
|
172
|
+
(t
|
|
173
|
+
(eliscript-macro-eval--fail
|
|
174
|
+
"gensym prefix must be a symbol or string")))))
|
|
175
|
+
(when (or (string-empty-p prefix)
|
|
176
|
+
(not (string-match-p
|
|
177
|
+
"\\`[A-Za-z_$][A-Za-z0-9_$?!*+=<>-]*\\'" prefix))
|
|
178
|
+
(string-prefix-p "__eliscript_" prefix))
|
|
179
|
+
(eliscript-macro-eval--fail
|
|
180
|
+
"invalid gensym prefix: %s" prefix))
|
|
181
|
+
prefix))
|
|
182
|
+
|
|
183
|
+
(defun eliscript-macro-eval--allocate-name (scope prefix-value)
|
|
184
|
+
"Allocate a unique symbol in SCOPE using PREFIX-VALUE."
|
|
185
|
+
(let* ((context (eliscript-macro-eval--scope-context scope))
|
|
186
|
+
(prefix (eliscript-macro-eval--gensym-prefix prefix-value))
|
|
187
|
+
(reserved (eliscript-macro-eval--context-reserved-names context))
|
|
188
|
+
candidate)
|
|
189
|
+
(while
|
|
190
|
+
(progn
|
|
191
|
+
(setf (eliscript-macro-eval--context-counter context)
|
|
192
|
+
(1+ (eliscript-macro-eval--context-counter context)))
|
|
193
|
+
(setq candidate
|
|
194
|
+
(format "%s$G%d"
|
|
195
|
+
prefix
|
|
196
|
+
(eliscript-macro-eval--context-counter context)))
|
|
197
|
+
(gethash candidate reserved)))
|
|
198
|
+
(eliscript-macro-eval--reserve-name context candidate)
|
|
199
|
+
(intern candidate)))
|
|
200
|
+
|
|
201
|
+
(defun eliscript-macro-eval--auto-gensym (symbol scope names)
|
|
202
|
+
"Return the generated symbol for template SYMBOL in SCOPE and NAMES."
|
|
203
|
+
(let ((existing (gethash symbol names eliscript-macro-eval--missing)))
|
|
204
|
+
(if (not (eq existing eliscript-macro-eval--missing))
|
|
205
|
+
existing
|
|
206
|
+
(let* ((name (symbol-name symbol))
|
|
207
|
+
(generated
|
|
208
|
+
(eliscript-macro-eval--allocate-name
|
|
209
|
+
scope (substring name 0 -1))))
|
|
210
|
+
(puthash symbol generated names)
|
|
211
|
+
generated))))
|
|
212
|
+
|
|
213
|
+
(defun eliscript-macro-eval--scope-bind (scope name value)
|
|
214
|
+
"Bind NAME to VALUE in SCOPE and return VALUE."
|
|
215
|
+
(puthash name value (eliscript-macro-eval--scope-values scope))
|
|
216
|
+
value)
|
|
217
|
+
|
|
218
|
+
(defun eliscript-macro-eval--scope-find (scope name)
|
|
219
|
+
"Return the scope containing NAME, or nil when NAME is unbound."
|
|
220
|
+
(let (found)
|
|
221
|
+
(while (and scope (not found))
|
|
222
|
+
(let ((value
|
|
223
|
+
(gethash name (eliscript-macro-eval--scope-values scope)
|
|
224
|
+
eliscript-macro-eval--missing)))
|
|
225
|
+
(unless (eq value eliscript-macro-eval--missing)
|
|
226
|
+
(setq found scope)))
|
|
227
|
+
(setq scope (and (not found)
|
|
228
|
+
(eliscript-macro-eval--scope-parent scope))))
|
|
229
|
+
found))
|
|
230
|
+
|
|
231
|
+
(defun eliscript-macro-eval--scope-value (scope name)
|
|
232
|
+
"Return NAME's value visible from SCOPE."
|
|
233
|
+
(let ((owner (eliscript-macro-eval--scope-find scope name)))
|
|
234
|
+
(unless owner
|
|
235
|
+
(eliscript-macro-eval--fail "void variable: %s" name))
|
|
236
|
+
(gethash name (eliscript-macro-eval--scope-values owner))))
|
|
237
|
+
|
|
238
|
+
(defun eliscript-macro-eval--scope-set (scope name value)
|
|
239
|
+
"Set existing NAME to VALUE through SCOPE and return VALUE."
|
|
240
|
+
(let ((owner (eliscript-macro-eval--scope-find scope name)))
|
|
241
|
+
(unless owner
|
|
242
|
+
(eliscript-macro-eval--fail
|
|
243
|
+
"cannot set unbound macro variable: %s" name))
|
|
244
|
+
(puthash name value (eliscript-macro-eval--scope-values owner))
|
|
245
|
+
value))
|
|
246
|
+
|
|
247
|
+
(defun eliscript-macro-eval--operator-p (form operator)
|
|
248
|
+
"Return non-nil when FORM is a proper call to OPERATOR."
|
|
249
|
+
(and (consp form)
|
|
250
|
+
(proper-list-p form)
|
|
251
|
+
(eq (car form) operator)))
|
|
252
|
+
|
|
253
|
+
(defun eliscript-macro-eval--splice-p (form depth)
|
|
254
|
+
"Return non-nil when FORM is a comma-splice active at DEPTH."
|
|
255
|
+
(and (= depth 1)
|
|
256
|
+
(eliscript-macro-eval--operator-p
|
|
257
|
+
form eliscript-macro-eval--splice-symbol)))
|
|
258
|
+
|
|
259
|
+
(defun eliscript-macro-eval--quasiquote-items
|
|
260
|
+
(items scope depth auto-gensyms)
|
|
261
|
+
"Evaluate quasiquoted ITEMS in SCOPE at DEPTH using AUTO-GENSYMS."
|
|
262
|
+
(let (result)
|
|
263
|
+
(dolist (item items (nreverse result))
|
|
264
|
+
(if (eliscript-macro-eval--splice-p item depth)
|
|
265
|
+
(progn
|
|
266
|
+
(unless (= (length item) 2)
|
|
267
|
+
(eliscript-macro-eval--fail ",@ expects one value"))
|
|
268
|
+
(let* ((value (eliscript-macro-eval--eval (cadr item) scope))
|
|
269
|
+
(parts (eliscript-macro-eval--sequence-items value)))
|
|
270
|
+
(when (eq parts eliscript-macro-eval--missing)
|
|
271
|
+
(eliscript-macro-eval--fail
|
|
272
|
+
"cannot splice %s" (eliscript-macro-eval--display value)))
|
|
273
|
+
(dolist (part parts)
|
|
274
|
+
(push part result))))
|
|
275
|
+
(push (eliscript-macro-eval--quasiquote
|
|
276
|
+
item scope depth auto-gensyms)
|
|
277
|
+
result)))))
|
|
278
|
+
|
|
279
|
+
(defun eliscript-macro-eval--quasiquote
|
|
280
|
+
(form scope depth auto-gensyms)
|
|
281
|
+
"Evaluate quasiquoted FORM in SCOPE at DEPTH using AUTO-GENSYMS."
|
|
282
|
+
(cond
|
|
283
|
+
((vectorp form)
|
|
284
|
+
(apply #'vector
|
|
285
|
+
(eliscript-macro-eval--quasiquote-items
|
|
286
|
+
(append form nil) scope depth auto-gensyms)))
|
|
287
|
+
((and (= depth 1)
|
|
288
|
+
(eliscript-macro-eval--operator-p
|
|
289
|
+
form eliscript-macro-eval--unquote-symbol))
|
|
290
|
+
(unless (= (length form) 2)
|
|
291
|
+
(eliscript-macro-eval--fail ", expects one value"))
|
|
292
|
+
(eliscript-macro-eval--eval (cadr form) scope))
|
|
293
|
+
((and (= depth 1)
|
|
294
|
+
(eliscript-macro-eval--operator-p
|
|
295
|
+
form eliscript-macro-eval--splice-symbol))
|
|
296
|
+
(eliscript-macro-eval--fail ",@ is only valid inside a collection"))
|
|
297
|
+
((eliscript-macro-eval--operator-p
|
|
298
|
+
form eliscript-macro-eval--quasiquote-symbol)
|
|
299
|
+
(unless (= (length form) 2)
|
|
300
|
+
(eliscript-macro-eval--fail "` expects one value"))
|
|
301
|
+
(list (car form)
|
|
302
|
+
(eliscript-macro-eval--quasiquote
|
|
303
|
+
(cadr form) scope (1+ depth) auto-gensyms)))
|
|
304
|
+
((and (> depth 1)
|
|
305
|
+
(eliscript-macro-eval--operator-p
|
|
306
|
+
form eliscript-macro-eval--unquote-symbol))
|
|
307
|
+
(list (car form)
|
|
308
|
+
(eliscript-macro-eval--quasiquote
|
|
309
|
+
(cadr form) scope (1- depth) auto-gensyms)))
|
|
310
|
+
((eliscript-macro-eval--list-p form)
|
|
311
|
+
(eliscript-macro-eval--quasiquote-items
|
|
312
|
+
form scope depth auto-gensyms))
|
|
313
|
+
((and (= depth 1)
|
|
314
|
+
(eliscript-macro-eval--symbol-p form)
|
|
315
|
+
(string-suffix-p "$" (symbol-name form)))
|
|
316
|
+
(eliscript-macro-eval--auto-gensym form scope auto-gensyms))
|
|
317
|
+
(t form)))
|
|
318
|
+
|
|
319
|
+
(defun eliscript-macro-eval--eval-sequence (forms scope)
|
|
320
|
+
"Evaluate FORMS in SCOPE and return the final value."
|
|
321
|
+
(let ((result nil))
|
|
322
|
+
(dolist (form forms result)
|
|
323
|
+
(setq result (eliscript-macro-eval--eval form scope)))))
|
|
324
|
+
|
|
325
|
+
(defun eliscript-macro-eval--eval-args (forms scope)
|
|
326
|
+
"Evaluate argument FORMS in SCOPE from left to right."
|
|
327
|
+
(mapcar (lambda (form) (eliscript-macro-eval--eval form scope)) forms))
|
|
328
|
+
|
|
329
|
+
(defun eliscript-macro-eval--eval-if (arguments scope)
|
|
330
|
+
"Evaluate macro if ARGUMENTS in SCOPE."
|
|
331
|
+
(unless (<= 2 (length arguments) 3)
|
|
332
|
+
(eliscript-macro-eval--fail "if expects 2..3 arguments"))
|
|
333
|
+
(if (eliscript-macro-eval--truthy-p
|
|
334
|
+
(eliscript-macro-eval--eval (nth 0 arguments) scope))
|
|
335
|
+
(eliscript-macro-eval--eval (nth 1 arguments) scope)
|
|
336
|
+
(if (= (length arguments) 3)
|
|
337
|
+
(eliscript-macro-eval--eval (nth 2 arguments) scope)
|
|
338
|
+
nil)))
|
|
339
|
+
|
|
340
|
+
(defun eliscript-macro-eval--eval-cond (clauses scope)
|
|
341
|
+
"Evaluate macro cond CLAUSES in SCOPE."
|
|
342
|
+
(let ((result nil)
|
|
343
|
+
done)
|
|
344
|
+
(dolist (clause clauses result)
|
|
345
|
+
(unless done
|
|
346
|
+
(unless (and (eliscript-macro-eval--list-p clause) clause)
|
|
347
|
+
(eliscript-macro-eval--fail
|
|
348
|
+
"invalid macro cond clause: %s"
|
|
349
|
+
(eliscript-macro-eval--display clause)))
|
|
350
|
+
(let ((test-value
|
|
351
|
+
(eliscript-macro-eval--eval (car clause) scope)))
|
|
352
|
+
(when (eliscript-macro-eval--truthy-p test-value)
|
|
353
|
+
(setq result
|
|
354
|
+
(if (cdr clause)
|
|
355
|
+
(eliscript-macro-eval--eval-sequence
|
|
356
|
+
(cdr clause) scope)
|
|
357
|
+
test-value)
|
|
358
|
+
done t)))))))
|
|
359
|
+
|
|
360
|
+
(defun eliscript-macro-eval--parse-binding (binding)
|
|
361
|
+
"Return macro let BINDING as a name/value-form cons."
|
|
362
|
+
(cond
|
|
363
|
+
((eliscript-macro-eval--symbol-p binding)
|
|
364
|
+
(cons binding eliscript-macro-eval--missing))
|
|
365
|
+
((and (eliscript-macro-eval--list-p binding)
|
|
366
|
+
(<= 1 (length binding) 2)
|
|
367
|
+
(eliscript-macro-eval--symbol-p (car binding)))
|
|
368
|
+
(cons (car binding)
|
|
369
|
+
(if (cdr binding) (cadr binding) eliscript-macro-eval--missing)))
|
|
370
|
+
(t
|
|
371
|
+
(eliscript-macro-eval--fail
|
|
372
|
+
"invalid macro let binding: %s"
|
|
373
|
+
(eliscript-macro-eval--display binding)))))
|
|
374
|
+
|
|
375
|
+
(defun eliscript-macro-eval--eval-let (arguments scope sequential)
|
|
376
|
+
"Evaluate macro let ARGUMENTS in SCOPE, sequentially when SEQUENTIAL."
|
|
377
|
+
(unless arguments
|
|
378
|
+
(eliscript-macro-eval--fail "macro let requires a binding list"))
|
|
379
|
+
(let ((bindings (car arguments))
|
|
380
|
+
(child (eliscript-macro-eval--make-scope scope)))
|
|
381
|
+
(unless (eliscript-macro-eval--list-p bindings)
|
|
382
|
+
(eliscript-macro-eval--fail "macro let bindings must be a list"))
|
|
383
|
+
(if sequential
|
|
384
|
+
(dolist (binding bindings)
|
|
385
|
+
(pcase-let ((`(,name . ,value-form)
|
|
386
|
+
(eliscript-macro-eval--parse-binding binding)))
|
|
387
|
+
(eliscript-macro-eval--scope-bind
|
|
388
|
+
child name
|
|
389
|
+
(if (eq value-form eliscript-macro-eval--missing)
|
|
390
|
+
nil
|
|
391
|
+
(eliscript-macro-eval--eval value-form child)))))
|
|
392
|
+
(let (values)
|
|
393
|
+
(dolist (binding bindings)
|
|
394
|
+
(pcase-let ((`(,name . ,value-form)
|
|
395
|
+
(eliscript-macro-eval--parse-binding binding)))
|
|
396
|
+
(push (cons name
|
|
397
|
+
(if (eq value-form eliscript-macro-eval--missing)
|
|
398
|
+
nil
|
|
399
|
+
(eliscript-macro-eval--eval value-form scope)))
|
|
400
|
+
values)))
|
|
401
|
+
(dolist (binding (nreverse values))
|
|
402
|
+
(eliscript-macro-eval--scope-bind
|
|
403
|
+
child (car binding) (cdr binding)))))
|
|
404
|
+
(eliscript-macro-eval--eval-sequence (cdr arguments) child)))
|
|
405
|
+
|
|
406
|
+
(defun eliscript-macro-eval--eval-setq (arguments scope)
|
|
407
|
+
"Evaluate macro setq ARGUMENTS in SCOPE."
|
|
408
|
+
(when (or (null arguments) (cl-oddp (length arguments)))
|
|
409
|
+
(eliscript-macro-eval--fail "macro setq expects name/value pairs"))
|
|
410
|
+
(let ((result nil))
|
|
411
|
+
(while arguments
|
|
412
|
+
(let ((name (pop arguments))
|
|
413
|
+
(value-form (pop arguments)))
|
|
414
|
+
(unless (eliscript-macro-eval--symbol-p name)
|
|
415
|
+
(eliscript-macro-eval--fail
|
|
416
|
+
"macro setq target must be a symbol"))
|
|
417
|
+
(setq result (eliscript-macro-eval--eval value-form scope))
|
|
418
|
+
(eliscript-macro-eval--scope-set scope name result)))
|
|
419
|
+
result))
|
|
420
|
+
|
|
421
|
+
(defun eliscript-macro-eval--eval-list-operation (operator values)
|
|
422
|
+
"Evaluate list OPERATOR over VALUES, or return the missing sentinel."
|
|
423
|
+
(pcase operator
|
|
424
|
+
('list values)
|
|
425
|
+
('vector (apply #'vector values))
|
|
426
|
+
('car
|
|
427
|
+
(unless (= (length values) 1)
|
|
428
|
+
(eliscript-macro-eval--fail "car expects 1 argument"))
|
|
429
|
+
(let ((items (eliscript-macro-eval--sequence-items (car values))))
|
|
430
|
+
(when (eq items eliscript-macro-eval--missing)
|
|
431
|
+
(eliscript-macro-eval--fail "car expects a list or vector"))
|
|
432
|
+
(car items)))
|
|
433
|
+
('cdr
|
|
434
|
+
(unless (= (length values) 1)
|
|
435
|
+
(eliscript-macro-eval--fail "cdr expects 1 argument"))
|
|
436
|
+
(let ((items (eliscript-macro-eval--sequence-items (car values))))
|
|
437
|
+
(when (eq items eliscript-macro-eval--missing)
|
|
438
|
+
(eliscript-macro-eval--fail "cdr expects a list or vector"))
|
|
439
|
+
(cdr items)))
|
|
440
|
+
('nth
|
|
441
|
+
(unless (= (length values) 2)
|
|
442
|
+
(eliscript-macro-eval--fail "nth expects 2 arguments"))
|
|
443
|
+
(let* ((index (eliscript-macro-eval--require-number
|
|
444
|
+
(nth 0 values) "nth"))
|
|
445
|
+
(items (eliscript-macro-eval--sequence-items (nth 1 values))))
|
|
446
|
+
(when (eq items eliscript-macro-eval--missing)
|
|
447
|
+
(eliscript-macro-eval--fail "nth expects a list or vector"))
|
|
448
|
+
(if (and (integerp index) (>= index 0) (< index (length items)))
|
|
449
|
+
(nth index items)
|
|
450
|
+
nil)))
|
|
451
|
+
('length
|
|
452
|
+
(unless (= (length values) 1)
|
|
453
|
+
(eliscript-macro-eval--fail "length expects 1 argument"))
|
|
454
|
+
(let* ((value (car values))
|
|
455
|
+
(items (eliscript-macro-eval--sequence-items value)))
|
|
456
|
+
(cond
|
|
457
|
+
((not (eq items eliscript-macro-eval--missing)) (length items))
|
|
458
|
+
((stringp value)
|
|
459
|
+
(cl-loop for character across value
|
|
460
|
+
sum (if (> character #xffff) 2 1)))
|
|
461
|
+
(t (eliscript-macro-eval--fail "length expects a sequence")))))
|
|
462
|
+
('cons
|
|
463
|
+
(unless (= (length values) 2)
|
|
464
|
+
(eliscript-macro-eval--fail "cons expects 2 arguments"))
|
|
465
|
+
(let ((tail (eliscript-macro-eval--sequence-items (nth 1 values))))
|
|
466
|
+
(when (eq tail eliscript-macro-eval--missing)
|
|
467
|
+
(eliscript-macro-eval--fail "cons tail must be a list or vector"))
|
|
468
|
+
(cons (nth 0 values) tail)))
|
|
469
|
+
('append
|
|
470
|
+
(let (result)
|
|
471
|
+
(dolist (value values result)
|
|
472
|
+
(let ((parts (eliscript-macro-eval--sequence-items value)))
|
|
473
|
+
(when (eq parts eliscript-macro-eval--missing)
|
|
474
|
+
(eliscript-macro-eval--fail
|
|
475
|
+
"append expects lists or vectors"))
|
|
476
|
+
(setq result (append result parts))))))
|
|
477
|
+
(_ eliscript-macro-eval--missing)))
|
|
478
|
+
|
|
479
|
+
(defun eliscript-macro-eval--eq (left right)
|
|
480
|
+
"Return host truth for Eliscript identity between LEFT and RIGHT."
|
|
481
|
+
(cond
|
|
482
|
+
((and (symbolp left) (symbolp right)) (eq left right))
|
|
483
|
+
((and (numberp left) (numberp right)) (= left right))
|
|
484
|
+
((and (stringp left) (stringp right)) (equal left right))
|
|
485
|
+
(t (eq left right))))
|
|
486
|
+
|
|
487
|
+
(defun eliscript-macro-eval--eval-predicate (operator values)
|
|
488
|
+
"Evaluate predicate OPERATOR over VALUES, or return the missing sentinel."
|
|
489
|
+
(let ((arity (length values)))
|
|
490
|
+
(pcase operator
|
|
491
|
+
('not
|
|
492
|
+
(unless (= arity 1)
|
|
493
|
+
(eliscript-macro-eval--fail "not expects 1 argument"))
|
|
494
|
+
(eliscript-macro-eval--boolean
|
|
495
|
+
(not (eliscript-macro-eval--truthy-p (car values)))))
|
|
496
|
+
('nil?
|
|
497
|
+
(unless (= arity 1)
|
|
498
|
+
(eliscript-macro-eval--fail "nil? expects 1 argument"))
|
|
499
|
+
(eliscript-macro-eval--boolean (null (car values))))
|
|
500
|
+
('undefined?
|
|
501
|
+
(unless (= arity 1)
|
|
502
|
+
(eliscript-macro-eval--fail "undefined? expects 1 argument"))
|
|
503
|
+
(eliscript-macro-eval--boolean (eq (car values) 'undefined)))
|
|
504
|
+
((or 'null 'nullish?)
|
|
505
|
+
(unless (= arity 1)
|
|
506
|
+
(eliscript-macro-eval--fail "%s expects 1 argument" operator))
|
|
507
|
+
(eliscript-macro-eval--boolean
|
|
508
|
+
(memq (car values) '(nil undefined))))
|
|
509
|
+
('symbolp
|
|
510
|
+
(unless (= arity 1)
|
|
511
|
+
(eliscript-macro-eval--fail "symbolp expects 1 argument"))
|
|
512
|
+
(eliscript-macro-eval--boolean
|
|
513
|
+
(eliscript-macro-eval--symbol-p (car values))))
|
|
514
|
+
('keywordp
|
|
515
|
+
(unless (= arity 1)
|
|
516
|
+
(eliscript-macro-eval--fail "keywordp expects 1 argument"))
|
|
517
|
+
(eliscript-macro-eval--boolean (keywordp (car values))))
|
|
518
|
+
('listp
|
|
519
|
+
(unless (= arity 1)
|
|
520
|
+
(eliscript-macro-eval--fail "listp expects 1 argument"))
|
|
521
|
+
(eliscript-macro-eval--boolean
|
|
522
|
+
(eliscript-macro-eval--list-p (car values))))
|
|
523
|
+
('consp
|
|
524
|
+
(unless (= arity 1)
|
|
525
|
+
(eliscript-macro-eval--fail "consp expects 1 argument"))
|
|
526
|
+
(eliscript-macro-eval--boolean
|
|
527
|
+
(and (consp (car values)) (proper-list-p (car values)))))
|
|
528
|
+
('vectorp
|
|
529
|
+
(unless (= arity 1)
|
|
530
|
+
(eliscript-macro-eval--fail "vectorp expects 1 argument"))
|
|
531
|
+
(eliscript-macro-eval--boolean (vectorp (car values))))
|
|
532
|
+
('stringp
|
|
533
|
+
(unless (= arity 1)
|
|
534
|
+
(eliscript-macro-eval--fail "stringp expects 1 argument"))
|
|
535
|
+
(eliscript-macro-eval--boolean (stringp (car values))))
|
|
536
|
+
('numberp
|
|
537
|
+
(unless (= arity 1)
|
|
538
|
+
(eliscript-macro-eval--fail "numberp expects 1 argument"))
|
|
539
|
+
(eliscript-macro-eval--boolean (numberp (car values))))
|
|
540
|
+
('atom
|
|
541
|
+
(unless (= arity 1)
|
|
542
|
+
(eliscript-macro-eval--fail "atom expects 1 argument"))
|
|
543
|
+
(eliscript-macro-eval--boolean
|
|
544
|
+
(not (and (consp (car values)) (proper-list-p (car values))))))
|
|
545
|
+
('eq
|
|
546
|
+
(unless (= arity 2)
|
|
547
|
+
(eliscript-macro-eval--fail "eq expects 2 arguments"))
|
|
548
|
+
(eliscript-macro-eval--boolean
|
|
549
|
+
(eliscript-macro-eval--eq (nth 0 values) (nth 1 values))))
|
|
550
|
+
('equal
|
|
551
|
+
(unless (= arity 2)
|
|
552
|
+
(eliscript-macro-eval--fail "equal expects 2 arguments"))
|
|
553
|
+
(eliscript-macro-eval--boolean
|
|
554
|
+
(equal (nth 0 values) (nth 1 values))))
|
|
555
|
+
(_ eliscript-macro-eval--missing))))
|
|
556
|
+
|
|
557
|
+
(defun eliscript-macro-eval--normalize-number (value)
|
|
558
|
+
"Return integer VALUE when a finite float has no fractional part."
|
|
559
|
+
(if (and (floatp value)
|
|
560
|
+
(= value (truncate value)))
|
|
561
|
+
(truncate value)
|
|
562
|
+
value))
|
|
563
|
+
|
|
564
|
+
(defun eliscript-macro-eval--eval-arithmetic (operator values)
|
|
565
|
+
"Evaluate arithmetic OPERATOR over VALUES, or return the missing sentinel."
|
|
566
|
+
(if (not (memq operator
|
|
567
|
+
'(+ - * / % mod 1+ 1- = /= < <= > >=)))
|
|
568
|
+
eliscript-macro-eval--missing
|
|
569
|
+
(let ((arity (length values)))
|
|
570
|
+
(cond
|
|
571
|
+
((memq operator '(1+ 1-))
|
|
572
|
+
(unless (= arity 1)
|
|
573
|
+
(eliscript-macro-eval--fail "%s expects 1 argument" operator)))
|
|
574
|
+
((memq operator '(% mod = /= < <= > >=))
|
|
575
|
+
(unless (= arity 2)
|
|
576
|
+
(eliscript-macro-eval--fail "%s expects 2 arguments" operator)))
|
|
577
|
+
((memq operator '(- /))
|
|
578
|
+
(when (= arity 0)
|
|
579
|
+
(eliscript-macro-eval--fail
|
|
580
|
+
"%s expects one or more numbers" operator))))
|
|
581
|
+
(let ((numbers
|
|
582
|
+
(mapcar (lambda (value)
|
|
583
|
+
(eliscript-macro-eval--require-number value operator))
|
|
584
|
+
values)))
|
|
585
|
+
(pcase operator
|
|
586
|
+
('1+ (1+ (car numbers)))
|
|
587
|
+
('1- (1- (car numbers)))
|
|
588
|
+
('+ (apply #'+ numbers))
|
|
589
|
+
('* (apply #'* numbers))
|
|
590
|
+
('-
|
|
591
|
+
(if (= arity 1)
|
|
592
|
+
(- (car numbers))
|
|
593
|
+
(cl-reduce #'- (cdr numbers) :initial-value (car numbers))))
|
|
594
|
+
('/
|
|
595
|
+
(eliscript-macro-eval--normalize-number
|
|
596
|
+
(if (= arity 1)
|
|
597
|
+
(/ 1.0 (car numbers))
|
|
598
|
+
(cl-reduce (lambda (left right) (/ (float left) right))
|
|
599
|
+
(cdr numbers) :initial-value (car numbers)))))
|
|
600
|
+
((or '% 'mod) (% (nth 0 numbers) (nth 1 numbers)))
|
|
601
|
+
('= (eliscript-macro-eval--boolean
|
|
602
|
+
(= (nth 0 numbers) (nth 1 numbers))))
|
|
603
|
+
('/= (eliscript-macro-eval--boolean
|
|
604
|
+
(/= (nth 0 numbers) (nth 1 numbers))))
|
|
605
|
+
('< (eliscript-macro-eval--boolean
|
|
606
|
+
(< (nth 0 numbers) (nth 1 numbers))))
|
|
607
|
+
('<= (eliscript-macro-eval--boolean
|
|
608
|
+
(<= (nth 0 numbers) (nth 1 numbers))))
|
|
609
|
+
('> (eliscript-macro-eval--boolean
|
|
610
|
+
(> (nth 0 numbers) (nth 1 numbers))))
|
|
611
|
+
('>= (eliscript-macro-eval--boolean
|
|
612
|
+
(>= (nth 0 numbers) (nth 1 numbers)))))))))
|
|
613
|
+
|
|
614
|
+
(defun eliscript-macro-eval--eval-call (form scope)
|
|
615
|
+
"Evaluate macro call FORM in SCOPE."
|
|
616
|
+
(let ((operator (car form))
|
|
617
|
+
(arguments (cdr form)))
|
|
618
|
+
(unless (eliscript-macro-eval--symbol-p operator)
|
|
619
|
+
(eliscript-macro-eval--fail "macro call operator must be a symbol"))
|
|
620
|
+
(if (eq operator eliscript-macro-eval--quasiquote-symbol)
|
|
621
|
+
(progn
|
|
622
|
+
(unless (= (length arguments) 1)
|
|
623
|
+
(eliscript-macro-eval--fail "` expects 1 argument"))
|
|
624
|
+
(eliscript-macro-eval--quasiquote
|
|
625
|
+
(car arguments) scope 1 (make-hash-table :test #'eq)))
|
|
626
|
+
(pcase operator
|
|
627
|
+
('quote
|
|
628
|
+
(unless (= (length arguments) 1)
|
|
629
|
+
(eliscript-macro-eval--fail "quote expects 1 argument"))
|
|
630
|
+
(car arguments))
|
|
631
|
+
('if (eliscript-macro-eval--eval-if arguments scope))
|
|
632
|
+
('when
|
|
633
|
+
(if (and arguments
|
|
634
|
+
(eliscript-macro-eval--truthy-p
|
|
635
|
+
(eliscript-macro-eval--eval (car arguments) scope)))
|
|
636
|
+
(eliscript-macro-eval--eval-sequence (cdr arguments) scope)
|
|
637
|
+
nil))
|
|
638
|
+
('unless
|
|
639
|
+
(if (and arguments
|
|
640
|
+
(not (eliscript-macro-eval--truthy-p
|
|
641
|
+
(eliscript-macro-eval--eval
|
|
642
|
+
(car arguments) scope))))
|
|
643
|
+
(eliscript-macro-eval--eval-sequence (cdr arguments) scope)
|
|
644
|
+
nil))
|
|
645
|
+
((or 'progn 'do)
|
|
646
|
+
(eliscript-macro-eval--eval-sequence arguments scope))
|
|
647
|
+
('let (eliscript-macro-eval--eval-let arguments scope nil))
|
|
648
|
+
('let* (eliscript-macro-eval--eval-let arguments scope t))
|
|
649
|
+
('setq (eliscript-macro-eval--eval-setq arguments scope))
|
|
650
|
+
('cond (eliscript-macro-eval--eval-cond arguments scope))
|
|
651
|
+
('and
|
|
652
|
+
(let ((result t))
|
|
653
|
+
(while (and arguments
|
|
654
|
+
(eliscript-macro-eval--truthy-p result))
|
|
655
|
+
(setq result (eliscript-macro-eval--eval
|
|
656
|
+
(pop arguments) scope)))
|
|
657
|
+
result))
|
|
658
|
+
('or
|
|
659
|
+
(let ((result nil))
|
|
660
|
+
(while (and arguments
|
|
661
|
+
(not (eliscript-macro-eval--truthy-p result)))
|
|
662
|
+
(setq result (eliscript-macro-eval--eval
|
|
663
|
+
(pop arguments) scope)))
|
|
664
|
+
result))
|
|
665
|
+
('error
|
|
666
|
+
(unless arguments
|
|
667
|
+
(eliscript-macro-eval--fail "error expects a message"))
|
|
668
|
+
(eliscript-macro-eval--fail
|
|
669
|
+
"%s" (eliscript-macro-eval--value-string
|
|
670
|
+
(eliscript-macro-eval--eval (car arguments) scope))))
|
|
671
|
+
('gensym
|
|
672
|
+
(unless (<= (length arguments) 1)
|
|
673
|
+
(eliscript-macro-eval--fail
|
|
674
|
+
"gensym expects 0..1 arguments"))
|
|
675
|
+
(eliscript-macro-eval--allocate-name
|
|
676
|
+
scope
|
|
677
|
+
(if arguments
|
|
678
|
+
(eliscript-macro-eval--eval (car arguments) scope)
|
|
679
|
+
"G")))
|
|
680
|
+
('macro-read-file
|
|
681
|
+
(eliscript-macro-eval--read-file arguments scope))
|
|
682
|
+
(_
|
|
683
|
+
(let* ((values (eliscript-macro-eval--eval-args arguments scope))
|
|
684
|
+
(list-result
|
|
685
|
+
(eliscript-macro-eval--eval-list-operation operator values))
|
|
686
|
+
(predicate-result
|
|
687
|
+
(if (eq list-result eliscript-macro-eval--missing)
|
|
688
|
+
(eliscript-macro-eval--eval-predicate operator values)
|
|
689
|
+
eliscript-macro-eval--missing))
|
|
690
|
+
(arithmetic-result
|
|
691
|
+
(if (and (eq list-result eliscript-macro-eval--missing)
|
|
692
|
+
(eq predicate-result eliscript-macro-eval--missing))
|
|
693
|
+
(eliscript-macro-eval--eval-arithmetic operator values)
|
|
694
|
+
eliscript-macro-eval--missing)))
|
|
695
|
+
(cond
|
|
696
|
+
((not (eq list-result eliscript-macro-eval--missing)) list-result)
|
|
697
|
+
((not (eq predicate-result eliscript-macro-eval--missing))
|
|
698
|
+
predicate-result)
|
|
699
|
+
((not (eq arithmetic-result eliscript-macro-eval--missing))
|
|
700
|
+
arithmetic-result)
|
|
701
|
+
((eq operator 'symbol-name)
|
|
702
|
+
(unless (and (= (length values) 1)
|
|
703
|
+
(eliscript-macro-eval--symbol-p (car values)))
|
|
704
|
+
(eliscript-macro-eval--fail
|
|
705
|
+
"symbol-name expects a symbol"))
|
|
706
|
+
(symbol-name (car values)))
|
|
707
|
+
((eq operator 'intern)
|
|
708
|
+
(unless (and (= (length values) 1) (stringp (car values)))
|
|
709
|
+
(eliscript-macro-eval--fail "intern expects a string"))
|
|
710
|
+
(let ((name (car values)))
|
|
711
|
+
(eliscript-macro-eval--reserve-name
|
|
712
|
+
(eliscript-macro-eval--scope-context scope) name)
|
|
713
|
+
(intern name)))
|
|
714
|
+
((memq operator '(concat str))
|
|
715
|
+
(mapconcat #'eliscript-macro-eval--value-string values ""))
|
|
716
|
+
(t
|
|
717
|
+
(eliscript-macro-eval--fail
|
|
718
|
+
"unsupported macro function: %s" operator)))))))))
|
|
719
|
+
|
|
720
|
+
(defun eliscript-macro-eval--eval (form scope)
|
|
721
|
+
"Evaluate location-free macro FORM in SCOPE."
|
|
722
|
+
(cond
|
|
723
|
+
((or (null form)
|
|
724
|
+
(eq form t)
|
|
725
|
+
(memq form '(false undefined))
|
|
726
|
+
(numberp form)
|
|
727
|
+
(stringp form)
|
|
728
|
+
(keywordp form)
|
|
729
|
+
(vectorp form))
|
|
730
|
+
form)
|
|
731
|
+
((eliscript-macro-eval--symbol-p form)
|
|
732
|
+
(eliscript-macro-eval--scope-value scope form))
|
|
733
|
+
((and (consp form) (proper-list-p form))
|
|
734
|
+
(eliscript-macro-eval--eval-call form scope))
|
|
735
|
+
(t
|
|
736
|
+
(eliscript-macro-eval--fail
|
|
737
|
+
"unsupported macro value: %s"
|
|
738
|
+
(eliscript-macro-eval--display form)))))
|
|
739
|
+
|
|
740
|
+
(defun eliscript-macro-eval-parse-parameters (parameters)
|
|
741
|
+
"Parse location-free macro PARAMETERS into a deterministic descriptor."
|
|
742
|
+
(unless (eliscript-macro-eval--list-p parameters)
|
|
743
|
+
(eliscript-macro-eval--fail
|
|
744
|
+
"macro parameters must be a list: %s"
|
|
745
|
+
(eliscript-macro-eval--display parameters)))
|
|
746
|
+
(let (required optional
|
|
747
|
+
(rest eliscript-macro-eval--missing)
|
|
748
|
+
(mode 'required))
|
|
749
|
+
(dolist (parameter parameters)
|
|
750
|
+
(unless (eliscript-macro-eval--symbol-p parameter)
|
|
751
|
+
(eliscript-macro-eval--fail
|
|
752
|
+
"macro parameters must be symbols: %s"
|
|
753
|
+
(eliscript-macro-eval--display parameters)))
|
|
754
|
+
(cond
|
|
755
|
+
((eq parameter '&optional)
|
|
756
|
+
(when (or (eq mode 'rest)
|
|
757
|
+
(not (eq rest eliscript-macro-eval--missing)))
|
|
758
|
+
(eliscript-macro-eval--fail "invalid macro parameter list"))
|
|
759
|
+
(setq mode 'optional))
|
|
760
|
+
((memq parameter '(&rest &body))
|
|
761
|
+
(when (or (eq mode 'rest)
|
|
762
|
+
(not (eq rest eliscript-macro-eval--missing)))
|
|
763
|
+
(eliscript-macro-eval--fail "invalid macro parameter list"))
|
|
764
|
+
(setq mode 'rest))
|
|
765
|
+
((eq mode 'rest)
|
|
766
|
+
(setq rest parameter
|
|
767
|
+
mode 'done))
|
|
768
|
+
((eq mode 'done)
|
|
769
|
+
(eliscript-macro-eval--fail "invalid macro parameter list"))
|
|
770
|
+
((eq mode 'required) (push parameter required))
|
|
771
|
+
(t (push parameter optional))))
|
|
772
|
+
(when (eq mode 'rest)
|
|
773
|
+
(eliscript-macro-eval--fail "invalid macro parameter list"))
|
|
774
|
+
(eliscript-macro-eval-parameters-create
|
|
775
|
+
:required (nreverse required)
|
|
776
|
+
:optional (nreverse optional)
|
|
777
|
+
:rest rest)))
|
|
778
|
+
|
|
779
|
+
(defun eliscript-macro-eval-run
|
|
780
|
+
(name parameters body arguments &optional context)
|
|
781
|
+
"Evaluate macro NAME with PARAMETERS, BODY, raw ARGUMENTS, and CONTEXT."
|
|
782
|
+
(let* ((required (eliscript-macro-eval-parameters-required parameters))
|
|
783
|
+
(optional (eliscript-macro-eval-parameters-optional parameters))
|
|
784
|
+
(rest (eliscript-macro-eval-parameters-rest parameters))
|
|
785
|
+
(minimum (length required))
|
|
786
|
+
(maximum (+ minimum (length optional)))
|
|
787
|
+
(count (length arguments))
|
|
788
|
+
(scope (eliscript-macro-eval--make-scope nil context))
|
|
789
|
+
(index 0))
|
|
790
|
+
(when (< count minimum)
|
|
791
|
+
(eliscript-macro-eval--fail
|
|
792
|
+
"wrong number of arguments for %s" name))
|
|
793
|
+
(when (and (eq rest eliscript-macro-eval--missing)
|
|
794
|
+
(> count maximum))
|
|
795
|
+
(eliscript-macro-eval--fail
|
|
796
|
+
"wrong number of arguments for %s" name))
|
|
797
|
+
(dolist (parameter required)
|
|
798
|
+
(eliscript-macro-eval--scope-bind
|
|
799
|
+
scope parameter (nth index arguments))
|
|
800
|
+
(setq index (1+ index)))
|
|
801
|
+
(dolist (parameter optional)
|
|
802
|
+
(eliscript-macro-eval--scope-bind
|
|
803
|
+
scope parameter (if (< index count) (nth index arguments) nil))
|
|
804
|
+
(setq index (1+ index)))
|
|
805
|
+
(unless (eq rest eliscript-macro-eval--missing)
|
|
806
|
+
(eliscript-macro-eval--scope-bind scope rest (nthcdr index arguments)))
|
|
807
|
+
(eliscript-macro-eval--eval-sequence body scope)))
|
|
808
|
+
|
|
809
|
+
(provide 'eliscript-macro-eval)
|
|
810
|
+
|
|
811
|
+
;;; eliscript-macro-eval.el ends here
|