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.
Files changed (195) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +135 -0
  3. package/bin/eliscript +16 -0
  4. package/bin/eliscript-bootstrap +15 -0
  5. package/bin/eliscript-build +15 -0
  6. package/bin/eliscript-check +15 -0
  7. package/bin/eliscript-eval +15 -0
  8. package/bin/eliscript-format +15 -0
  9. package/bin/eliscript-org +10 -0
  10. package/bin/eliscript-portable +17 -0
  11. package/bin/eliscript-seed +10 -0
  12. package/bin/eliscript-seed-build +10 -0
  13. package/bin/eliscript-watch +15 -0
  14. package/bootstrap/README.md +100 -0
  15. package/bootstrap/compiler/analyzer.eli +1426 -0
  16. package/bootstrap/compiler/compiler.eli +472 -0
  17. package/bootstrap/compiler/emitter.eli +1745 -0
  18. package/bootstrap/compiler/expander.eli +2522 -0
  19. package/bootstrap/compiler/formatter.eli +359 -0
  20. package/bootstrap/compiler/ir.eli +297 -0
  21. package/bootstrap/compiler/lower.eli +661 -0
  22. package/bootstrap/compiler/project.eli +1120 -0
  23. package/bootstrap/compiler/reader.eli +321 -0
  24. package/bootstrap/compiler/source-map.eli +165 -0
  25. package/bootstrap/compiler/symbol.eli +92 -0
  26. package/bootstrap/compiler/syntax.eli +54 -0
  27. package/bootstrap/compiler/transient-analysis.eli +825 -0
  28. package/bootstrap/host/build.mjs +28 -0
  29. package/bootstrap/host/bun.mjs +282 -0
  30. package/bootstrap/host/check-cli.mjs +163 -0
  31. package/bootstrap/host/eliscript-cli.mjs +36 -0
  32. package/bootstrap/host/evaluation-cli.mjs +401 -0
  33. package/bootstrap/host/evaluation.mjs +560 -0
  34. package/bootstrap/host/format-cli.mjs +139 -0
  35. package/bootstrap/host/project-cli.mjs +295 -0
  36. package/bootstrap/host/project.mjs +1080 -0
  37. package/bootstrap/host/watch-cli.mjs +129 -0
  38. package/bootstrap/host/watch.mjs +267 -0
  39. package/browser/host.mjs +357 -0
  40. package/browser/worker.mjs +60 -0
  41. package/compiler/README.md +118 -0
  42. package/compiler/eliscript-analyzer.el +716 -0
  43. package/compiler/eliscript-cli.el +108 -0
  44. package/compiler/eliscript-diagnostic.el +158 -0
  45. package/compiler/eliscript-emitter.el +1351 -0
  46. package/compiler/eliscript-evaluation.el +259 -0
  47. package/compiler/eliscript-expander.el +1630 -0
  48. package/compiler/eliscript-form.el +79 -0
  49. package/compiler/eliscript-ir-emitter.el +1460 -0
  50. package/compiler/eliscript-ir.el +356 -0
  51. package/compiler/eliscript-lower.el +463 -0
  52. package/compiler/eliscript-macro-eval.el +811 -0
  53. package/compiler/eliscript-parameters.el +324 -0
  54. package/compiler/eliscript-portable.el +492 -0
  55. package/compiler/eliscript-project-cli.el +156 -0
  56. package/compiler/eliscript-project.el +1676 -0
  57. package/compiler/eliscript-reader.el +421 -0
  58. package/compiler/eliscript-source-map.el +192 -0
  59. package/compiler/eliscript-symbol.el +101 -0
  60. package/compiler/eliscript-transient-analysis.el +817 -0
  61. package/compiler/eliscript.el +239 -0
  62. package/dist/bootstrap/analyzer.mjs +968 -0
  63. package/dist/bootstrap/analyzer.mjs.map +1 -0
  64. package/dist/bootstrap/compiler.mjs +228 -0
  65. package/dist/bootstrap/compiler.mjs.map +1 -0
  66. package/dist/bootstrap/emitter.mjs +1173 -0
  67. package/dist/bootstrap/emitter.mjs.map +1 -0
  68. package/dist/bootstrap/expander.mjs +1469 -0
  69. package/dist/bootstrap/expander.mjs.map +1 -0
  70. package/dist/bootstrap/formatter.mjs +304 -0
  71. package/dist/bootstrap/formatter.mjs.map +1 -0
  72. package/dist/bootstrap/ir.mjs +193 -0
  73. package/dist/bootstrap/ir.mjs.map +1 -0
  74. package/dist/bootstrap/lower.mjs +344 -0
  75. package/dist/bootstrap/lower.mjs.map +1 -0
  76. package/dist/bootstrap/project.mjs +585 -0
  77. package/dist/bootstrap/project.mjs.map +1 -0
  78. package/dist/bootstrap/reader.mjs +259 -0
  79. package/dist/bootstrap/reader.mjs.map +1 -0
  80. package/dist/bootstrap/source-map.mjs +154 -0
  81. package/dist/bootstrap/source-map.mjs.map +1 -0
  82. package/dist/bootstrap/symbol.mjs +74 -0
  83. package/dist/bootstrap/symbol.mjs.map +1 -0
  84. package/dist/bootstrap/syntax.mjs +38 -0
  85. package/dist/bootstrap/syntax.mjs.map +1 -0
  86. package/dist/bootstrap/transient-analysis.mjs +660 -0
  87. package/dist/bootstrap/transient-analysis.mjs.map +1 -0
  88. package/dist/browser/compiler.js +6491 -0
  89. package/editor/README.md +83 -0
  90. package/editor/eliscript-mode.el +878 -0
  91. package/editor/eliscript-repl.el +501 -0
  92. package/package.json +163 -0
  93. package/platform/README.md +48 -0
  94. package/platform/browser.mjs +204 -0
  95. package/platform/worker.mjs +166 -0
  96. package/runtime/README.md +296 -0
  97. package/runtime/core/collection-internals.mjs +512 -0
  98. package/runtime/core/collection.mjs +645 -0
  99. package/runtime/core/data-text.mjs +532 -0
  100. package/runtime/core/data.mjs +268 -0
  101. package/runtime/core/identifier.mjs +314 -0
  102. package/runtime/core/lazy-sequence.mjs +244 -0
  103. package/runtime/core/list-internals.mjs +18 -0
  104. package/runtime/core/list.mjs +273 -0
  105. package/runtime/core/map-internals.mjs +793 -0
  106. package/runtime/core/map.mjs +394 -0
  107. package/runtime/core/metadata-internals.mjs +14 -0
  108. package/runtime/core/metadata.mjs +44 -0
  109. package/runtime/core/object-impl.mjs +65 -0
  110. package/runtime/core/object-impl.mjs.map +1 -0
  111. package/runtime/core/object.mjs +13 -0
  112. package/runtime/core/order.mjs +234 -0
  113. package/runtime/core/persistent-kind.mjs +43 -0
  114. package/runtime/core/protocol-error.mjs +36 -0
  115. package/runtime/core/protocol-impl.mjs +326 -0
  116. package/runtime/core/protocol-impl.mjs.map +1 -0
  117. package/runtime/core/protocol.mjs +14 -0
  118. package/runtime/core/queue-internals.mjs +27 -0
  119. package/runtime/core/queue.mjs +238 -0
  120. package/runtime/core/record.mjs +346 -0
  121. package/runtime/core/regex.mjs +184 -0
  122. package/runtime/core/sequence.mjs +632 -0
  123. package/runtime/core/set-algebra.mjs +287 -0
  124. package/runtime/core/set-internals.mjs +33 -0
  125. package/runtime/core/set.mjs +378 -0
  126. package/runtime/core/sorted-internals.mjs +215 -0
  127. package/runtime/core/sorted.mjs +598 -0
  128. package/runtime/core/text-impl.mjs +95 -0
  129. package/runtime/core/text-impl.mjs.map +1 -0
  130. package/runtime/core/text.mjs +15 -0
  131. package/runtime/core/transducer.mjs +668 -0
  132. package/runtime/core/transient-internals.mjs +38 -0
  133. package/runtime/core/transient.mjs +53 -0
  134. package/runtime/core/truth.mjs +3 -0
  135. package/runtime/core/type.mjs +262 -0
  136. package/runtime/core/value-internals.mjs +188 -0
  137. package/runtime/core/value.mjs +93 -0
  138. package/runtime/core/vector-internals.mjs +102 -0
  139. package/runtime/core/vector.mjs +951 -0
  140. package/runtime/literals.mjs +34 -0
  141. package/runtime/source-mapping.mjs +220 -0
  142. package/runtime/testing/list.mjs +52 -0
  143. package/runtime/testing/map.mjs +145 -0
  144. package/runtime/testing/queue.mjs +49 -0
  145. package/runtime/testing/set.mjs +61 -0
  146. package/runtime/testing/sorted.mjs +105 -0
  147. package/runtime/testing/value.mjs +17 -0
  148. package/runtime/testing/vector.mjs +102 -0
  149. package/runtime/worker-value-codec.mjs +565 -0
  150. package/runtime/worker-value-stream.mjs +786 -0
  151. package/runtime/worker.mjs +975 -0
  152. package/stdlib/README.md +774 -0
  153. package/stdlib/bit.eli +36 -0
  154. package/stdlib/core/collection.eli +134 -0
  155. package/stdlib/core/data-text.eli +16 -0
  156. package/stdlib/core/data.eli +306 -0
  157. package/stdlib/core/identifier.eli +32 -0
  158. package/stdlib/core/lazy-sequence.eli +63 -0
  159. package/stdlib/core/metadata.eli +20 -0
  160. package/stdlib/core/object.eli +89 -0
  161. package/stdlib/core/order.eli +133 -0
  162. package/stdlib/core/protocol.eli +387 -0
  163. package/stdlib/core/record.eli +16 -0
  164. package/stdlib/core/regex.eli +17 -0
  165. package/stdlib/core/seq.eli +701 -0
  166. package/stdlib/core/set.eli +312 -0
  167. package/stdlib/core/sorted.eli +53 -0
  168. package/stdlib/core/text.eli +103 -0
  169. package/stdlib/core/transducer.eli +59 -0
  170. package/stdlib/core/transient.eli +24 -0
  171. package/stdlib/core/type.eli +19 -0
  172. package/stdlib/core/walk.eli +195 -0
  173. package/stdlib/core/zipper.eli +508 -0
  174. package/stdlib/data-text.eli +1092 -0
  175. package/stdlib/data.eli +42 -0
  176. package/stdlib/deferred.eli +70 -0
  177. package/stdlib/function.eli +120 -0
  178. package/stdlib/hierarchy.eli +245 -0
  179. package/stdlib/identifier.eli +147 -0
  180. package/stdlib/interop/js.eli +616 -0
  181. package/stdlib/json.eli +870 -0
  182. package/stdlib/metadata.eli +56 -0
  183. package/stdlib/multimethod.eli +464 -0
  184. package/stdlib/numeric.eli +193 -0
  185. package/stdlib/object.eli +96 -0
  186. package/stdlib/persistent-list.eli +145 -0
  187. package/stdlib/persistent-map.eli +724 -0
  188. package/stdlib/persistent-queue.eli +32 -0
  189. package/stdlib/persistent-set.eli +236 -0
  190. package/stdlib/persistent-vector.eli +342 -0
  191. package/stdlib/result.eli +141 -0
  192. package/stdlib/sequence.eli +106 -0
  193. package/stdlib/state/atom.eli +265 -0
  194. package/stdlib/text.eli +88 -0
  195. package/stdlib/value.eli +325 -0
