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,716 @@
|
|
|
1
|
+
;;; eliscript-analyzer.el --- Lexical analysis for Eliscript -*- lexical-binding: t; -*-
|
|
2
|
+
|
|
3
|
+
;;; Commentary:
|
|
4
|
+
|
|
5
|
+
;; The analyzer validates lexical bindings before lowering to explicit IR.
|
|
6
|
+
|
|
7
|
+
;;; Code:
|
|
8
|
+
|
|
9
|
+
(require 'cl-lib)
|
|
10
|
+
(require 'eliscript-diagnostic)
|
|
11
|
+
(require 'eliscript-form)
|
|
12
|
+
(require 'eliscript-parameters)
|
|
13
|
+
(require 'eliscript-portable)
|
|
14
|
+
(require 'eliscript-symbol)
|
|
15
|
+
(require 'eliscript-transient-analysis)
|
|
16
|
+
|
|
17
|
+
(cl-defstruct (eliscript-analyzer--binding
|
|
18
|
+
(:constructor eliscript-analyzer--binding-create))
|
|
19
|
+
name
|
|
20
|
+
output-name
|
|
21
|
+
kind
|
|
22
|
+
mutable)
|
|
23
|
+
|
|
24
|
+
(cl-defstruct (eliscript-analyzer--scope
|
|
25
|
+
(:constructor eliscript-analyzer--scope-create))
|
|
26
|
+
parent
|
|
27
|
+
names
|
|
28
|
+
outputs
|
|
29
|
+
function-context)
|
|
30
|
+
|
|
31
|
+
(cl-defstruct (eliscript-analyzer--recur-target
|
|
32
|
+
(:constructor eliscript-analyzer--recur-target-create))
|
|
33
|
+
kind
|
|
34
|
+
arity)
|
|
35
|
+
|
|
36
|
+
(defconst eliscript-analyzer--builtin-operators
|
|
37
|
+
'(if when unless progn do while loop recur and or not
|
|
38
|
+
+ * - / % mod = /= not= < <= > >= 1+ 1-
|
|
39
|
+
int32 uint32 imul32 bit-and bit-or bit-xor bit-not
|
|
40
|
+
bit-shift-left bit-shift-right unsigned-bit-shift-right
|
|
41
|
+
value-type host-identity-token string-code-unit-at string-from-code-unit
|
|
42
|
+
string-to-number string-to-bigint number-float64-words
|
|
43
|
+
eq equal null nil? undefined? nullish?
|
|
44
|
+
list vector hash-map hash-set queue js-array car cdr cons js-cons nth js-nth aref
|
|
45
|
+
length js-length
|
|
46
|
+
object-keys object-has? object-assoc __eliscript_arity_error
|
|
47
|
+
new print str funcall apply))
|
|
48
|
+
|
|
49
|
+
(defvar eliscript-analyzer--filename nil)
|
|
50
|
+
(defvar eliscript-analyzer--current-span nil)
|
|
51
|
+
(defvar eliscript-analyzer--tail-position nil)
|
|
52
|
+
(defvar eliscript-analyzer--recur-targets nil)
|
|
53
|
+
(defvar eliscript-analyzer--recur-barrier nil)
|
|
54
|
+
|
|
55
|
+
(defun eliscript-analyzer--fail (format-string &rest arguments)
|
|
56
|
+
"Signal an analysis error using FORMAT-STRING and ARGUMENTS."
|
|
57
|
+
(apply #'eliscript-diagnostic-signal
|
|
58
|
+
'eliscript-analyze-error "ELI-A0001" "analysis"
|
|
59
|
+
eliscript-analyzer--filename eliscript-analyzer--current-span
|
|
60
|
+
format-string arguments))
|
|
61
|
+
|
|
62
|
+
(defun eliscript-analyzer--make-scope (&optional parent function-context)
|
|
63
|
+
"Create a lexical scope under PARENT with FUNCTION-CONTEXT."
|
|
64
|
+
(eliscript-analyzer--scope-create
|
|
65
|
+
:parent parent
|
|
66
|
+
:names (make-hash-table :test #'eq)
|
|
67
|
+
:outputs (make-hash-table :test #'equal)
|
|
68
|
+
:function-context function-context))
|
|
69
|
+
|
|
70
|
+
(defun eliscript-analyzer--async-function-p (scope)
|
|
71
|
+
"Return non-nil when SCOPE is inside its nearest async function."
|
|
72
|
+
(let (context)
|
|
73
|
+
(while (and scope (not context))
|
|
74
|
+
(setq context (eliscript-analyzer--scope-function-context scope))
|
|
75
|
+
(setq scope (eliscript-analyzer--scope-parent scope)))
|
|
76
|
+
(eq context 'async)))
|
|
77
|
+
|
|
78
|
+
(defun eliscript-analyzer--map-binding-name (name)
|
|
79
|
+
"Return NAME's output identifier as an analysis diagnostic."
|
|
80
|
+
(condition-case error-data
|
|
81
|
+
(eliscript-symbol-binding-name name)
|
|
82
|
+
(eliscript-compile-error
|
|
83
|
+
(eliscript-analyzer--fail
|
|
84
|
+
"%s" (eliscript-diagnostic-condition-message error-data)))))
|
|
85
|
+
|
|
86
|
+
(defun eliscript-analyzer--declare (scope name kind mutable)
|
|
87
|
+
"Declare NAME with KIND and MUTABLE status in SCOPE."
|
|
88
|
+
(let* ((eliscript-analyzer--current-span
|
|
89
|
+
(or (eliscript-form-span name) eliscript-analyzer--current-span))
|
|
90
|
+
(name (eliscript-form-value name))
|
|
91
|
+
(names (eliscript-analyzer--scope-names scope))
|
|
92
|
+
(outputs (eliscript-analyzer--scope-outputs scope))
|
|
93
|
+
(output-name (eliscript-analyzer--map-binding-name name))
|
|
94
|
+
(existing-name (gethash name names))
|
|
95
|
+
(existing-output (gethash output-name outputs)))
|
|
96
|
+
(when existing-name
|
|
97
|
+
(eliscript-analyzer--fail "duplicate %s binding: %s" kind name))
|
|
98
|
+
(when existing-output
|
|
99
|
+
(eliscript-analyzer--fail
|
|
100
|
+
"binding %s emits %s, which collides with %s"
|
|
101
|
+
name output-name (eliscript-analyzer--binding-name existing-output)))
|
|
102
|
+
(let ((binding (eliscript-analyzer--binding-create
|
|
103
|
+
:name name
|
|
104
|
+
:output-name output-name
|
|
105
|
+
:kind kind
|
|
106
|
+
:mutable mutable)))
|
|
107
|
+
(puthash name binding names)
|
|
108
|
+
(puthash output-name binding outputs)
|
|
109
|
+
binding)))
|
|
110
|
+
|
|
111
|
+
(defun eliscript-analyzer--declare-pattern (scope pattern kind mutable)
|
|
112
|
+
"Declare every name in binding PATTERN in SCOPE."
|
|
113
|
+
(dolist
|
|
114
|
+
(name
|
|
115
|
+
(eliscript-binding-names
|
|
116
|
+
pattern
|
|
117
|
+
(lambda (form message)
|
|
118
|
+
(let ((eliscript-analyzer--current-span
|
|
119
|
+
(or (eliscript-form-span form)
|
|
120
|
+
eliscript-analyzer--current-span)))
|
|
121
|
+
(eliscript-analyzer--fail "%s" message)))))
|
|
122
|
+
(eliscript-analyzer--declare scope name kind mutable)))
|
|
123
|
+
|
|
124
|
+
(defun eliscript-analyzer--analyze-pattern-defaults (pattern scope)
|
|
125
|
+
"Analyze map defaults nested in binding PATTERN using lexical SCOPE."
|
|
126
|
+
(dolist
|
|
127
|
+
(default
|
|
128
|
+
(eliscript-binding-defaults
|
|
129
|
+
pattern
|
|
130
|
+
(lambda (form message)
|
|
131
|
+
(let ((eliscript-analyzer--current-span
|
|
132
|
+
(or (eliscript-form-span form)
|
|
133
|
+
eliscript-analyzer--current-span)))
|
|
134
|
+
(eliscript-analyzer--fail "%s" message)))))
|
|
135
|
+
(eliscript-analyzer--analyze-non-tail default scope)))
|
|
136
|
+
|
|
137
|
+
(defun eliscript-analyzer--lookup (scope name)
|
|
138
|
+
"Resolve NAME from SCOPE or its parents."
|
|
139
|
+
(let (binding)
|
|
140
|
+
(while (and scope (not binding))
|
|
141
|
+
(setq binding (gethash name (eliscript-analyzer--scope-names scope)))
|
|
142
|
+
(setq scope (eliscript-analyzer--scope-parent scope)))
|
|
143
|
+
binding))
|
|
144
|
+
|
|
145
|
+
(defun eliscript-analyzer--qualified-symbol-p (symbol)
|
|
146
|
+
"Return non-nil when SYMBOL is an explicit JavaScript path."
|
|
147
|
+
(string-match-p "[./]" (symbol-name symbol)))
|
|
148
|
+
|
|
149
|
+
(defun eliscript-analyzer--require-binding (scope name)
|
|
150
|
+
"Resolve NAME in SCOPE and signal when it is unbound."
|
|
151
|
+
(or (eliscript-analyzer--lookup scope name)
|
|
152
|
+
(eliscript-analyzer--fail "unbound symbol: %s" name)))
|
|
153
|
+
|
|
154
|
+
(defun eliscript-analyzer--analyze-sequence (forms scope)
|
|
155
|
+
"Analyze every form in FORMS using SCOPE."
|
|
156
|
+
(let ((tail-position eliscript-analyzer--tail-position))
|
|
157
|
+
(while forms
|
|
158
|
+
(let ((eliscript-analyzer--tail-position
|
|
159
|
+
(and tail-position (null (cdr forms)))))
|
|
160
|
+
(eliscript-analyzer--analyze-expression (car forms) scope))
|
|
161
|
+
(setq forms (cdr forms)))))
|
|
162
|
+
|
|
163
|
+
(defun eliscript-analyzer--analyze-non-tail (form scope)
|
|
164
|
+
"Analyze FORM in SCOPE outside a recur tail position."
|
|
165
|
+
(let ((eliscript-analyzer--tail-position nil))
|
|
166
|
+
(eliscript-analyzer--analyze-expression form scope)))
|
|
167
|
+
|
|
168
|
+
(defun eliscript-analyzer--analyze-non-tail-sequence (forms scope)
|
|
169
|
+
"Analyze every FORM in FORMS outside a recur tail position."
|
|
170
|
+
(let ((eliscript-analyzer--tail-position nil))
|
|
171
|
+
(eliscript-analyzer--analyze-sequence forms scope)))
|
|
172
|
+
|
|
173
|
+
(defun eliscript-analyzer--binding-pair (binding)
|
|
174
|
+
"Return the source name and initializer represented by BINDING."
|
|
175
|
+
(let* ((eliscript-analyzer--current-span
|
|
176
|
+
(or (eliscript-form-span binding) eliscript-analyzer--current-span))
|
|
177
|
+
(value (eliscript-form-value binding)))
|
|
178
|
+
(cond
|
|
179
|
+
((eliscript-binding-pattern-p binding)
|
|
180
|
+
(eliscript-binding-names
|
|
181
|
+
binding
|
|
182
|
+
(lambda (form message)
|
|
183
|
+
(let ((eliscript-analyzer--current-span
|
|
184
|
+
(or (eliscript-form-span form)
|
|
185
|
+
eliscript-analyzer--current-span)))
|
|
186
|
+
(eliscript-analyzer--fail "%s" message))))
|
|
187
|
+
(list binding nil))
|
|
188
|
+
((and (proper-list-p value)
|
|
189
|
+
(<= 1 (length value))
|
|
190
|
+
(<= (length value) 2)
|
|
191
|
+
(eliscript-binding-pattern-p (car value)))
|
|
192
|
+
(eliscript-binding-names
|
|
193
|
+
(car value)
|
|
194
|
+
(lambda (form message)
|
|
195
|
+
(let ((eliscript-analyzer--current-span
|
|
196
|
+
(or (eliscript-form-span form)
|
|
197
|
+
eliscript-analyzer--current-span)))
|
|
198
|
+
(eliscript-analyzer--fail "%s" message))))
|
|
199
|
+
(list (car value) (cadr value)))
|
|
200
|
+
(t (eliscript-analyzer--fail
|
|
201
|
+
"invalid let binding: %S" (eliscript-form-strip binding))))))
|
|
202
|
+
|
|
203
|
+
(defun eliscript-analyzer--analyze-let (arguments scope sequential)
|
|
204
|
+
"Analyze let ARGUMENTS in SCOPE, honoring SEQUENTIAL semantics."
|
|
205
|
+
(unless arguments
|
|
206
|
+
(eliscript-analyzer--fail "%s requires a binding list"
|
|
207
|
+
(if sequential "let*" "let")))
|
|
208
|
+
(let* ((bindings-form (car arguments))
|
|
209
|
+
(bindings (eliscript-form-value bindings-form))
|
|
210
|
+
(body (cdr arguments))
|
|
211
|
+
(child (eliscript-analyzer--make-scope scope)))
|
|
212
|
+
(unless (proper-list-p bindings)
|
|
213
|
+
(let ((eliscript-analyzer--current-span
|
|
214
|
+
(or (eliscript-form-span bindings-form)
|
|
215
|
+
eliscript-analyzer--current-span)))
|
|
216
|
+
(eliscript-analyzer--fail "let bindings must be a list")))
|
|
217
|
+
(if sequential
|
|
218
|
+
(dolist (binding bindings)
|
|
219
|
+
(pcase-let ((`(,name ,value)
|
|
220
|
+
(eliscript-analyzer--binding-pair binding)))
|
|
221
|
+
(eliscript-analyzer--analyze-non-tail value child)
|
|
222
|
+
(eliscript-analyzer--analyze-pattern-defaults name child)
|
|
223
|
+
(eliscript-analyzer--declare-pattern child name 'local t)))
|
|
224
|
+
(let ((parsed (mapcar #'eliscript-analyzer--binding-pair bindings)))
|
|
225
|
+
(dolist (binding parsed)
|
|
226
|
+
(eliscript-analyzer--analyze-non-tail (cadr binding) scope)
|
|
227
|
+
(eliscript-analyzer--analyze-pattern-defaults
|
|
228
|
+
(car binding) scope))
|
|
229
|
+
(dolist (binding parsed)
|
|
230
|
+
(eliscript-analyzer--declare-pattern
|
|
231
|
+
child (car binding) 'local t))))
|
|
232
|
+
(eliscript-analyzer--analyze-sequence body child)))
|
|
233
|
+
|
|
234
|
+
(defun eliscript-analyzer--analyze-function
|
|
235
|
+
(parameters body scope &optional asynchronous)
|
|
236
|
+
"Analyze function PARAMETERS and BODY in SCOPE.
|
|
237
|
+
|
|
238
|
+
When ASYNCHRONOUS is non-nil, allow `await' in this function body."
|
|
239
|
+
(let ((parsed
|
|
240
|
+
(eliscript-parameters-parse
|
|
241
|
+
parameters
|
|
242
|
+
(lambda (form message)
|
|
243
|
+
(let ((eliscript-analyzer--current-span
|
|
244
|
+
(or (eliscript-form-span form)
|
|
245
|
+
eliscript-analyzer--current-span)))
|
|
246
|
+
(eliscript-analyzer--fail "%s" message))))))
|
|
247
|
+
(dolist (parameter parsed)
|
|
248
|
+
(eliscript-analyzer--analyze-pattern-defaults
|
|
249
|
+
(eliscript-parameter-form parameter) scope))
|
|
250
|
+
(let ((child (eliscript-analyzer--make-scope
|
|
251
|
+
scope (if asynchronous 'async 'sync)))
|
|
252
|
+
(target (eliscript-analyzer--recur-target-create
|
|
253
|
+
:kind 'function :arity (length parsed))))
|
|
254
|
+
(dolist (parameter parsed)
|
|
255
|
+
(eliscript-analyzer--declare-pattern
|
|
256
|
+
child (eliscript-parameter-form parameter) 'parameter t))
|
|
257
|
+
(let ((eliscript-analyzer--recur-targets (list target))
|
|
258
|
+
(eliscript-analyzer--tail-position t)
|
|
259
|
+
(eliscript-analyzer--recur-barrier nil))
|
|
260
|
+
(eliscript-analyzer--analyze-sequence body child)))))
|
|
261
|
+
|
|
262
|
+
(defun eliscript-analyzer--analyze-loop (arguments scope)
|
|
263
|
+
"Analyze binding LOOP ARGUMENTS in lexical SCOPE."
|
|
264
|
+
(unless arguments
|
|
265
|
+
(eliscript-analyzer--fail "loop requires a binding list"))
|
|
266
|
+
(let* ((bindings-form (car arguments))
|
|
267
|
+
(bindings (eliscript-form-value bindings-form))
|
|
268
|
+
(body (cdr arguments))
|
|
269
|
+
(child (eliscript-analyzer--make-scope scope)))
|
|
270
|
+
(unless (proper-list-p bindings)
|
|
271
|
+
(let ((eliscript-analyzer--current-span
|
|
272
|
+
(or (eliscript-form-span bindings-form)
|
|
273
|
+
eliscript-analyzer--current-span)))
|
|
274
|
+
(eliscript-analyzer--fail "loop bindings must be a list")))
|
|
275
|
+
(let ((parsed
|
|
276
|
+
(mapcar
|
|
277
|
+
(lambda (binding)
|
|
278
|
+
(let ((value (eliscript-form-value binding)))
|
|
279
|
+
(unless (and (proper-list-p value) (= (length value) 2))
|
|
280
|
+
(let ((eliscript-analyzer--current-span
|
|
281
|
+
(or (eliscript-form-span binding)
|
|
282
|
+
eliscript-analyzer--current-span)))
|
|
283
|
+
(eliscript-analyzer--fail
|
|
284
|
+
"loop bindings require exactly one initializer: %S"
|
|
285
|
+
(eliscript-form-strip binding))))
|
|
286
|
+
(eliscript-analyzer--binding-pair binding)))
|
|
287
|
+
bindings)))
|
|
288
|
+
(dolist (binding parsed)
|
|
289
|
+
(eliscript-analyzer--analyze-non-tail (cadr binding) scope)
|
|
290
|
+
(eliscript-analyzer--analyze-pattern-defaults
|
|
291
|
+
(car binding) scope))
|
|
292
|
+
(dolist (binding parsed)
|
|
293
|
+
(eliscript-analyzer--declare-pattern
|
|
294
|
+
child (car binding) 'loop t))
|
|
295
|
+
(let ((eliscript-analyzer--recur-targets
|
|
296
|
+
(cons
|
|
297
|
+
(eliscript-analyzer--recur-target-create
|
|
298
|
+
:kind 'loop :arity (length parsed))
|
|
299
|
+
eliscript-analyzer--recur-targets))
|
|
300
|
+
(eliscript-analyzer--tail-position t)
|
|
301
|
+
(eliscript-analyzer--recur-barrier nil))
|
|
302
|
+
(eliscript-analyzer--analyze-sequence body child)))))
|
|
303
|
+
|
|
304
|
+
(defun eliscript-analyzer--analyze-recur (arguments scope)
|
|
305
|
+
"Analyze RECUR ARGUMENTS in lexical SCOPE."
|
|
306
|
+
(let ((target (car eliscript-analyzer--recur-targets)))
|
|
307
|
+
(unless target
|
|
308
|
+
(eliscript-analyzer--fail
|
|
309
|
+
"recur is only valid inside a function or loop"))
|
|
310
|
+
(when eliscript-analyzer--recur-barrier
|
|
311
|
+
(eliscript-analyzer--fail "recur cannot cross a try boundary"))
|
|
312
|
+
(unless eliscript-analyzer--tail-position
|
|
313
|
+
(eliscript-analyzer--fail "recur is only valid in tail position"))
|
|
314
|
+
(unless (= (length arguments)
|
|
315
|
+
(eliscript-analyzer--recur-target-arity target))
|
|
316
|
+
(eliscript-analyzer--fail
|
|
317
|
+
"recur expects %d arguments for the current %s, got %d"
|
|
318
|
+
(eliscript-analyzer--recur-target-arity target)
|
|
319
|
+
(eliscript-analyzer--recur-target-kind target)
|
|
320
|
+
(length arguments)))
|
|
321
|
+
(eliscript-analyzer--analyze-non-tail-sequence arguments scope)))
|
|
322
|
+
|
|
323
|
+
(defun eliscript-analyzer--analyze-assignment (arguments scope form-name)
|
|
324
|
+
"Analyze assignment ARGUMENTS in SCOPE for FORM-NAME."
|
|
325
|
+
(when (or (null arguments) (= (% (length arguments) 2) 1))
|
|
326
|
+
(eliscript-analyzer--fail
|
|
327
|
+
"%s expects one or more name/value pairs" form-name))
|
|
328
|
+
(while arguments
|
|
329
|
+
(let* ((name (pop arguments))
|
|
330
|
+
(name-value (eliscript-form-value name))
|
|
331
|
+
(value (pop arguments))
|
|
332
|
+
(binding (and (symbolp name-value)
|
|
333
|
+
(eliscript-analyzer--require-binding
|
|
334
|
+
scope name-value))))
|
|
335
|
+
(unless binding
|
|
336
|
+
(eliscript-analyzer--fail
|
|
337
|
+
"assignment target must be a symbol: %S"
|
|
338
|
+
(eliscript-form-strip name)))
|
|
339
|
+
(unless (eliscript-analyzer--binding-mutable binding)
|
|
340
|
+
(let ((eliscript-analyzer--current-span
|
|
341
|
+
(or (eliscript-form-span name)
|
|
342
|
+
eliscript-analyzer--current-span)))
|
|
343
|
+
(eliscript-analyzer--fail
|
|
344
|
+
"cannot assign to immutable binding: %s" name-value)))
|
|
345
|
+
(eliscript-analyzer--analyze-non-tail value scope))))
|
|
346
|
+
|
|
347
|
+
(defun eliscript-analyzer--analyze-object (arguments scope form-name)
|
|
348
|
+
"Analyze host object ARGUMENTS in SCOPE for FORM-NAME."
|
|
349
|
+
(when (= (% (length arguments) 2) 1)
|
|
350
|
+
(eliscript-analyzer--fail "%s expects key/value pairs" form-name))
|
|
351
|
+
(while arguments
|
|
352
|
+
(let ((key (pop arguments))
|
|
353
|
+
(value (pop arguments)))
|
|
354
|
+
(unless (let ((key-value (eliscript-form-value key)))
|
|
355
|
+
(or (keywordp key-value)
|
|
356
|
+
(stringp key-value)
|
|
357
|
+
(symbolp key-value)))
|
|
358
|
+
(eliscript-analyzer--analyze-non-tail key scope))
|
|
359
|
+
(eliscript-analyzer--analyze-non-tail value scope))))
|
|
360
|
+
|
|
361
|
+
(defun eliscript-analyzer--analyze-property-call (operator arguments scope)
|
|
362
|
+
"Analyze property OPERATOR with ARGUMENTS in SCOPE."
|
|
363
|
+
(pcase operator
|
|
364
|
+
('get
|
|
365
|
+
(when (or (< (length arguments) 2) (> (length arguments) 3))
|
|
366
|
+
(eliscript-analyzer--fail "get expects 2..3 arguments")))
|
|
367
|
+
('put
|
|
368
|
+
(unless (= (length arguments) 3)
|
|
369
|
+
(eliscript-analyzer--fail "put expects 3 arguments")))
|
|
370
|
+
('js-call
|
|
371
|
+
(when (< (length arguments) 2)
|
|
372
|
+
(eliscript-analyzer--fail "js-call expects 2+ arguments"))))
|
|
373
|
+
(eliscript-analyzer--analyze-non-tail (car arguments) scope)
|
|
374
|
+
(let ((key (cadr arguments)))
|
|
375
|
+
(unless (let ((key-value (eliscript-form-value key)))
|
|
376
|
+
(or (keywordp key-value) (stringp key-value)))
|
|
377
|
+
(eliscript-analyzer--analyze-non-tail key scope)))
|
|
378
|
+
(eliscript-analyzer--analyze-non-tail-sequence
|
|
379
|
+
(nthcdr 2 arguments) scope))
|
|
380
|
+
|
|
381
|
+
(defun eliscript-analyzer--analyze-cond (clauses scope)
|
|
382
|
+
"Analyze cond CLAUSES in SCOPE."
|
|
383
|
+
(dolist (clause clauses)
|
|
384
|
+
(let ((value (eliscript-form-value clause)))
|
|
385
|
+
(unless (and (proper-list-p value) value)
|
|
386
|
+
(let ((eliscript-analyzer--current-span
|
|
387
|
+
(or (eliscript-form-span clause)
|
|
388
|
+
eliscript-analyzer--current-span)))
|
|
389
|
+
(eliscript-analyzer--fail
|
|
390
|
+
"invalid cond clause: %S" (eliscript-form-strip clause))))
|
|
391
|
+
(eliscript-analyzer--analyze-non-tail (car value) scope)
|
|
392
|
+
(eliscript-analyzer--analyze-sequence (cdr value) scope))))
|
|
393
|
+
|
|
394
|
+
(defun eliscript-analyzer--clause-operator (form)
|
|
395
|
+
"Return FORM's clause operator, or nil when it is not a clause."
|
|
396
|
+
(let ((value (eliscript-form-value form)))
|
|
397
|
+
(and (proper-list-p value)
|
|
398
|
+
value
|
|
399
|
+
(symbolp (eliscript-form-value (car value)))
|
|
400
|
+
(eliscript-form-value (car value)))))
|
|
401
|
+
|
|
402
|
+
(defun eliscript-analyzer--analyze-try (arguments scope)
|
|
403
|
+
"Analyze try ARGUMENTS in lexical SCOPE."
|
|
404
|
+
(let ((eliscript-analyzer--recur-barrier t)
|
|
405
|
+
clauses-started catch-seen finally-seen)
|
|
406
|
+
(dolist (argument arguments)
|
|
407
|
+
(let ((operator (eliscript-analyzer--clause-operator argument)))
|
|
408
|
+
(cond
|
|
409
|
+
((eq operator 'catch)
|
|
410
|
+
(when catch-seen
|
|
411
|
+
(eliscript-analyzer--fail "try accepts at most one catch clause"))
|
|
412
|
+
(when finally-seen
|
|
413
|
+
(eliscript-analyzer--fail "catch must precede finally"))
|
|
414
|
+
(setq clauses-started t
|
|
415
|
+
catch-seen t)
|
|
416
|
+
(let* ((clause (eliscript-form-value argument))
|
|
417
|
+
(binding (cadr clause)))
|
|
418
|
+
(unless (and binding
|
|
419
|
+
(eliscript-binding-pattern-p binding))
|
|
420
|
+
(let ((eliscript-analyzer--current-span
|
|
421
|
+
(or (eliscript-form-span argument)
|
|
422
|
+
eliscript-analyzer--current-span)))
|
|
423
|
+
(eliscript-analyzer--fail
|
|
424
|
+
"catch requires a binding symbol, vector, or map")))
|
|
425
|
+
(eliscript-analyzer--analyze-pattern-defaults binding scope)
|
|
426
|
+
(let ((child (eliscript-analyzer--make-scope scope)))
|
|
427
|
+
(eliscript-analyzer--declare-pattern child binding 'catch t)
|
|
428
|
+
(eliscript-analyzer--analyze-sequence
|
|
429
|
+
(cddr clause) child))))
|
|
430
|
+
((eq operator 'finally)
|
|
431
|
+
(when finally-seen
|
|
432
|
+
(eliscript-analyzer--fail
|
|
433
|
+
"try accepts at most one finally clause"))
|
|
434
|
+
(setq clauses-started t
|
|
435
|
+
finally-seen t)
|
|
436
|
+
(eliscript-analyzer--analyze-sequence
|
|
437
|
+
(cdr (eliscript-form-value argument)) scope))
|
|
438
|
+
(clauses-started
|
|
439
|
+
(let ((eliscript-analyzer--current-span
|
|
440
|
+
(or (eliscript-form-span argument)
|
|
441
|
+
eliscript-analyzer--current-span)))
|
|
442
|
+
(eliscript-analyzer--fail
|
|
443
|
+
"try body forms must precede catch and finally clauses")))
|
|
444
|
+
(t
|
|
445
|
+
(eliscript-analyzer--analyze-expression argument scope)))))
|
|
446
|
+
(unless (or catch-seen finally-seen)
|
|
447
|
+
(eliscript-analyzer--fail
|
|
448
|
+
"try requires a catch or finally clause"))))
|
|
449
|
+
|
|
450
|
+
(defun eliscript-analyzer--analyze-call (form scope)
|
|
451
|
+
"Analyze call FORM in SCOPE."
|
|
452
|
+
(let* ((items (eliscript-form-value form))
|
|
453
|
+
(operator-form (car items))
|
|
454
|
+
(operator (eliscript-form-value operator-form))
|
|
455
|
+
(arguments (cdr items)))
|
|
456
|
+
(pcase operator
|
|
457
|
+
((or 'lambda 'fn)
|
|
458
|
+
(unless arguments
|
|
459
|
+
(eliscript-analyzer--fail "%s requires an argument list" operator))
|
|
460
|
+
(eliscript-analyzer--analyze-function
|
|
461
|
+
(car arguments) (cdr arguments) scope))
|
|
462
|
+
('async
|
|
463
|
+
(unless arguments
|
|
464
|
+
(eliscript-analyzer--fail "async requires an argument list"))
|
|
465
|
+
(eliscript-analyzer--analyze-function
|
|
466
|
+
(car arguments) (cdr arguments) scope t))
|
|
467
|
+
('await
|
|
468
|
+
(unless (= (length arguments) 1)
|
|
469
|
+
(eliscript-analyzer--fail "await expects 1 argument"))
|
|
470
|
+
(unless (eliscript-analyzer--async-function-p scope)
|
|
471
|
+
(eliscript-analyzer--fail
|
|
472
|
+
"await is only valid inside an async function"))
|
|
473
|
+
(eliscript-analyzer--analyze-non-tail (car arguments) scope))
|
|
474
|
+
('throw
|
|
475
|
+
(unless (= (length arguments) 1)
|
|
476
|
+
(eliscript-analyzer--fail "throw expects 1 argument"))
|
|
477
|
+
(eliscript-analyzer--analyze-non-tail (car arguments) scope))
|
|
478
|
+
('try (eliscript-analyzer--analyze-try arguments scope))
|
|
479
|
+
((or 'catch 'finally)
|
|
480
|
+
(eliscript-analyzer--fail
|
|
481
|
+
"%s is only valid as a try clause" operator))
|
|
482
|
+
('let (eliscript-analyzer--analyze-let arguments scope nil))
|
|
483
|
+
('let* (eliscript-analyzer--analyze-let arguments scope t))
|
|
484
|
+
('loop (eliscript-analyzer--analyze-loop arguments scope))
|
|
485
|
+
('recur (eliscript-analyzer--analyze-recur arguments scope))
|
|
486
|
+
('setq (eliscript-analyzer--analyze-assignment arguments scope "setq"))
|
|
487
|
+
('set!
|
|
488
|
+
(unless (= (length arguments) 2)
|
|
489
|
+
(eliscript-analyzer--fail "set! expects 2 arguments"))
|
|
490
|
+
(eliscript-analyzer--analyze-assignment arguments scope "set!"))
|
|
491
|
+
('cond (eliscript-analyzer--analyze-cond arguments scope))
|
|
492
|
+
('if
|
|
493
|
+
(unless (<= 2 (length arguments) 3)
|
|
494
|
+
(eliscript-analyzer--fail "if expects 2..3 arguments"))
|
|
495
|
+
(eliscript-analyzer--analyze-non-tail (car arguments) scope)
|
|
496
|
+
(dolist (branch (cdr arguments))
|
|
497
|
+
(eliscript-analyzer--analyze-expression branch scope)))
|
|
498
|
+
((or 'when 'unless)
|
|
499
|
+
(unless arguments
|
|
500
|
+
(eliscript-analyzer--fail "%s expects 1+ arguments" operator))
|
|
501
|
+
(eliscript-analyzer--analyze-non-tail (car arguments) scope)
|
|
502
|
+
(eliscript-analyzer--analyze-sequence (cdr arguments) scope))
|
|
503
|
+
((or 'progn 'do 'and 'or)
|
|
504
|
+
(eliscript-analyzer--analyze-sequence arguments scope))
|
|
505
|
+
('while
|
|
506
|
+
(unless arguments
|
|
507
|
+
(eliscript-analyzer--fail "while expects 1+ arguments"))
|
|
508
|
+
(eliscript-analyzer--analyze-non-tail-sequence arguments scope))
|
|
509
|
+
('quote nil)
|
|
510
|
+
('js-object
|
|
511
|
+
(eliscript-analyzer--analyze-object
|
|
512
|
+
arguments scope (symbol-name operator)))
|
|
513
|
+
('hash-map
|
|
514
|
+
(when (= (% (length arguments) 2) 1)
|
|
515
|
+
(eliscript-analyzer--fail
|
|
516
|
+
"hash-map expects complete key/value pairs"))
|
|
517
|
+
(eliscript-analyzer--analyze-non-tail-sequence arguments scope))
|
|
518
|
+
((or 'get 'put 'js-call)
|
|
519
|
+
(eliscript-analyzer--analyze-property-call operator arguments scope))
|
|
520
|
+
('js* nil)
|
|
521
|
+
((pred (lambda (name)
|
|
522
|
+
(memq name eliscript-analyzer--builtin-operators)))
|
|
523
|
+
(eliscript-analyzer--analyze-non-tail-sequence arguments scope))
|
|
524
|
+
((or 'defun 'defn 'defportable 'defasync 'defvar 'defconst
|
|
525
|
+
'export 'export-default 'import 'module)
|
|
526
|
+
(eliscript-analyzer--fail "%s is only valid at module top level" operator))
|
|
527
|
+
(_
|
|
528
|
+
(eliscript-analyzer--analyze-non-tail operator-form scope)
|
|
529
|
+
(eliscript-analyzer--analyze-non-tail-sequence arguments scope)))))
|
|
530
|
+
|
|
531
|
+
(defun eliscript-analyzer--analyze-expression (form scope)
|
|
532
|
+
"Analyze expression FORM in lexical SCOPE."
|
|
533
|
+
(let* ((eliscript-analyzer--current-span
|
|
534
|
+
(or (eliscript-form-span form) eliscript-analyzer--current-span))
|
|
535
|
+
(value (eliscript-form-value form)))
|
|
536
|
+
(cond
|
|
537
|
+
((or (null value) (eq value t) (numberp value) (stringp value)
|
|
538
|
+
(keywordp value) (eq value 'false) (eq value 'undefined)) nil)
|
|
539
|
+
((symbolp value)
|
|
540
|
+
(if (eliscript-analyzer--qualified-symbol-p value)
|
|
541
|
+
(condition-case error-data
|
|
542
|
+
(eliscript-symbol-reference-name value)
|
|
543
|
+
(eliscript-compile-error
|
|
544
|
+
(eliscript-analyzer--fail
|
|
545
|
+
"%s" (eliscript-diagnostic-condition-message error-data))))
|
|
546
|
+
(eliscript-analyzer--require-binding scope value)))
|
|
547
|
+
((vectorp value)
|
|
548
|
+
(eliscript-analyzer--analyze-non-tail-sequence
|
|
549
|
+
(append value nil) scope))
|
|
550
|
+
((consp value)
|
|
551
|
+
(unless (proper-list-p value)
|
|
552
|
+
(eliscript-analyzer--fail
|
|
553
|
+
"dotted call forms are not supported: %S"
|
|
554
|
+
(eliscript-form-strip form)))
|
|
555
|
+
(eliscript-analyzer--analyze-call form scope))
|
|
556
|
+
(t (eliscript-analyzer--fail
|
|
557
|
+
"unsupported form: %S" (eliscript-form-strip form))))))
|
|
558
|
+
|
|
559
|
+
(defun eliscript-analyzer--import-bindings (arguments &optional portable)
|
|
560
|
+
"Return local binding names declared by import ARGUMENTS.
|
|
561
|
+
|
|
562
|
+
When PORTABLE is non-nil, accept named imports only."
|
|
563
|
+
(unless (stringp (eliscript-form-value (car arguments)))
|
|
564
|
+
(eliscript-analyzer--fail "import module must be a string"))
|
|
565
|
+
(let ((specifiers (cdr arguments))
|
|
566
|
+
bindings
|
|
567
|
+
default-seen
|
|
568
|
+
namespace-seen
|
|
569
|
+
named-seen)
|
|
570
|
+
(while specifiers
|
|
571
|
+
(let* ((specifier-form (pop specifiers))
|
|
572
|
+
(specifier (eliscript-form-value specifier-form)))
|
|
573
|
+
(pcase specifier
|
|
574
|
+
(:default
|
|
575
|
+
(when portable
|
|
576
|
+
(eliscript-analyzer--fail
|
|
577
|
+
"import-portable only supports named imports"))
|
|
578
|
+
(when default-seen
|
|
579
|
+
(eliscript-analyzer--fail "duplicate :default import"))
|
|
580
|
+
(unless specifiers
|
|
581
|
+
(eliscript-analyzer--fail ":default requires a binding"))
|
|
582
|
+
(setq default-seen t)
|
|
583
|
+
(push (pop specifiers) bindings))
|
|
584
|
+
(:as
|
|
585
|
+
(when portable
|
|
586
|
+
(eliscript-analyzer--fail
|
|
587
|
+
"import-portable only supports named imports"))
|
|
588
|
+
(when namespace-seen
|
|
589
|
+
(eliscript-analyzer--fail "duplicate :as import"))
|
|
590
|
+
(unless specifiers
|
|
591
|
+
(eliscript-analyzer--fail ":as requires a namespace binding"))
|
|
592
|
+
(setq namespace-seen t)
|
|
593
|
+
(push (pop specifiers) bindings))
|
|
594
|
+
((pred symbolp)
|
|
595
|
+
(setq named-seen t)
|
|
596
|
+
(push specifier-form bindings))
|
|
597
|
+
(_ (eliscript-analyzer--fail
|
|
598
|
+
"invalid import specifier: %S" specifier)))))
|
|
599
|
+
(when (and namespace-seen named-seen)
|
|
600
|
+
(eliscript-analyzer--fail
|
|
601
|
+
"namespace and named imports cannot be combined"))
|
|
602
|
+
(when (and portable (null bindings))
|
|
603
|
+
(eliscript-analyzer--fail
|
|
604
|
+
"import-portable expects one or more bindings"))
|
|
605
|
+
(nreverse bindings)))
|
|
606
|
+
|
|
607
|
+
(defun eliscript-analyzer--flatten-modules (forms)
|
|
608
|
+
"Return top-level FORMS with module wrappers removed."
|
|
609
|
+
(apply
|
|
610
|
+
#'append
|
|
611
|
+
(mapcar
|
|
612
|
+
(lambda (form)
|
|
613
|
+
(let ((value (eliscript-form-value form)))
|
|
614
|
+
(if (and (consp value)
|
|
615
|
+
(eq (eliscript-form-value (car value)) 'module))
|
|
616
|
+
(let ((eliscript-analyzer--current-span
|
|
617
|
+
(or (eliscript-form-span form)
|
|
618
|
+
eliscript-analyzer--current-span)))
|
|
619
|
+
(unless (and (cdr value)
|
|
620
|
+
(symbolp (eliscript-form-value (cadr value))))
|
|
621
|
+
(eliscript-analyzer--fail "module name must be a symbol"))
|
|
622
|
+
(eliscript-analyzer--flatten-modules (cddr value)))
|
|
623
|
+
(list form))))
|
|
624
|
+
forms)))
|
|
625
|
+
|
|
626
|
+
(defun eliscript-analyzer--predeclare-top-level (forms scope)
|
|
627
|
+
"Declare all top-level bindings from FORMS in SCOPE."
|
|
628
|
+
(dolist (form forms)
|
|
629
|
+
(let* ((value (eliscript-form-value form))
|
|
630
|
+
(eliscript-analyzer--current-span
|
|
631
|
+
(or (eliscript-form-span form) eliscript-analyzer--current-span)))
|
|
632
|
+
(when (consp value)
|
|
633
|
+
(pcase (eliscript-form-value (car value))
|
|
634
|
+
((or 'import 'import-portable)
|
|
635
|
+
(unless (cdr value)
|
|
636
|
+
(eliscript-analyzer--fail "%s expects a module name"
|
|
637
|
+
(eliscript-form-value (car value))))
|
|
638
|
+
(dolist (name
|
|
639
|
+
(eliscript-analyzer--import-bindings
|
|
640
|
+
(cdr value)
|
|
641
|
+
(eq (eliscript-form-value (car value)) 'import-portable)))
|
|
642
|
+
(eliscript-analyzer--declare scope name 'import nil)))
|
|
643
|
+
('defvar
|
|
644
|
+
(unless (<= 1 (length (cdr value)) 2)
|
|
645
|
+
(eliscript-analyzer--fail "defvar expects 1..2 arguments"))
|
|
646
|
+
(eliscript-analyzer--declare scope (cadr value) 'variable t))
|
|
647
|
+
('defconst
|
|
648
|
+
(unless (<= 1 (length (cdr value)) 2)
|
|
649
|
+
(eliscript-analyzer--fail "defconst expects 1..2 arguments"))
|
|
650
|
+
(eliscript-analyzer--declare scope (cadr value) 'constant nil))
|
|
651
|
+
((or 'defun 'defn 'defportable 'defasync)
|
|
652
|
+
(unless (>= (length (cdr value)) 2)
|
|
653
|
+
(eliscript-analyzer--fail
|
|
654
|
+
"%s expects 2+ arguments"
|
|
655
|
+
(eliscript-form-value (car value))))
|
|
656
|
+
(eliscript-analyzer--declare
|
|
657
|
+
scope (cadr value) 'function nil)))))))
|
|
658
|
+
|
|
659
|
+
(defun eliscript-analyzer--analyze-top-level (form scope)
|
|
660
|
+
"Analyze top-level FORM in module SCOPE."
|
|
661
|
+
(let* ((value (eliscript-form-value form))
|
|
662
|
+
(eliscript-analyzer--current-span
|
|
663
|
+
(or (eliscript-form-span form) eliscript-analyzer--current-span)))
|
|
664
|
+
(if (not (consp value))
|
|
665
|
+
(eliscript-analyzer--analyze-expression form scope)
|
|
666
|
+
(let* ((operator-form (car value))
|
|
667
|
+
(operator (eliscript-form-value operator-form))
|
|
668
|
+
(arguments (cdr value)))
|
|
669
|
+
(pcase operator
|
|
670
|
+
((or 'import 'import-portable) nil)
|
|
671
|
+
((or 'defvar 'defconst)
|
|
672
|
+
(when (> (length arguments) 2)
|
|
673
|
+
(eliscript-analyzer--fail "%s expects 1..2 arguments" operator))
|
|
674
|
+
(eliscript-analyzer--analyze-expression (cadr arguments) scope))
|
|
675
|
+
((or 'defun 'defn 'defportable 'defasync)
|
|
676
|
+
(unless (>= (length arguments) 2)
|
|
677
|
+
(eliscript-analyzer--fail "%s expects 2+ arguments" operator))
|
|
678
|
+
(eliscript-analyzer--analyze-function
|
|
679
|
+
(cadr arguments) (cddr arguments) scope (eq operator 'defasync)))
|
|
680
|
+
('export
|
|
681
|
+
(unless arguments
|
|
682
|
+
(eliscript-analyzer--fail "export expects one or more bindings"))
|
|
683
|
+
(dolist (name arguments)
|
|
684
|
+
(let ((name-value (eliscript-form-value name))
|
|
685
|
+
(eliscript-analyzer--current-span
|
|
686
|
+
(or (eliscript-form-span name)
|
|
687
|
+
eliscript-analyzer--current-span)))
|
|
688
|
+
(unless (symbolp name-value)
|
|
689
|
+
(eliscript-analyzer--fail
|
|
690
|
+
"export name must be a symbol: %S"
|
|
691
|
+
(eliscript-form-strip name)))
|
|
692
|
+
(eliscript-analyzer--require-binding scope name-value))))
|
|
693
|
+
('export-default
|
|
694
|
+
(unless (= (length arguments) 1)
|
|
695
|
+
(eliscript-analyzer--fail "export-default expects 1 argument"))
|
|
696
|
+
(eliscript-analyzer--analyze-expression (car arguments) scope))
|
|
697
|
+
(_ (eliscript-analyzer--analyze-expression form scope)))))))
|
|
698
|
+
|
|
699
|
+
(defun eliscript-analyze-module (forms &optional filename)
|
|
700
|
+
"Validate lexical bindings in module FORMS read from FILENAME.
|
|
701
|
+
|
|
702
|
+
Return FORMS unchanged for the emitter."
|
|
703
|
+
(let* ((eliscript-analyzer--filename filename)
|
|
704
|
+
(eliscript-analyzer--current-span nil)
|
|
705
|
+
(flattened (eliscript-analyzer--flatten-modules forms))
|
|
706
|
+
(scope (eliscript-analyzer--make-scope)))
|
|
707
|
+
(eliscript-analyzer--predeclare-top-level flattened scope)
|
|
708
|
+
(dolist (form flattened)
|
|
709
|
+
(eliscript-analyzer--analyze-top-level form scope))
|
|
710
|
+
(eliscript-transient--analyze-module forms filename)
|
|
711
|
+
(eliscript-portable-validate-module forms filename)
|
|
712
|
+
forms))
|
|
713
|
+
|
|
714
|
+
(provide 'eliscript-analyzer)
|
|
715
|
+
|
|
716
|
+
;;; eliscript-analyzer.el ends here
|