@@ -0,0 +1,1426 @@
1
+ (module bootstrap.compiler.analyzer
2
+ (import "./symbol.mjs" binding-name reference-name)
3
+ (import "./syntax.mjs"
4
+ node-kind node-span node-value node-name node-items)
5
+ (import "./transient-analysis.mjs" analyze-transients)
6
+
7
+ (defconst builtin-operators
8
+ (js-array "if" "when" "unless" "progn" "do" "while" "loop" "recur"
9
+ "and" "or" "not"
10
+ "+" "*" "-" "/" "%" "mod" "=" "/=" "not=" "<" "<=" ">" ">="
11
+ "1+" "1-" "eq" "equal" "null" "nil?" "undefined?" "nullish?"
12
+ "int32" "uint32" "imul32" "bit-and" "bit-or" "bit-xor" "bit-not"
13
+ "bit-shift-left" "bit-shift-right" "unsigned-bit-shift-right"
14
+ "value-type" "host-identity-token" "string-code-unit-at" "string-from-code-unit"
15
+ "string-to-number" "string-to-bigint" "number-float64-words"
16
+ "list" "vector" "hash-map" "hash-set" "queue" "js-array" "car"
17
+ "cdr" "cons" "js-cons" "nth" "js-nth" "aref" "length" "js-length"
18
+ "new" "print" "str"
19
+ "object-keys" "object-has?" "object-assoc" "__eliscript_arity_error"
20
+ "funcall" "apply"))
21
+
22
+ (defconst portable-forbidden-operators
23
+ (js-array "js*" "print" "put" "js-call" "new" "async" "await" "throw" "try"))
24
+
25
+ (defconst fail
26
+ (js* "(diagnostic) => { const p = diagnostic.location.start; const error = new Error(`${diagnostic.location.file}:${p.line}:${p.column}: ${diagnostic.message}`); error.eliscriptDiagnostic = diagnostic; throw error; }"))
27
+ (defconst guard
28
+ (js* "(operation, onError) => { try { return operation(); } catch (error) { return onError(error.message); } }"))
29
+
30
+ (defun compiler-fail (context span code phase message)
31
+ (let ((file (or (get context :filename)
32
+ (and span (get span :filename))
33
+ "<input>")))
34
+ (fail
35
+ (js-object
36
+ :format "eliscript-diagnostic"
37
+ :version 1
38
+ :code code
39
+ :severity "error"
40
+ :phase phase
41
+ :message message
42
+ :location
43
+ (js-object
44
+ :file file
45
+ :start
46
+ (js-object
47
+ :offset (if span (get span :start) 0)
48
+ :line (if span (get span :line) 1)
49
+ :column (if span (get span :column) 1))
50
+ :end
51
+ (js-object
52
+ :offset (if span (get span :end) 0)
53
+ :line (if span (get span :endLine) 1)
54
+ :column (if span (get span :endColumn) 1)))))))
55
+
56
+ (defun analyzer-fail (context span message)
57
+ (compiler-fail context span "ELI-A0001" "analysis" message))
58
+
59
+ (defun portable-fail (context span message)
60
+ (compiler-fail
61
+ context span "ELI-P0001" "portable-analysis" message))
62
+
63
+ (defun symbol-node-p (node)
64
+ (and node (= (node-kind node) "symbol")))
65
+
66
+ (defun keyword-node-p (node)
67
+ (and node (= (node-kind node) "keyword")))
68
+
69
+ (defun vector-node-p (node)
70
+ (and node (= (node-kind node) "vector")))
71
+
72
+ (defun map-node-p (node)
73
+ (and node
74
+ (= (node-kind node) "list")
75
+ (> (get (node-items node) :length) 0)
76
+ (symbol-node-p (js-nth 0 (node-items node)))
77
+ (= (node-name (js-nth 0 (node-items node))) "hash-map")))
78
+
79
+ (defun string-node-p (node)
80
+ (and node
81
+ (= (node-kind node) "literal")
82
+ (= (js* "typeof node.value") "string")))
83
+
84
+ (defun null-node-p (node)
85
+ (and node
86
+ (= (node-kind node) "literal")
87
+ (null (node-value node))))
88
+
89
+ (defun list-items (node)
90
+ (cond
91
+ ((and node (= (node-kind node) "list")) (node-items node))
92
+ ((null-node-p node) (js-array))
93
+ (t undefined)))
94
+
95
+ (defun binding-marker-p (name)
96
+ (js-call (js-array "&optional" "&rest" "&body") :includes name))
97
+
98
+ (defun split-binding-name (name)
99
+ (let ((slash (js-call name :lastIndexOf "/")))
100
+ (if (< slash 0)
101
+ (js-object :namespace undefined :local name)
102
+ (js-object
103
+ :namespace (js-call name :slice 0 slash)
104
+ :local (js-call name :slice (1+ slash))))))
105
+
106
+ (defun map-binding-shortcut (node)
107
+ (when (keyword-node-p node)
108
+ (let* ((parts (split-binding-name (node-name node)))
109
+ (kind (get parts :local)))
110
+ (when (js-call (js-array "keys" "strs" "syms") :includes kind)
111
+ (js-object :kind kind :namespace (get parts :namespace))))))
112
+
113
+ (defun shortcut-entry (shortcut name-node)
114
+ (let* ((name (node-name name-node))
115
+ (parts (split-binding-name name))
116
+ (local (get parts :local))
117
+ (namespace (or (get shortcut :namespace)
118
+ (get parts :namespace)))
119
+ (kind (get shortcut :kind))
120
+ (qualified (if namespace (str namespace "/" local) local)))
121
+ (js-object
122
+ :target (js-object
123
+ :kind "symbol" :name local :span (node-span name-node))
124
+ :keyKind kind
125
+ :keyName (if (= kind "strs") name qualified))))
126
+
127
+ (defun map-binding-key-p (node)
128
+ (or (keyword-node-p node)
129
+ (and node
130
+ (= (node-kind node) "literal")
131
+ (or (= (js* "typeof node.value") "string")
132
+ (= (js* "typeof node.value") "number")))))
133
+
134
+ (defun map-binding-spec (pattern context)
135
+ (let ((items (js-call (node-items pattern) :slice 1))
136
+ (entries (js-array))
137
+ (defaults (new globalThis.Map))
138
+ (as undefined)
139
+ (shortcuts-seen (new globalThis.Set))
140
+ (or-seen false)
141
+ (as-seen false)
142
+ (index 0))
143
+ (while (< index (get items :length))
144
+ (let* ((left (js-nth index items))
145
+ (right (js-nth (1+ index) items))
146
+ (directive (and (keyword-node-p left) (node-name left)))
147
+ (shortcut (map-binding-shortcut left)))
148
+ (cond
149
+ (shortcut
150
+ (let ((kind (get shortcut :kind)))
151
+ (when (js-call shortcuts-seen :has directive)
152
+ (analyzer-fail context (node-span left)
153
+ (str "map binding pattern accepts :" directive " once")))
154
+ (js-call shortcuts-seen :add directive)
155
+ (unless (vector-node-p right)
156
+ (analyzer-fail context (node-span right)
157
+ (str "map binding :" kind " value must be a vector")))
158
+ (js-call (node-items right) :forEach
159
+ (lambda (name-node)
160
+ (let ((name (and (symbol-node-p name-node)
161
+ (node-name name-node))))
162
+ (unless (and name
163
+ (not (binding-marker-p name))
164
+ (> (get
165
+ (get (split-binding-name name) :local)
166
+ :length)
167
+ 0))
168
+ (analyzer-fail context (node-span name-node)
169
+ (str "map binding :" kind
170
+ " entries must be symbols")))
171
+ (js-call entries :push
172
+ (shortcut-entry shortcut name-node)))))))
173
+ ((= directive "or")
174
+ (when or-seen
175
+ (analyzer-fail context (node-span left)
176
+ "map binding pattern accepts :or once"))
177
+ (setq or-seen t)
178
+ (unless (map-node-p right)
179
+ (analyzer-fail context (node-span right)
180
+ "map binding :or value must be a map"))
181
+ (let ((pairs (js-call (node-items right) :slice 1))
182
+ (default-index 0))
183
+ (while (< default-index (get pairs :length))
184
+ (let* ((name-node (js-nth default-index pairs))
185
+ (default (js-nth (1+ default-index) pairs))
186
+ (name (and (symbol-node-p name-node)
187
+ (node-name name-node))))
188
+ (unless (and name (not (binding-marker-p name)))
189
+ (analyzer-fail context (node-span name-node)
190
+ "map binding :or keys must be binding symbols"))
191
+ (when (js-call defaults :has name)
192
+ (analyzer-fail context (node-span name-node)
193
+ "duplicate map binding :or default"))
194
+ (js-call defaults :set name default))
195
+ (setq default-index (+ default-index 2)))))
196
+ ((= directive "as")
197
+ (when as-seen
198
+ (analyzer-fail context (node-span left)
199
+ "map binding pattern accepts :as once"))
200
+ (setq as-seen t)
201
+ (unless (and (symbol-node-p right)
202
+ (not (binding-marker-p (node-name right))))
203
+ (analyzer-fail context (node-span right)
204
+ "map binding :as value must be a symbol"))
205
+ (setq as right))
206
+ (directive
207
+ (analyzer-fail context (node-span left)
208
+ (str "unknown map binding directive: :" directive)))
209
+ (t
210
+ (unless (binding-pattern-p left)
211
+ (analyzer-fail context (node-span left)
212
+ "map binding entry target must be a binding pattern"))
213
+ (unless (map-binding-key-p right)
214
+ (analyzer-fail context (node-span right)
215
+ "map binding entry key must be a keyword, string, or number"))
216
+ (js-call entries :push (js-object :target left :key right)))))
217
+ (setq index (+ index 2)))
218
+ (js-call defaults :forEach
219
+ (lambda (default name)
220
+ (let ((entry
221
+ (js-call entries :find
222
+ (lambda (candidate)
223
+ (and (symbol-node-p (get candidate :target))
224
+ (= (node-name (get candidate :target)) name))))))
225
+ (unless entry
226
+ (analyzer-fail context (node-span default)
227
+ (str "map binding :or has no scalar target: " name)))
228
+ (put entry :default default)
229
+ (put entry :hasDefault t))))
230
+ (js-object :entries entries :as as)))
231
+
232
+ (defun binding-nodes (pattern context)
233
+ (cond
234
+ ((symbol-node-p pattern)
235
+ (if (binding-marker-p (node-name pattern))
236
+ (analyzer-fail
237
+ context (node-span pattern) "invalid binding pattern marker")
238
+ (js-array pattern)))
239
+ ((vector-node-p pattern)
240
+ (let ((result (js-array))
241
+ (rest-seen false)
242
+ (rest-done false))
243
+ (js-call (node-items pattern) :forEach
244
+ (lambda (element)
245
+ (let ((name (and (symbol-node-p element) (node-name element))))
246
+ (cond
247
+ ((= name "&rest")
248
+ (when (or rest-seen rest-done)
249
+ (analyzer-fail
250
+ context (node-span element)
251
+ "invalid vector binding pattern"))
252
+ (setq rest-seen t))
253
+ (rest-seen
254
+ (unless (and name (not (binding-marker-p name)))
255
+ (analyzer-fail
256
+ context (node-span element)
257
+ "vector &rest binding must be a symbol"))
258
+ (js-call result :push element)
259
+ (setq rest-seen false)
260
+ (setq rest-done t))
261
+ (rest-done
262
+ (analyzer-fail
263
+ context (node-span element)
264
+ "vector &rest binding must be final"))
265
+ ((null-node-p element) nil)
266
+ (t
267
+ (js-call (binding-nodes element context) :forEach
268
+ (lambda (binding)
269
+ (js-call result :push binding))))))))
270
+ (when rest-seen
271
+ (analyzer-fail
272
+ context (node-span pattern)
273
+ "vector &rest requires a binding"))
274
+ result))
275
+ ((map-node-p pattern)
276
+ (let* ((spec (map-binding-spec pattern context))
277
+ (result (js-array)))
278
+ (js-call (get spec :entries) :forEach
279
+ (lambda (entry)
280
+ (js-call (binding-nodes (get entry :target) context) :forEach
281
+ (lambda (binding) (js-call result :push binding)))))
282
+ (when (get spec :as) (js-call result :push (get spec :as)))
283
+ result))
284
+ (t
285
+ (analyzer-fail
286
+ context (and pattern (node-span pattern))
287
+ (str "binding pattern must be a symbol, vector, or map: "
288
+ (node-display pattern))))))
289
+
290
+ (defun binding-pattern-p (node)
291
+ (or (and (symbol-node-p node)
292
+ (not (binding-marker-p (node-name node))))
293
+ (vector-node-p node)
294
+ (map-node-p node)))
295
+
296
+ (defun function-parameters (parameters context)
297
+ (let ((items (list-items parameters))
298
+ (parsed (js-array))
299
+ (mode "required")
300
+ (index 0))
301
+ (when (eq items undefined)
302
+ (analyzer-fail
303
+ context (node-span parameters) "function arguments must be a list"))
304
+ (while (< index (get items :length))
305
+ (let* ((parameter (js-nth index items))
306
+ (name (and (symbol-node-p parameter) (node-name parameter))))
307
+ (cond
308
+ ((= name "&optional")
309
+ (unless (= mode "required")
310
+ (analyzer-fail
311
+ context (node-span parameter)
312
+ "invalid function parameter list"))
313
+ (setq mode "optional"))
314
+ ((= name "&rest")
315
+ (unless (or (= mode "required") (= mode "optional"))
316
+ (analyzer-fail
317
+ context (node-span parameter)
318
+ "invalid function parameter list"))
319
+ (setq mode "rest"))
320
+ ((= name "&body")
321
+ (analyzer-fail
322
+ context (node-span parameter)
323
+ "invalid function parameter list"))
324
+ ((= mode "rest")
325
+ (unless (and name (not (binding-marker-p name)))
326
+ (analyzer-fail
327
+ context (node-span parameter)
328
+ "function &rest parameter must be a symbol"))
329
+ (js-call parsed :push
330
+ (js-object :kind "rest" :node parameter :bindings (js-array parameter)))
331
+ (setq mode "done"))
332
+ ((= mode "done")
333
+ (analyzer-fail
334
+ context (node-span parameter)
335
+ "invalid function parameter list"))
336
+ ((binding-pattern-p parameter)
337
+ (js-call parsed :push
338
+ (js-object
339
+ :kind mode
340
+ :node parameter
341
+ :bindings (binding-nodes parameter context))))
342
+ (t
343
+ (analyzer-fail
344
+ context (node-span parameter)
345
+ (str "function arguments must be symbols, vectors, or maps: "
346
+ (node-display parameters))))))
347
+ (setq index (1+ index)))
348
+ (when (= mode "rest")
349
+ (analyzer-fail
350
+ context (node-span parameters) "invalid function parameter list"))
351
+ parsed))
352
+
353
+ (defun node-display (node)
354
+ (cond
355
+ ((not node) "nil")
356
+ ((= (node-kind node) "undefined") "undefined")
357
+ ((= (node-kind node) "symbol") (node-name node))
358
+ ((= (node-kind node) "keyword") (str ":" (node-name node)))
359
+ ((= (node-kind node) "literal")
360
+ (let ((value (node-value node)))
361
+ (cond
362
+ ((null value) "nil")
363
+ ((eq value t) "t")
364
+ ((eq value false) "false")
365
+ ((= (js* "typeof value") "string")
366
+ (js-call (js* "JSON") :stringify value))
367
+ (t (str value)))))
368
+ ((or (= (node-kind node) "list") (= (node-kind node) "vector"))
369
+ (let* ((items (node-items node))
370
+ (contents
371
+ (js-call
372
+ (js-call items :map node-display)
373
+ :join " ")))
374
+ (cond
375
+ ((and (= (node-kind node) "list")
376
+ (= (get items :length) 2)
377
+ (symbol-node-p (js-nth 0 items))
378
+ (= (node-name (js-nth 0 items)) "quote"))
379
+ (str "'" (node-display (js-nth 1 items))))
380
+ ((= (node-kind node) "list") (str "(" contents ")"))
381
+ (t (str "[" contents "]")))))
382
+ (t "<unsupported>")))
383
+
384
+ (defun make-scope (parent &optional function-context)
385
+ (js-object
386
+ :parent parent
387
+ :names (new (js* "Map"))
388
+ :outputs (new (js* "Map"))
389
+ :functionContext function-context))
390
+
391
+ (defun async-function-p (scope)
392
+ (let ((current scope)
393
+ (function-context nil))
394
+ (while (and current (not function-context))
395
+ (setq function-context (get current :functionContext))
396
+ (setq current (get current :parent)))
397
+ (= function-context "async")))
398
+
399
+ (defun guarded-binding-name (name context span)
400
+ (funcall
401
+ guard
402
+ (lambda () (binding-name name))
403
+ (lambda (message) (analyzer-fail context span message))))
404
+
405
+ (defun guarded-reference-name (name context span)
406
+ (funcall
407
+ guard
408
+ (lambda () (reference-name name))
409
+ (lambda (message) (analyzer-fail context span message))))
410
+
411
+ (defun declare-binding (scope name-node kind mutable context)
412
+ (unless (symbol-node-p name-node)
413
+ (analyzer-fail
414
+ context (and name-node (node-span name-node))
415
+ (str "expected binding name, got " (node-display name-node))))
416
+ (let* ((name (node-name name-node))
417
+ (span (node-span name-node))
418
+ (names (get scope :names))
419
+ (outputs (get scope :outputs))
420
+ (output-name (guarded-binding-name name context span))
421
+ (existing-name (js-call names :get name))
422
+ (existing-output (js-call outputs :get output-name)))
423
+ (when existing-name
424
+ (analyzer-fail
425
+ context span (str "duplicate " kind " binding: " name)))
426
+ (when existing-output
427
+ (analyzer-fail
428
+ context span
429
+ (str "binding " name " emits " output-name
430
+ ", which collides with " (get existing-output :name))))
431
+ (let ((binding
432
+ (js-object
433
+ :name name
434
+ :outputName output-name
435
+ :kind kind
436
+ :mutable mutable)))
437
+ (js-call names :set name binding)
438
+ (js-call outputs :set output-name binding)
439
+ binding)))
440
+
441
+ (defun declare-pattern (scope pattern kind mutable context)
442
+ (js-call (binding-nodes pattern context) :forEach
443
+ (lambda (binding)
444
+ (declare-binding scope binding kind mutable context))))
445
+
446
+ (defun lookup-binding (scope name)
447
+ (let ((current scope)
448
+ (binding undefined))
449
+ (while (and current (not binding))
450
+ (setq binding (js-call (get current :names) :get name))
451
+ (setq current (get current :parent)))
452
+ binding))
453
+
454
+ (defun require-binding (scope node context)
455
+ (let* ((name (node-name node))
456
+ (binding (lookup-binding scope name)))
457
+ (or binding
458
+ (analyzer-fail
459
+ context (node-span node) (str "unbound symbol: " name)))))
460
+
461
+ (defun qualified-name-p (name)
462
+ (or (js-call name :includes ".")
463
+ (js-call name :includes "/")))
464
+
465
+ (defun analyze-sequence (forms scope context)
466
+ (let ((tail-position (get context :tailPosition))
467
+ (index 0))
468
+ (while (< index (get forms :length))
469
+ (put context :tailPosition
470
+ (and tail-position (= index (1- (get forms :length)))))
471
+ (analyze-expression (js-nth index forms) scope context)
472
+ (setq index (1+ index)))
473
+ (put context :tailPosition tail-position)))
474
+
475
+ (defun analyze-non-tail (form scope context)
476
+ (let ((tail-position (get context :tailPosition)))
477
+ (put context :tailPosition false)
478
+ (analyze-expression form scope context)
479
+ (put context :tailPosition tail-position)))
480
+
481
+ (defun analyze-pattern-defaults (pattern scope context)
482
+ (cond
483
+ ((vector-node-p pattern)
484
+ (js-call (node-items pattern) :forEach
485
+ (lambda (element)
486
+ (unless (or (null-node-p element)
487
+ (and (symbol-node-p element)
488
+ (= (node-name element) "&rest")))
489
+ (analyze-pattern-defaults element scope context)))))
490
+ ((map-node-p pattern)
491
+ (let ((spec (map-binding-spec pattern context)))
492
+ (js-call (get spec :entries) :forEach
493
+ (lambda (entry)
494
+ (when (get entry :hasDefault)
495
+ (analyze-non-tail (get entry :default) scope context))
496
+ (analyze-pattern-defaults
497
+ (get entry :target) scope context)))))))
498
+
499
+ (defun analyze-non-tail-sequence (forms scope context)
500
+ (let ((tail-position (get context :tailPosition)))
501
+ (put context :tailPosition false)
502
+ (analyze-sequence forms scope context)
503
+ (put context :tailPosition tail-position)))
504
+
505
+ (defun binding-pair (binding context)
506
+ (cond
507
+ ((binding-pattern-p binding)
508
+ (binding-nodes binding context)
509
+ (js-array binding undefined))
510
+ (t
511
+ (let ((items (list-items binding)))
512
+ (if (and (not (eq items undefined))
513
+ (>= (get items :length) 1)
514
+ (<= (get items :length) 2)
515
+ (binding-pattern-p (js-nth 0 items)))
516
+ (progn
517
+ (binding-nodes (js-nth 0 items) context)
518
+ (js-array (js-nth 0 items) (js-nth 1 items)))
519
+ (analyzer-fail
520
+ context (node-span binding)
521
+ (str "invalid let binding: " (node-display binding))))))))
522
+
523
+ (defun analyze-let (args scope sequential context form-span)
524
+ (when (= (get args :length) 0)
525
+ (analyzer-fail
526
+ context form-span
527
+ (str (if sequential "let*" "let") " requires a binding list")))
528
+ (let* ((bindings-form (js-nth 0 args))
529
+ (bindings (list-items bindings-form))
530
+ (body (js-call args :slice 1))
531
+ (child (make-scope scope)))
532
+ (when (eq bindings undefined)
533
+ (analyzer-fail
534
+ context (node-span bindings-form) "let bindings must be a list"))
535
+ (if sequential
536
+ (js-call bindings :forEach
537
+ (lambda (binding)
538
+ (let ((pair (binding-pair binding context)))
539
+ (analyze-non-tail (js-nth 1 pair) child context)
540
+ (analyze-pattern-defaults (js-nth 0 pair) child context)
541
+ (declare-pattern
542
+ child (js-nth 0 pair) "local" t context))))
543
+ (let ((parsed
544
+ (js-call bindings :map
545
+ (lambda (binding) (binding-pair binding context)))))
546
+ (js-call parsed :forEach
547
+ (lambda (pair)
548
+ (analyze-non-tail (js-nth 1 pair) scope context)
549
+ (analyze-pattern-defaults (js-nth 0 pair) scope context)))
550
+ (js-call parsed :forEach
551
+ (lambda (pair)
552
+ (declare-pattern
553
+ child (js-nth 0 pair) "local" t context)))))
554
+ (analyze-sequence body child context)))
555
+
556
+ (defun analyze-function
557
+ (parameters body scope context &optional asynchronous)
558
+ (let ((parameter-items (function-parameters parameters context)))
559
+ (js-call parameter-items :forEach
560
+ (lambda (parameter)
561
+ (analyze-pattern-defaults (get parameter :node) scope context)))
562
+ (let ((child (make-scope
563
+ scope (if asynchronous "async" "sync"))))
564
+ (js-call parameter-items :forEach
565
+ (lambda (parameter)
566
+ (js-call (get parameter :bindings) :forEach
567
+ (lambda (binding)
568
+ (declare-binding
569
+ child binding "parameter" t context)))))
570
+ (let ((recur-targets (get context :recurTargets))
571
+ (tail-position (get context :tailPosition))
572
+ (recur-barrier (get context :recurBarrier)))
573
+ (put context :recurTargets
574
+ (js-array (js-object :kind "function"
575
+ :arity (get parameter-items :length))))
576
+ (put context :tailPosition t)
577
+ (put context :recurBarrier false)
578
+ (analyze-sequence body child context)
579
+ (put context :recurTargets recur-targets)
580
+ (put context :tailPosition tail-position)
581
+ (put context :recurBarrier recur-barrier)))))
582
+
583
+ (defun analyze-loop (args scope context form-span)
584
+ (when (= (get args :length) 0)
585
+ (analyzer-fail context form-span "loop requires a binding list"))
586
+ (let* ((bindings-form (js-nth 0 args))
587
+ (bindings (list-items bindings-form))
588
+ (body (js-call args :slice 1))
589
+ (child (make-scope scope))
590
+ (parsed (js-array)))
591
+ (when (eq bindings undefined)
592
+ (analyzer-fail
593
+ context (node-span bindings-form) "loop bindings must be a list"))
594
+ (js-call bindings :forEach
595
+ (lambda (binding)
596
+ (let ((items (list-items binding)))
597
+ (unless (and (not (eq items undefined))
598
+ (= (get items :length) 2))
599
+ (analyzer-fail
600
+ context (node-span binding)
601
+ (str "loop bindings require exactly one initializer: "
602
+ (node-display binding))))
603
+ (js-call parsed :push (binding-pair binding context)))))
604
+ (js-call parsed :forEach
605
+ (lambda (pair)
606
+ (analyze-non-tail (js-nth 1 pair) scope context)
607
+ (analyze-pattern-defaults (js-nth 0 pair) scope context)))
608
+ (js-call parsed :forEach
609
+ (lambda (pair)
610
+ (declare-pattern child (js-nth 0 pair) "loop" t context)))
611
+ (let ((recur-targets (get context :recurTargets))
612
+ (tail-position (get context :tailPosition))
613
+ (recur-barrier (get context :recurBarrier))
614
+ (targets (js-array)))
615
+ (js-call targets :push
616
+ (js-object :kind "loop" :arity (get parsed :length)))
617
+ (js-call recur-targets :forEach
618
+ (lambda (target) (js-call targets :push target)))
619
+ (put context :recurTargets targets)
620
+ (put context :tailPosition t)
621
+ (put context :recurBarrier false)
622
+ (analyze-sequence body child context)
623
+ (put context :recurTargets recur-targets)
624
+ (put context :tailPosition tail-position)
625
+ (put context :recurBarrier recur-barrier))))
626
+
627
+ (defun analyze-recur (args scope context form-span)
628
+ (let* ((targets (get context :recurTargets))
629
+ (target (and (> (get targets :length) 0) (js-nth 0 targets))))
630
+ (unless target
631
+ (analyzer-fail
632
+ context form-span
633
+ "recur is only valid inside a function or loop"))
634
+ (when (get context :recurBarrier)
635
+ (analyzer-fail
636
+ context form-span "recur cannot cross a try boundary"))
637
+ (unless (get context :tailPosition)
638
+ (analyzer-fail
639
+ context form-span "recur is only valid in tail position"))
640
+ (unless (= (get args :length) (get target :arity))
641
+ (analyzer-fail
642
+ context form-span
643
+ (str "recur expects " (get target :arity)
644
+ " arguments for the current " (get target :kind)
645
+ ", got " (get args :length))))
646
+ (analyze-non-tail-sequence args scope context)))
647
+
648
+ (defun analyze-assignment
649
+ (args scope form-name context form-span)
650
+ (when (or (= (get args :length) 0)
651
+ (= (% (get args :length) 2) 1))
652
+ (analyzer-fail
653
+ context form-span
654
+ (str form-name " expects one or more name/value pairs")))
655
+ (let ((index 0))
656
+ (while (< index (get args :length))
657
+ (let* ((name-node (js-nth index args))
658
+ (value (js-nth (1+ index) args)))
659
+ (unless (symbol-node-p name-node)
660
+ (analyzer-fail
661
+ context form-span
662
+ (str "assignment target must be a symbol: "
663
+ (node-display name-node))))
664
+ (let ((binding (require-binding scope name-node context)))
665
+ (unless (get binding :mutable)
666
+ (analyzer-fail
667
+ context (node-span name-node)
668
+ (str "cannot assign to immutable binding: "
669
+ (node-name name-node)))))
670
+ (analyze-non-tail value scope context))
671
+ (setq index (+ index 2)))))
672
+
673
+ (defun analyze-object (args scope context form-span form-name)
674
+ (when (= (% (get args :length) 2) 1)
675
+ (analyzer-fail
676
+ context form-span (str form-name " expects key/value pairs")))
677
+ (let ((index 0))
678
+ (while (< index (get args :length))
679
+ (let ((key (js-nth index args))
680
+ (value (js-nth (1+ index) args)))
681
+ (unless (or (keyword-node-p key)
682
+ (string-node-p key)
683
+ (symbol-node-p key))
684
+ (analyze-non-tail key scope context))
685
+ (analyze-non-tail value scope context))
686
+ (setq index (+ index 2)))))
687
+
688
+ (defun analyze-property-call
689
+ (operator args scope context form-span)
690
+ (cond
691
+ ((= operator "get")
692
+ (when (or (< (get args :length) 2)
693
+ (> (get args :length) 3))
694
+ (analyzer-fail context form-span "get expects 2..3 arguments")))
695
+ ((= operator "put")
696
+ (unless (= (get args :length) 3)
697
+ (analyzer-fail context form-span "put expects 3 arguments")))
698
+ ((= operator "js-call")
699
+ (when (< (get args :length) 2)
700
+ (analyzer-fail context form-span "js-call expects 2+ arguments"))))
701
+ (analyze-non-tail (js-nth 0 args) scope context)
702
+ (let ((key (js-nth 1 args)))
703
+ (unless (or (keyword-node-p key) (string-node-p key))
704
+ (analyze-non-tail key scope context)))
705
+ (analyze-non-tail-sequence (js-call args :slice 2) scope context))
706
+
707
+ (defun analyze-cond (clauses scope context)
708
+ (js-call clauses :forEach
709
+ (lambda (clause)
710
+ (let ((items (list-items clause)))
711
+ (when (or (eq items undefined) (= (get items :length) 0))
712
+ (analyzer-fail
713
+ context (node-span clause)
714
+ (str "invalid cond clause: " (node-display clause))))
715
+ (analyze-non-tail (js-nth 0 items) scope context)
716
+ (analyze-sequence (js-call items :slice 1) scope context)))))
717
+
718
+ (defun clause-operator (form)
719
+ (let ((items (list-items form)))
720
+ (and (not (eq items undefined))
721
+ (> (get items :length) 0)
722
+ (symbol-node-p (js-nth 0 items))
723
+ (node-name (js-nth 0 items)))))
724
+
725
+ (defun analyze-try (args scope context form-span)
726
+ (let ((recur-barrier (get context :recurBarrier))
727
+ (clauses-started false)
728
+ (catch-seen false)
729
+ (finally-seen false))
730
+ (put context :recurBarrier t)
731
+ (js-call args :forEach
732
+ (lambda (argument)
733
+ (let ((operator (clause-operator argument)))
734
+ (cond
735
+ ((= operator "catch")
736
+ (when catch-seen
737
+ (analyzer-fail
738
+ context form-span
739
+ "try accepts at most one catch clause"))
740
+ (when finally-seen
741
+ (analyzer-fail context form-span "catch must precede finally"))
742
+ (setq clauses-started t)
743
+ (setq catch-seen t)
744
+ (let* ((items (list-items argument))
745
+ (binding (js-nth 1 items)))
746
+ (unless (binding-pattern-p binding)
747
+ (analyzer-fail
748
+ context (node-span argument)
749
+ "catch requires a binding symbol, vector, or map"))
750
+ (analyze-pattern-defaults binding scope context)
751
+ (let ((child (make-scope scope)))
752
+ (declare-pattern child binding "catch" t context)
753
+ (analyze-sequence
754
+ (js-call items :slice 2) child context))))
755
+ ((= operator "finally")
756
+ (when finally-seen
757
+ (analyzer-fail
758
+ context form-span
759
+ "try accepts at most one finally clause"))
760
+ (setq clauses-started t)
761
+ (setq finally-seen t)
762
+ (analyze-sequence
763
+ (js-call (list-items argument) :slice 1) scope context))
764
+ (clauses-started
765
+ (analyzer-fail
766
+ context (node-span argument)
767
+ "try body forms must precede catch and finally clauses"))
768
+ (t (analyze-expression argument scope context))))))
769
+ (unless (or catch-seen finally-seen)
770
+ (analyzer-fail
771
+ context form-span "try requires a catch or finally clause"))
772
+ (put context :recurBarrier recur-barrier)))
773
+
774
+ (defun analyze-call (form scope context)
775
+ (let* ((items (node-items form))
776
+ (operator-node (js-nth 0 items))
777
+ (operator (and (symbol-node-p operator-node)
778
+ (node-name operator-node)))
779
+ (args (js-call items :slice 1))
780
+ (form-span (node-span form)))
781
+ (cond
782
+ ((or (= operator "lambda") (= operator "fn"))
783
+ (when (= (get args :length) 0)
784
+ (analyzer-fail
785
+ context form-span
786
+ (str operator " requires an argument list")))
787
+ (analyze-function
788
+ (js-nth 0 args) (js-call args :slice 1) scope context))
789
+ ((= operator "async")
790
+ (when (= (get args :length) 0)
791
+ (analyzer-fail
792
+ context form-span "async requires an argument list"))
793
+ (analyze-function
794
+ (js-nth 0 args) (js-call args :slice 1) scope context t))
795
+ ((= operator "await")
796
+ (unless (= (get args :length) 1)
797
+ (analyzer-fail context form-span "await expects 1 argument"))
798
+ (unless (async-function-p scope)
799
+ (analyzer-fail
800
+ context form-span
801
+ "await is only valid inside an async function"))
802
+ (analyze-non-tail (js-nth 0 args) scope context))
803
+ ((= operator "throw")
804
+ (unless (= (get args :length) 1)
805
+ (analyzer-fail context form-span "throw expects 1 argument"))
806
+ (analyze-non-tail (js-nth 0 args) scope context))
807
+ ((= operator "try")
808
+ (analyze-try args scope context form-span))
809
+ ((or (= operator "catch") (= operator "finally"))
810
+ (analyzer-fail
811
+ context form-span
812
+ (str operator " is only valid as a try clause")))
813
+ ((= operator "let")
814
+ (analyze-let args scope false context form-span))
815
+ ((= operator "let*")
816
+ (analyze-let args scope t context form-span))
817
+ ((= operator "loop")
818
+ (analyze-loop args scope context form-span))
819
+ ((= operator "recur")
820
+ (analyze-recur args scope context form-span))
821
+ ((= operator "setq")
822
+ (analyze-assignment args scope "setq" context form-span))
823
+ ((= operator "set!")
824
+ (unless (= (get args :length) 2)
825
+ (analyzer-fail context form-span "set! expects 2 arguments"))
826
+ (analyze-assignment args scope "set!" context form-span))
827
+ ((= operator "cond") (analyze-cond args scope context))
828
+ ((= operator "if")
829
+ (unless (and (>= (get args :length) 2)
830
+ (<= (get args :length) 3))
831
+ (analyzer-fail context form-span "if expects 2..3 arguments"))
832
+ (analyze-non-tail (js-nth 0 args) scope context)
833
+ (analyze-sequence (js-call args :slice 1) scope context))
834
+ ((or (= operator "when") (= operator "unless"))
835
+ (when (= (get args :length) 0)
836
+ (analyzer-fail
837
+ context form-span (str operator " expects 1+ arguments")))
838
+ (analyze-non-tail (js-nth 0 args) scope context)
839
+ (analyze-sequence (js-call args :slice 1) scope context))
840
+ ((or (= operator "progn") (= operator "do")
841
+ (= operator "and") (= operator "or"))
842
+ (analyze-sequence args scope context))
843
+ ((= operator "while")
844
+ (when (= (get args :length) 0)
845
+ (analyzer-fail context form-span "while expects 1+ arguments"))
846
+ (analyze-non-tail-sequence args scope context))
847
+ ((= operator "quote") nil)
848
+ ((= operator "js-object")
849
+ (analyze-object args scope context form-span operator))
850
+ ((= operator "hash-map")
851
+ (when (= (% (get args :length) 2) 1)
852
+ (analyzer-fail
853
+ context form-span
854
+ "hash-map expects complete key/value pairs"))
855
+ (analyze-non-tail-sequence args scope context))
856
+ ((or (= operator "get") (= operator "put") (= operator "js-call"))
857
+ (analyze-property-call
858
+ operator args scope context form-span))
859
+ ((= operator "js*") nil)
860
+ ((js-call builtin-operators :includes operator)
861
+ (analyze-non-tail-sequence args scope context))
862
+ ((js-call
863
+ (js-array "defun" "defn" "defportable" "defasync"
864
+ "defvar" "defconst"
865
+ "export" "export-default" "import" "import-portable" "module")
866
+ :includes operator)
867
+ (analyzer-fail
868
+ context form-span
869
+ (str operator " is only valid at module top level")))
870
+ (t
871
+ (analyze-non-tail operator-node scope context)
872
+ (analyze-non-tail-sequence args scope context)))))
873
+
874
+ (defun analyze-expression (form scope context)
875
+ (when form
876
+ (cond
877
+ ((or (= (node-kind form) "literal")
878
+ (= (node-kind form) "undefined")
879
+ (= (node-kind form) "keyword")) nil)
880
+ ((= (node-kind form) "symbol")
881
+ (let ((name (node-name form)))
882
+ (if (qualified-name-p name)
883
+ (guarded-reference-name name context (node-span form))
884
+ (require-binding scope form context))))
885
+ ((= (node-kind form) "vector")
886
+ (analyze-non-tail-sequence (node-items form) scope context))
887
+ ((= (node-kind form) "list")
888
+ (analyze-call form scope context))
889
+ (t
890
+ (analyzer-fail
891
+ context (node-span form)
892
+ (str "unsupported form: " (node-display form)))))))
893
+
894
+ (defun import-bindings (args context form-span portable)
895
+ (unless (and (> (get args :length) 0)
896
+ (string-node-p (js-nth 0 args)))
897
+ (analyzer-fail context form-span "import module must be a string"))
898
+ (let ((bindings (js-array))
899
+ (index 1)
900
+ (default-seen false)
901
+ (namespace-seen false)
902
+ (named-seen false))
903
+ (while (< index (get args :length))
904
+ (let ((specifier (js-nth index args)))
905
+ (cond
906
+ ((and (keyword-node-p specifier)
907
+ (= (node-name specifier) "default"))
908
+ (when portable
909
+ (analyzer-fail
910
+ context form-span
911
+ "import-portable only supports named imports"))
912
+ (when default-seen
913
+ (analyzer-fail
914
+ context form-span "duplicate :default import"))
915
+ (when (>= (1+ index) (get args :length))
916
+ (analyzer-fail
917
+ context form-span ":default requires a binding"))
918
+ (setq default-seen t)
919
+ (setq index (1+ index))
920
+ (js-call bindings :push (js-nth index args)))
921
+ ((and (keyword-node-p specifier)
922
+ (= (node-name specifier) "as"))
923
+ (when portable
924
+ (analyzer-fail
925
+ context form-span
926
+ "import-portable only supports named imports"))
927
+ (when namespace-seen
928
+ (analyzer-fail
929
+ context form-span "duplicate :as import"))
930
+ (when (>= (1+ index) (get args :length))
931
+ (analyzer-fail
932
+ context form-span ":as requires a namespace binding"))
933
+ (setq namespace-seen t)
934
+ (setq index (1+ index))
935
+ (js-call bindings :push (js-nth index args)))
936
+ ((symbol-node-p specifier)
937
+ (setq named-seen t)
938
+ (js-call bindings :push specifier))
939
+ (t
940
+ (analyzer-fail
941
+ context form-span
942
+ (str "invalid import specifier: "
943
+ (node-display specifier)))))
944
+ (setq index (1+ index))))
945
+ (when (and namespace-seen named-seen)
946
+ (analyzer-fail
947
+ context form-span
948
+ "namespace and named imports cannot be combined"))
949
+ (when (and portable (= (get bindings :length) 0))
950
+ (analyzer-fail
951
+ context form-span
952
+ "import-portable expects one or more bindings"))
953
+ bindings))
954
+
955
+ (defun append-flattened-modules (forms result context)
956
+ (js-call forms :forEach
957
+ (lambda (form)
958
+ (if (and (= (node-kind form) "list")
959
+ (symbol-node-p (js-nth 0 (node-items form)))
960
+ (= (node-name (js-nth 0 (node-items form))) "module"))
961
+ (let ((items (node-items form)))
962
+ (unless (and (> (get items :length) 1)
963
+ (symbol-node-p (js-nth 1 items)))
964
+ (analyzer-fail
965
+ context (node-span form) "module name must be a symbol"))
966
+ (append-flattened-modules
967
+ (js-call items :slice 2) result context))
968
+ (js-call result :push form)))))
969
+
970
+ (defun flatten-modules (forms context)
971
+ (let ((result (js-array)))
972
+ (append-flattened-modules forms result context)
973
+ result))
974
+
975
+ (defun predeclare-top-level (forms scope context)
976
+ (js-call forms :forEach
977
+ (lambda (form)
978
+ (when (= (node-kind form) "list")
979
+ (let* ((items (node-items form))
980
+ (operator-node (js-nth 0 items))
981
+ (operator (and (symbol-node-p operator-node)
982
+ (node-name operator-node)))
983
+ (args (js-call items :slice 1))
984
+ (count (get args :length)))
985
+ (cond
986
+ ((or (= operator "import") (= operator "import-portable"))
987
+ (when (= count 0)
988
+ (analyzer-fail
989
+ context (node-span form)
990
+ (str operator " expects a module name")))
991
+ (js-call
992
+ (import-bindings
993
+ args context (node-span form)
994
+ (= operator "import-portable"))
995
+ :forEach
996
+ (lambda (name)
997
+ (declare-binding scope name "import" false context))))
998
+ ((= operator "defvar")
999
+ (unless (and (>= count 1) (<= count 2))
1000
+ (analyzer-fail
1001
+ context (node-span form) "defvar expects 1..2 arguments"))
1002
+ (declare-binding
1003
+ scope (js-nth 0 args) "variable" t context))
1004
+ ((= operator "defconst")
1005
+ (unless (and (>= count 1) (<= count 2))
1006
+ (analyzer-fail
1007
+ context (node-span form) "defconst expects 1..2 arguments"))
1008
+ (declare-binding
1009
+ scope (js-nth 0 args) "constant" false context))
1010
+ ((or (= operator "defun") (= operator "defn")
1011
+ (= operator "defportable") (= operator "defasync"))
1012
+ (unless (>= count 2)
1013
+ (analyzer-fail
1014
+ context (node-span form)
1015
+ (str operator " expects 2+ arguments")))
1016
+ (declare-binding
1017
+ scope (js-nth 0 args) "function" false context))))))))
1018
+
1019
+ (defun analyze-top-level (form scope context)
1020
+ (if (not (= (node-kind form) "list"))
1021
+ (analyze-expression form scope context)
1022
+ (let* ((items (node-items form))
1023
+ (operator-node (js-nth 0 items))
1024
+ (operator (and (symbol-node-p operator-node)
1025
+ (node-name operator-node)))
1026
+ (args (js-call items :slice 1))
1027
+ (count (get args :length)))
1028
+ (cond
1029
+ ((or (= operator "import") (= operator "import-portable")) nil)
1030
+ ((or (= operator "defvar") (= operator "defconst"))
1031
+ (when (> count 2)
1032
+ (analyzer-fail
1033
+ context (node-span form)
1034
+ (str operator " expects 1..2 arguments")))
1035
+ (analyze-expression (js-nth 1 args) scope context))
1036
+ ((or (= operator "defun") (= operator "defn")
1037
+ (= operator "defportable") (= operator "defasync"))
1038
+ (unless (>= count 2)
1039
+ (analyzer-fail
1040
+ context (node-span form)
1041
+ (str operator " expects 2+ arguments")))
1042
+ (analyze-function
1043
+ (js-nth 1 args)
1044
+ (js-call args :slice 2)
1045
+ scope context (= operator "defasync")))
1046
+ ((= operator "export")
1047
+ (when (= count 0)
1048
+ (analyzer-fail
1049
+ context (node-span form)
1050
+ "export expects one or more bindings"))
1051
+ (js-call args :forEach
1052
+ (lambda (name)
1053
+ (unless (symbol-node-p name)
1054
+ (analyzer-fail
1055
+ context (node-span name)
1056
+ (str "export name must be a symbol: "
1057
+ (node-display name))))
1058
+ (require-binding scope name context))))
1059
+ ((= operator "export-default")
1060
+ (unless (= count 1)
1061
+ (analyzer-fail
1062
+ context (node-span form)
1063
+ "export-default expects 1 argument"))
1064
+ (analyze-expression (js-nth 0 args) scope context))
1065
+ (t (analyze-expression form scope context))))))
1066
+
1067
+ (defun portable-declarations (forms context)
1068
+ (let ((declarations (new (js* "Map"))))
1069
+ (js-call forms :forEach
1070
+ (lambda (form)
1071
+ (when (= (node-kind form) "list")
1072
+ (let* ((items (node-items form))
1073
+ (operator-node (js-nth 0 items))
1074
+ (operator (and (symbol-node-p operator-node)
1075
+ (node-name operator-node)))
1076
+ (args (js-call items :slice 1)))
1077
+ (cond
1078
+ ((= operator "defconst")
1079
+ (js-call declarations :set (node-name (js-nth 0 args))
1080
+ (js-object :kind "constant" :form form
1081
+ :initializer (js-nth 1 args))))
1082
+ ((= operator "defvar")
1083
+ (js-call declarations :set (node-name (js-nth 0 args))
1084
+ (js-object :kind "variable" :form form)))
1085
+ ((or (= operator "defun") (= operator "defn")
1086
+ (= operator "defportable") (= operator "defasync"))
1087
+ (js-call declarations :set (node-name (js-nth 0 args))
1088
+ (js-object
1089
+ :kind (if (= operator "defportable")
1090
+ "portable" "function")
1091
+ :form form
1092
+ :parameters (js-nth 1 args)
1093
+ :body (js-call args :slice 2))))
1094
+ ((or (= operator "import") (= operator "import-portable"))
1095
+ (js-call
1096
+ (import-bindings
1097
+ args context (node-span form)
1098
+ (= operator "import-portable"))
1099
+ :forEach
1100
+ (lambda (name)
1101
+ (js-call declarations :set (node-name name)
1102
+ (js-object
1103
+ :kind (if (= operator "import-portable")
1104
+ "portable-import" "import")
1105
+ :form form))))))))))
1106
+ declarations))
1107
+
1108
+ (defun portable-scope (parameters context)
1109
+ (let ((scope (new (js* "Set"))))
1110
+ (when parameters
1111
+ (js-call (function-parameters parameters context) :forEach
1112
+ (lambda (parameter)
1113
+ (js-call (get parameter :bindings) :forEach
1114
+ (lambda (binding)
1115
+ (js-call scope :add (node-name binding)))))))
1116
+ scope))
1117
+
1118
+ (defun copy-portable-scope (scope)
1119
+ (new (js* "Set") scope))
1120
+
1121
+ (defun add-portable-pattern (pattern scope context)
1122
+ (js-call (binding-nodes pattern context) :forEach
1123
+ (lambda (binding)
1124
+ (js-call scope :add (node-name binding)))))
1125
+
1126
+ (defun portable-reference
1127
+ (node scope declarations dependencies entry context)
1128
+ (let ((name (node-name node)))
1129
+ (unless (js-call scope :has name)
1130
+ (when (qualified-name-p name)
1131
+ (portable-fail
1132
+ context (node-span node)
1133
+ (str "portable function " entry
1134
+ " uses qualified host reference: " name)))
1135
+ (let ((declaration (js-call declarations :get name)))
1136
+ (when declaration
1137
+ (let ((kind (get declaration :kind)))
1138
+ (if (or (= kind "portable") (= kind "constant")
1139
+ (= kind "portable-import"))
1140
+ (js-call dependencies :add name)
1141
+ (portable-fail
1142
+ context (node-span node)
1143
+ (str "portable function " entry
1144
+ " depends on non-portable " kind ": " name)))))))))
1145
+
1146
+ (defun portable-sequence
1147
+ (forms scope declarations dependencies entry context)
1148
+ (js-call forms :forEach
1149
+ (lambda (form)
1150
+ (portable-expression
1151
+ form scope declarations dependencies entry context))))
1152
+
1153
+ (defun portable-function-expression
1154
+ (args scope declarations dependencies entry context)
1155
+ (let ((child (copy-portable-scope scope)))
1156
+ (js-call (function-parameters (js-nth 0 args) context) :forEach
1157
+ (lambda (parameter)
1158
+ (js-call (get parameter :bindings) :forEach
1159
+ (lambda (binding)
1160
+ (js-call child :add (node-name binding))))))
1161
+ (portable-sequence
1162
+ (js-call args :slice 1)
1163
+ child declarations dependencies entry context)))
1164
+
1165
+ (defun portable-let-expression
1166
+ (args scope sequential declarations dependencies entry context)
1167
+ (let ((bindings (list-items (js-nth 0 args)))
1168
+ (child (copy-portable-scope scope)))
1169
+ (if sequential
1170
+ (js-call bindings :forEach
1171
+ (lambda (binding)
1172
+ (let* ((items (list-items binding))
1173
+ (name (if items (js-nth 0 items) binding))
1174
+ (initializer (and items (js-nth 1 items))))
1175
+ (portable-expression
1176
+ initializer child declarations dependencies entry context)
1177
+ (add-portable-pattern name child context))))
1178
+ (progn
1179
+ (js-call bindings :forEach
1180
+ (lambda (binding)
1181
+ (let ((items (list-items binding)))
1182
+ (when items
1183
+ (portable-expression
1184
+ (js-nth 1 items) scope declarations dependencies entry context)))))
1185
+ (js-call bindings :forEach
1186
+ (lambda (binding)
1187
+ (let ((items (list-items binding)))
1188
+ (add-portable-pattern
1189
+ (if items (js-nth 0 items) binding) child context))))))
1190
+ (portable-sequence
1191
+ (js-call args :slice 1)
1192
+ child declarations dependencies entry context)))
1193
+
1194
+ (defun portable-loop-expression
1195
+ (args scope declarations dependencies entry context)
1196
+ (let ((bindings (list-items (js-nth 0 args)))
1197
+ (child (copy-portable-scope scope)))
1198
+ (js-call bindings :forEach
1199
+ (lambda (binding)
1200
+ (let ((items (list-items binding)))
1201
+ (portable-expression
1202
+ (js-nth 1 items) scope declarations dependencies entry context))))
1203
+ (js-call bindings :forEach
1204
+ (lambda (binding)
1205
+ (add-portable-pattern
1206
+ (js-nth 0 (list-items binding)) child context)))
1207
+ (portable-sequence
1208
+ (js-call args :slice 1)
1209
+ child declarations dependencies entry context)))
1210
+
1211
+ (defun portable-assignment
1212
+ (args scope declarations dependencies entry context)
1213
+ (let ((index 0))
1214
+ (while (< index (get args :length))
1215
+ (let ((name (js-nth index args))
1216
+ (value (js-nth (1+ index) args)))
1217
+ (unless (js-call scope :has (node-name name))
1218
+ (portable-fail
1219
+ context (node-span name)
1220
+ (str "portable function " entry
1221
+ " may only assign local bindings: " (node-name name))))
1222
+ (portable-expression
1223
+ value scope declarations dependencies entry context))
1224
+ (setq index (+ index 2)))))
1225
+
1226
+ (defun portable-object
1227
+ (args scope declarations dependencies entry context)
1228
+ (let ((index 0))
1229
+ (while (< index (get args :length))
1230
+ (let ((key (js-nth index args))
1231
+ (value (js-nth (1+ index) args)))
1232
+ (unless (or (keyword-node-p key)
1233
+ (string-node-p key)
1234
+ (symbol-node-p key))
1235
+ (portable-expression
1236
+ key scope declarations dependencies entry context))
1237
+ (portable-expression
1238
+ value scope declarations dependencies entry context))
1239
+ (setq index (+ index 2)))))
1240
+
1241
+ (defun portable-host-key-call
1242
+ (args scope declarations dependencies entry context)
1243
+ (let ((index 0))
1244
+ (while (< index (get args :length))
1245
+ (let ((argument (js-nth index args)))
1246
+ (unless (and (= index 1) (keyword-node-p argument))
1247
+ (portable-expression
1248
+ argument scope declarations dependencies entry context)))
1249
+ (setq index (1+ index)))))
1250
+
1251
+ (defun portable-call
1252
+ (form scope declarations dependencies entry context)
1253
+ (let* ((items (node-items form))
1254
+ (operator-node (js-nth 0 items))
1255
+ (operator (and (symbol-node-p operator-node)
1256
+ (node-name operator-node)))
1257
+ (args (js-call items :slice 1)))
1258
+ (when (js-call portable-forbidden-operators :includes operator)
1259
+ (portable-fail
1260
+ context (node-span form)
1261
+ (str "portable function " entry " cannot use " operator " ("
1262
+ (cond
1263
+ ((= operator "js*") "raw JavaScript")
1264
+ ((= operator "print") "console output")
1265
+ ((= operator "put") "object mutation")
1266
+ ((= operator "js-call") "host method calls")
1267
+ ((= operator "new") "host constructors")
1268
+ ((= operator "async") "asynchronous functions")
1269
+ ((= operator "await") "asynchronous suspension")
1270
+ ((or (= operator "throw") (= operator "try"))
1271
+ "exception control flow")
1272
+ (t "unsupported host capability"))
1273
+ ")")))
1274
+ (cond
1275
+ ((= operator "quote") nil)
1276
+ ((or (= operator "lambda") (= operator "fn"))
1277
+ (portable-function-expression
1278
+ args scope declarations dependencies entry context))
1279
+ ((= operator "let")
1280
+ (portable-let-expression
1281
+ args scope false declarations dependencies entry context))
1282
+ ((= operator "let*")
1283
+ (portable-let-expression
1284
+ args scope t declarations dependencies entry context))
1285
+ ((= operator "loop")
1286
+ (portable-loop-expression
1287
+ args scope declarations dependencies entry context))
1288
+ ((or (= operator "setq") (= operator "set!"))
1289
+ (portable-assignment
1290
+ args scope declarations dependencies entry context))
1291
+ ((= operator "js-object")
1292
+ (portable-object
1293
+ args scope declarations dependencies entry context))
1294
+ ((or (= operator "get")
1295
+ (= operator "aref")
1296
+ (= operator "object-has?")
1297
+ (= operator "object-assoc"))
1298
+ (portable-host-key-call
1299
+ args scope declarations dependencies entry context))
1300
+ (t
1301
+ (unless (js-call builtin-operators :includes operator)
1302
+ (portable-expression
1303
+ operator-node scope declarations dependencies entry context))
1304
+ (portable-sequence
1305
+ args scope declarations dependencies entry context)))))
1306
+
1307
+ (defun portable-expression
1308
+ (form scope declarations dependencies entry context)
1309
+ (when form
1310
+ (cond
1311
+ ((or (= (node-kind form) "literal")
1312
+ (= (node-kind form) "undefined")) nil)
1313
+ ((= (node-kind form) "keyword") nil)
1314
+ ((= (node-kind form) "symbol")
1315
+ (portable-reference
1316
+ form scope declarations dependencies entry context))
1317
+ ((= (node-kind form) "vector")
1318
+ (portable-sequence
1319
+ (node-items form) scope declarations dependencies entry context))
1320
+ ((= (node-kind form) "list")
1321
+ (portable-call
1322
+ form scope declarations dependencies entry context)))))
1323
+
1324
+ (defun portable-dependencies (declaration declarations entry context)
1325
+ (let ((dependencies (new (js* "Set")))
1326
+ (scope (portable-scope (get declaration :parameters) context)))
1327
+ (if (= (get declaration :kind) "portable")
1328
+ (portable-sequence
1329
+ (get declaration :body)
1330
+ scope declarations dependencies entry context)
1331
+ (portable-expression
1332
+ (get declaration :initializer)
1333
+ scope declarations dependencies entry context))
1334
+ dependencies))
1335
+
1336
+ (defun validate-portable-module (forms context)
1337
+ (let ((declarations (portable-declarations forms context))
1338
+ (pending (js-array))
1339
+ (visited (new (js* "Set"))))
1340
+ (js-call declarations :forEach
1341
+ (lambda (declaration name)
1342
+ (when (= (get declaration :kind) "portable")
1343
+ (js-call pending :push name))))
1344
+ (while (> (get pending :length) 0)
1345
+ (let ((name (js-call pending :pop)))
1346
+ (unless (js-call visited :has name)
1347
+ (js-call visited :add name)
1348
+ (let ((declaration (js-call declarations :get name)))
1349
+ (js-call
1350
+ (portable-dependencies
1351
+ declaration declarations name context)
1352
+ :forEach
1353
+ (lambda (dependency)
1354
+ (js-call pending :push dependency)))))))
1355
+ forms))
1356
+
1357
+ (defun select-portable-module
1358
+ (forms entries filename &optional allow-project-imports)
1359
+ (let* ((context (js-object :filename filename))
1360
+ (flattened (flatten-modules forms context))
1361
+ (declarations (portable-declarations flattened context))
1362
+ (pending (js-array))
1363
+ (selected (new (js* "Set"))))
1364
+ (js-call entries :forEach
1365
+ (lambda (entry)
1366
+ (let ((declaration (js-call declarations :get entry)))
1367
+ (unless (and declaration (= (get declaration :kind) "portable"))
1368
+ (portable-fail
1369
+ context nil
1370
+ (str "portable entry " entry
1371
+ " is not declared with defportable")))
1372
+ (js-call pending :push entry))))
1373
+ (while (> (get pending :length) 0)
1374
+ (let ((name (js-call pending :pop)))
1375
+ (unless (js-call selected :has name)
1376
+ (js-call selected :add name)
1377
+ (let ((declaration (js-call declarations :get name)))
1378
+ (when (and (= (get declaration :kind) "portable-import")
1379
+ (not allow-project-imports))
1380
+ (portable-fail
1381
+ context (node-span (get declaration :form))
1382
+ (str "portable import " name
1383
+ " requires a project build")))
1384
+ (js-call
1385
+ (portable-dependencies
1386
+ declaration declarations name context)
1387
+ :forEach
1388
+ (lambda (dependency)
1389
+ (js-call pending :push dependency)))))))
1390
+ (js-call flattened :filter
1391
+ (lambda (form)
1392
+ (when (= (node-kind form) "list")
1393
+ (let* ((items (node-items form))
1394
+ (operator (and (symbol-node-p (js-nth 0 items))
1395
+ (node-name (js-nth 0 items))))
1396
+ (name (and (> (get items :length) 1)
1397
+ (symbol-node-p (js-nth 1 items))
1398
+ (node-name (js-nth 1 items)))))
1399
+ (or
1400
+ (and (or (= operator "defconst")
1401
+ (= operator "defportable"))
1402
+ (js-call selected :has name))
1403
+ (and (= operator "import-portable")
1404
+ (js-call
1405
+ (js-call items :slice 2)
1406
+ :some
1407
+ (lambda (imported)
1408
+ (js-call selected :has (node-name imported))))))))))))
1409
+
1410
+ (defun analyze-module (forms filename)
1411
+ (let* ((context
1412
+ (js-object
1413
+ :filename filename
1414
+ :tailPosition false
1415
+ :recurTargets (js-array)
1416
+ :recurBarrier false))
1417
+ (flattened (flatten-modules forms context))
1418
+ (scope (make-scope nil)))
1419
+ (predeclare-top-level flattened scope context)
1420
+ (js-call flattened :forEach
1421
+ (lambda (form) (analyze-top-level form scope context)))
1422
+ (analyze-transients forms filename)
1423
+ (validate-portable-module flattened context)
1424
+ forms))
1425
+
1426
+ (export analyze-module select-portable-module))