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,1745 @@
1
+ (module bootstrap.compiler.emitter
2
+ (import "./symbol.mjs" binding-name reference-name)
3
+ (import "./ir.mjs"
4
+ node-p
5
+ program-filename
6
+ program-body
7
+ ir-kind
8
+ ir-span
9
+ ir-value
10
+ ir-children
11
+ ir-property)
12
+ (import "./source-map.mjs" create-source-map)
13
+
14
+ (defconst literal-runtime-import
15
+ "import { hashMap as __eliscript_hash_map, hashSet as __eliscript_hash_set, keyword as __eliscript_keyword, list as __eliscript_list, queue as __eliscript_queue, symbol as __eliscript_symbol, vector as __eliscript_vector } from \"eliscript/runtime/literals.mjs\";\n")
16
+ (defconst collection-runtime-import
17
+ "import { count as __eliscript_count, nth as __eliscript_nth } from \"eliscript/runtime/core/collection.mjs\";\n")
18
+ (defconst list-runtime-import
19
+ "import { cons as __eliscript_cons, first as __eliscript_first, rest as __eliscript_rest } from \"eliscript/runtime/core/list.mjs\";\n")
20
+ (defconst value-runtime-import
21
+ "import { equalValues as __eliscript_equal } from \"eliscript/runtime/core/value.mjs\";\n")
22
+ (defconst binding-runtime
23
+ "import { get as __eliscript_get } from \"eliscript/runtime/core/collection.mjs\";\nconst __eliscript_binding_missing = Symbol(\"eliscript.binding.missing\");\nconst __eliscript_binding_get = (value, key, notFound) => { if (value == null) return notFound; const prototype = typeof value === \"object\" ? Object.getPrototypeOf(value) : undefined; if (prototype === Object.prototype || prototype === null) { const type = key && Object.getOwnPropertyDescriptor(key, Symbol.for(\"eliscript.value.type\")); if (type?.value === \"keyword\" || type?.value === \"symbol\") key = key.qualifiedName; } return __eliscript_get(value, key, notFound); };\n")
24
+ (defconst host-identity-token-helper
25
+ "const __eliscript_host_identity_token = (() => { const objects = new WeakMap(); const symbols = new Map(); let next = 1; return (value) => { const type = typeof value; if ((type !== \"object\" || value === null) && type !== \"function\" && type !== \"symbol\") throw new TypeError(\"host-identity-token expects an object, function, or symbol\"); const identities = type === \"symbol\" ? symbols : objects; const cached = identities.get(value); if (cached !== undefined) return cached; if (next > Number.MAX_SAFE_INTEGER) throw new RangeError(\"host identity token space exhausted\"); const token = next; next += 1; identities.set(value, token); return token; }; })();\n")
26
+ (defconst fail
27
+ (js* "(message) => { throw new Error(message); }"))
28
+ (defconst native-locate
29
+ (js* "(span, output) => { const fragment = typeof output === \"string\" ? { text: output, marks: [] } : output; const marks = fragment.marks; if (fragment.text.length === 0 || span == null || span === false || (marks.length > 0 && marks[0].offset === 0)) return fragment; return { text: fragment.text, marks: [{ offset: 0, span }, ...marks] }; }"))
30
+ (defvar temporary-counter 0)
31
+ (defvar binding-temporaries (new globalThis.Map))
32
+
33
+ (defun make-fragment (text marks)
34
+ (js-object :text text :marks marks))
35
+
36
+ (defun text-fragment (text)
37
+ (make-fragment text (js-array)))
38
+
39
+ (defun as-fragment (part)
40
+ (if (= (js* "typeof part") "string")
41
+ (text-fragment part)
42
+ part))
43
+
44
+ (defun combine (parts)
45
+ (let ((text "")
46
+ (marks (js-array))
47
+ (part-index 0)
48
+ (fragment nil)
49
+ (offset 0)
50
+ (fragment-marks nil)
51
+ (mark-index 0)
52
+ (mark nil))
53
+ (while (< part-index (get parts :length))
54
+ (setq fragment (as-fragment (js-nth part-index parts)))
55
+ (setq offset (get text :length))
56
+ (setq fragment-marks (get fragment :marks))
57
+ (setq mark-index 0)
58
+ (while (< mark-index (get fragment-marks :length))
59
+ (setq mark (js-nth mark-index fragment-marks))
60
+ (js-call marks :push
61
+ (js-object
62
+ :offset (+ offset (get mark :offset))
63
+ :span (get mark :span)))
64
+ (setq mark-index (1+ mark-index)))
65
+ (setq text (str text (get fragment :text)))
66
+ (setq part-index (1+ part-index)))
67
+ (make-fragment text marks)))
68
+
69
+ (defun join-fragments (parts separator)
70
+ (let ((joined (js-array))
71
+ (index 0))
72
+ (while (< index (get parts :length))
73
+ (when (> index 0)
74
+ (js-call joined :push separator))
75
+ (js-call joined :push (js-nth index parts))
76
+ (setq index (1+ index)))
77
+ (combine joined)))
78
+
79
+ (defun reference-indent (fragment)
80
+ (let* ((input (as-fragment fragment))
81
+ (source (get input :text))
82
+ (source-marks (get input :marks))
83
+ (text "")
84
+ (marks (js-array))
85
+ (index 0)
86
+ (mark-index 0)
87
+ (line-start t))
88
+ (while (< index (get source :length))
89
+ (let ((code (js-call source :charCodeAt index)))
90
+ (when (and line-start (not (= code 10)))
91
+ (setq text (str text " ")))
92
+ (while (and (< mark-index (get source-marks :length))
93
+ (= (get (js-nth mark-index source-marks) :offset) index))
94
+ (js-call marks :push
95
+ (js-object
96
+ :offset (get text :length)
97
+ :span (get (js-nth mark-index source-marks) :span)))
98
+ (setq mark-index (1+ mark-index)))
99
+ (setq text (str text (js-call source :charAt index)))
100
+ (setq line-start (= code 10))
101
+ (setq index (1+ index))))
102
+ (make-fragment text marks)))
103
+
104
+ (defun indent (fragment)
105
+ (let* ((input (as-fragment fragment))
106
+ (source (get input :text))
107
+ (source-marks (get input :marks))
108
+ (text-parts (js-array))
109
+ (marks (js-array))
110
+ (source-length (get source :length))
111
+ (output-length 0)
112
+ (source-index 0)
113
+ (mark-index 0)
114
+ (mark-count (get source-marks :length)))
115
+ (while (< source-index source-length)
116
+ (let* ((newline-index (js-call source :indexOf "\n" source-index))
117
+ (segment-end
118
+ (if (= newline-index -1)
119
+ source-length
120
+ (1+ newline-index)))
121
+ (indented (not (= (js-call source :charCodeAt source-index) 10))))
122
+ (when indented
123
+ (js-call text-parts :push " ")
124
+ (setq output-length (+ output-length 2)))
125
+ (while (and (< mark-index mark-count)
126
+ (>= (get (js-nth mark-index source-marks) :offset)
127
+ source-index)
128
+ (< (get (js-nth mark-index source-marks) :offset)
129
+ segment-end))
130
+ (let ((mark (js-nth mark-index source-marks)))
131
+ (js-call marks :push
132
+ (js-object
133
+ :offset (+ output-length
134
+ (- (get mark :offset) source-index))
135
+ :span (get mark :span))))
136
+ (setq mark-index (1+ mark-index)))
137
+ (js-call text-parts :push
138
+ (js-call source :slice source-index segment-end))
139
+ (setq output-length
140
+ (+ output-length (- segment-end source-index)))
141
+ (setq source-index segment-end)))
142
+ (make-fragment (js-call text-parts :join "") marks)))
143
+
144
+ (defun reference-locate (node output)
145
+ (let* ((fragment (as-fragment output))
146
+ (marks (get fragment :marks))
147
+ (has-start
148
+ (js-call marks :some
149
+ (lambda (mark) (= (get mark :offset) 0)))))
150
+ (if (or (= (get (get fragment :text) :length) 0)
151
+ (not (ir-span node))
152
+ has-start)
153
+ fragment
154
+ (let ((located
155
+ (js-array (js-object :offset 0 :span (ir-span node)))))
156
+ (js-call marks :forEach
157
+ (lambda (mark) (js-call located :push mark)))
158
+ (make-fragment (get fragment :text) located)))))
159
+
160
+ (defun locate (node output)
161
+ (native-locate (ir-span node) output))
162
+
163
+ (defun fresh-name ()
164
+ (setq temporary-counter (1+ temporary-counter))
165
+ (str "__eliscript_value_" temporary-counter))
166
+
167
+ (defun json-string (value)
168
+ (JSON/stringify value))
169
+
170
+ (defun children (node)
171
+ (or (ir-children node) (js-array)))
172
+
173
+ (defun require-arity (name nodes minimum maximum)
174
+ (let ((count (get nodes :length)))
175
+ (when (or (< count minimum)
176
+ (and maximum (> count maximum)))
177
+ (let ((expected
178
+ (if (and maximum (= minimum maximum))
179
+ (str minimum)
180
+ (str minimum (if maximum (str ".." maximum) "+"))))
181
+ (plural
182
+ (if (and maximum (= minimum maximum) (= minimum 1))
183
+ ""
184
+ "s")))
185
+ (fail
186
+ (str name " expects " expected " argument" plural
187
+ ", got " count))))))
188
+
189
+ (defun require-node-arity (node minimum maximum)
190
+ (require-arity
191
+ (or (ir-value node) (ir-kind node))
192
+ (children node)
193
+ minimum
194
+ maximum))
195
+
196
+ (defun emit-binding-name (node)
197
+ (native-locate (ir-span node) (binding-name (ir-value node))))
198
+
199
+ (defun emit-array-binding-pattern (node)
200
+ (let* ((nodes (children node))
201
+ (rest (ir-property node "rest"))
202
+ (last-index (1- (get nodes :length)))
203
+ (parts (js-array))
204
+ (index 0)
205
+ (last-hole false))
206
+ (js-call nodes :forEach
207
+ (lambda (child)
208
+ (setq last-hole (= (ir-kind child) "binding-hole"))
209
+ (js-call parts :push
210
+ (cond
211
+ ((and rest (= index last-index))
212
+ (combine (js-array "..." (emit-binding-pattern-node child))))
213
+ (last-hole "")
214
+ (t (emit-binding-pattern-node child))))
215
+ (setq index (1+ index))))
216
+ (native-locate
217
+ (ir-span node)
218
+ (combine
219
+ (js-array "[" (join-fragments parts ", ")
220
+ (if (and last-hole (not rest)) "," "") "]")))))
221
+
222
+ (defun map-binding-pattern-p (node)
223
+ (and (ir-property node "pattern")
224
+ (= (ir-kind (js-nth 0 (children node))) "map-binding-pattern")))
225
+
226
+ (defun binding-temporary (node)
227
+ (if (js-call binding-temporaries :has node)
228
+ (js-call binding-temporaries :get node)
229
+ (let ((name (fresh-name)))
230
+ (js-call binding-temporaries :set node name)
231
+ name)))
232
+
233
+ (defun emit-map-binding-pattern (node source)
234
+ (let* ((nodes (children node))
235
+ (entry-count (ir-property node "entryCount"))
236
+ (entries (js-call nodes :slice 0 entry-count))
237
+ (as (and (ir-property node "as") (js-nth entry-count nodes)))
238
+ (parts (js-array)))
239
+ (when as
240
+ (js-call parts :push
241
+ (combine (js-array "let " (emit-binding-name as) " = " source ";"))))
242
+ (js-call entries :forEach
243
+ (lambda (entry)
244
+ (let* ((entry-nodes (children entry))
245
+ (target (js-nth 0 entry-nodes))
246
+ (key (js-nth 1 entry-nodes))
247
+ (default (and (ir-property entry "default")
248
+ (js-nth 2 entry-nodes)))
249
+ (temporary (fresh-name))
250
+ (resolved
251
+ (combine
252
+ (js-array "(" temporary
253
+ " === __eliscript_binding_missing ? "
254
+ (if default (emit-expression default) "null")
255
+ " : " temporary ")"))))
256
+ (js-call parts :push
257
+ (combine
258
+ (js-array "const " temporary
259
+ " = __eliscript_binding_get(" source ", "
260
+ (emit-expression key)
261
+ ", __eliscript_binding_missing);")))
262
+ (js-call parts :push (emit-pattern-setup target resolved)))))
263
+ (join-fragments parts "\n")))
264
+
265
+ (defun emit-pattern-setup (pattern source)
266
+ (let ((kind (ir-kind pattern)))
267
+ (cond
268
+ ((= kind "binding-name")
269
+ (combine (js-array "let " (emit-binding-name pattern) " = " source ";")))
270
+ ((= kind "array-binding-pattern")
271
+ (combine
272
+ (js-array "let " (emit-array-binding-pattern pattern) " = " source ";")))
273
+ ((= kind "map-binding-pattern")
274
+ (emit-map-binding-pattern pattern source))
275
+ (t (fail (str "invalid IR binding setup node: " kind))))))
276
+
277
+ (defun emit-binding-setup (binding)
278
+ (if (map-binding-pattern-p binding)
279
+ (emit-map-binding-pattern
280
+ (js-nth 0 (children binding)) (binding-temporary binding))
281
+ (text-fragment "")))
282
+
283
+ (defun emit-binding-setups (bindings)
284
+ (let ((parts (js-array)))
285
+ (js-call bindings :forEach
286
+ (lambda (binding)
287
+ (when (map-binding-pattern-p binding)
288
+ (js-call parts :push (emit-binding-setup binding)))))
289
+ (join-fragments parts "\n")))
290
+
291
+ (defun binding-default-value (binding)
292
+ (if (and (ir-property binding "pattern")
293
+ (not (map-binding-pattern-p binding)))
294
+ "[]"
295
+ "null"))
296
+
297
+ (defun emit-binding-pattern-node (node)
298
+ (let ((kind (ir-kind node)))
299
+ (cond
300
+ ((= kind "binding-name") (emit-binding-name node))
301
+ ((= kind "binding-hole") "")
302
+ ((= kind "array-binding-pattern")
303
+ (emit-array-binding-pattern node))
304
+ ((= kind "map-binding-pattern")
305
+ (fail "map binding patterns require a generated source binding"))
306
+ (t (fail (str "invalid IR binding pattern node: " kind))))))
307
+
308
+ (defun emit-binding-target (node)
309
+ (if (map-binding-pattern-p node)
310
+ (binding-temporary node)
311
+ (if (ir-property node "pattern")
312
+ (emit-binding-pattern-node (js-nth 0 (children node)))
313
+ (emit-binding-name node))))
314
+
315
+ (defun binding-initializer (node)
316
+ (let ((nodes (children node)))
317
+ (js-nth 0
318
+ (if (ir-property node "pattern")
319
+ (js-call nodes :slice 1)
320
+ nodes))))
321
+
322
+ (defun emit-parameter (node)
323
+ (let ((name (emit-binding-target node))
324
+ (kind (ir-property node "parameterKind"))
325
+ (pattern (ir-property node "pattern")))
326
+ (cond
327
+ ((= kind "optional")
328
+ (combine
329
+ (js-array name " = "
330
+ (if pattern (binding-default-value node) "null"))))
331
+ ((= kind "rest") (combine (js-array "..." name)))
332
+ (t name))))
333
+
334
+ (defun emit-arguments (nodes)
335
+ (join-fragments
336
+ (js-call nodes :map (lambda (node) (emit-expression node)))
337
+ ", "))
338
+
339
+ (defun emit-callee (node)
340
+ (let ((output (emit-expression node)))
341
+ (if (= (ir-kind node) "reference")
342
+ output
343
+ (combine (js-array "(" output ")")))))
344
+
345
+ (defun emit-returning-body (nodes)
346
+ (if (= (get nodes :length) 0)
347
+ (text-fragment "return null;")
348
+ (let ((parts (js-array))
349
+ (index 0)
350
+ (last-index (1- (get nodes :length))))
351
+ (while (< index last-index)
352
+ (js-call parts :push (emit-expression (js-nth index nodes)))
353
+ (js-call parts :push ";\n")
354
+ (setq index (1+ index)))
355
+ (js-call parts :push "return ")
356
+ (js-call parts :push (emit-expression (js-nth last-index nodes)))
357
+ (js-call parts :push ";")
358
+ (combine parts))))
359
+
360
+ (defun emit-statement-body (nodes)
361
+ (let ((parts (js-array)))
362
+ (js-call nodes :forEach
363
+ (lambda (node)
364
+ (js-call parts :push (combine (js-array (emit-expression node) ";")))))
365
+ (join-fragments parts "\n")))
366
+
367
+ (defun contains-await-p (value)
368
+ (let ((pending (js-array value))
369
+ (current nil)
370
+ (kind nil)
371
+ (nodes nil)
372
+ (index 0)
373
+ (found false))
374
+ (while (and (not found) (> (get pending :length) 0))
375
+ (setq current (js-call pending :pop))
376
+ (cond
377
+ ((node-p current)
378
+ (setq kind (ir-kind current))
379
+ (cond
380
+ ((= kind "await-expression") (setq found t))
381
+ ((not (= kind "function-expression"))
382
+ (setq nodes (children current))
383
+ (setq index (1- (get nodes :length)))
384
+ (while (>= index 0)
385
+ (js-call pending :push (js-nth index nodes))
386
+ (setq index (1- index))))))
387
+ ((js* "Array.isArray(current)")
388
+ (setq index (1- (get current :length)))
389
+ (while (>= index 0)
390
+ (js-call pending :push (js-nth index current))
391
+ (setq index (1- index))))))
392
+ found))
393
+
394
+ (defun emit-iife (parameters body invocation asynchronous)
395
+ (if asynchronous
396
+ (combine
397
+ (js-array "(await (async (" parameters ") => {\n" body "\n})("
398
+ invocation "))"))
399
+ (combine
400
+ (js-array "((" parameters ") => {\n" body "\n})(" invocation ")"))))
401
+
402
+ (defun emit-do (nodes)
403
+ (cond
404
+ ((= (get nodes :length) 0) (text-fragment "null"))
405
+ ((= (get nodes :length) 1) (emit-expression (js-nth 0 nodes)))
406
+ (t
407
+ (emit-iife
408
+ "" (indent (emit-returning-body nodes)) ""
409
+ (contains-await-p nodes)))))
410
+
411
+ (defun split-counted-children (node property)
412
+ (let ((all (children node))
413
+ (count (ir-property node property)))
414
+ (js-array (js-call all :slice 0 count) (js-call all :slice count))))
415
+
416
+ (defun emit-function (node)
417
+ (let* ((parts (split-counted-children node "parameterCount"))
418
+ (parameters (js-nth 0 parts))
419
+ (body (js-nth 1 parts)))
420
+ (combine
421
+ (js-array (if (ir-property node "async") "async " "") "("
422
+ (join-fragments
423
+ (js-call parameters :map
424
+ (lambda (parameter) (emit-parameter parameter)))
425
+ ", ")
426
+ ") => {\n"
427
+ (indent (emit-function-body parameters body))
428
+ "\n}"))))
429
+
430
+ (defun contains-recur-target-p (value target-kind)
431
+ (let ((pending (js-array value))
432
+ (current nil)
433
+ (kind nil)
434
+ (nodes nil)
435
+ (index 0)
436
+ (found false))
437
+ (while (and (not found) (> (get pending :length) 0))
438
+ (setq current (js-call pending :pop))
439
+ (cond
440
+ ((node-p current)
441
+ (setq kind (ir-kind current))
442
+ (cond
443
+ ((= kind "recur")
444
+ (when (= (ir-property current "targetKind") target-kind)
445
+ (setq found t)))
446
+ ((not (= kind "function-expression"))
447
+ (setq nodes (children current))
448
+ (setq index (1- (get nodes :length)))
449
+ (while (>= index 0)
450
+ (js-call pending :push (js-nth index nodes))
451
+ (setq index (1- index))))))
452
+ ((js* "Array.isArray(current)")
453
+ (setq index (1- (get current :length)))
454
+ (while (>= index 0)
455
+ (js-call pending :push (js-nth index current))
456
+ (setq index (1- index))))))
457
+ found))
458
+
459
+ (defun emit-rebinding-target (binding temporary)
460
+ (let ((target (emit-binding-target binding)))
461
+ (if (ir-property binding "pattern")
462
+ (combine (js-array "(" target " = " temporary ");"))
463
+ (combine (js-array target " = " temporary ";")))))
464
+
465
+ (defun emit-recur-tail (node target)
466
+ (unless (= (ir-property node "targetKind") (get target :kind))
467
+ (fail "recur target does not match its enclosing loop"))
468
+ (let ((args (children node))
469
+ (bindings (get target :bindings))
470
+ (temporaries (js-array))
471
+ (parts (js-array))
472
+ (index 0))
473
+ (while (< index (get args :length))
474
+ (let ((temporary (fresh-name)))
475
+ (js-call temporaries :push temporary)
476
+ (js-call parts :push
477
+ (combine
478
+ (js-array "const " temporary " = "
479
+ (emit-expression (js-nth index args)) ";"))))
480
+ (setq index (1+ index)))
481
+ (setq index 0)
482
+ (while (< index (get bindings :length))
483
+ (js-call parts :push
484
+ (emit-rebinding-target
485
+ (js-nth index bindings) (js-nth index temporaries)))
486
+ (setq index (1+ index)))
487
+ (js-call parts :push
488
+ (combine (js-array "continue " (get target :label) ";")))
489
+ (native-locate (ir-span node) (join-fragments parts "\n"))))
490
+
491
+ (defun emit-tail-body (nodes target)
492
+ (if (= (get nodes :length) 0)
493
+ (text-fragment "return null;")
494
+ (let ((parts (js-array))
495
+ (index 0)
496
+ (last-index (1- (get nodes :length))))
497
+ (while (< index last-index)
498
+ (js-call parts :push
499
+ (combine (js-array (emit-expression (js-nth index nodes)) ";")))
500
+ (setq index (1+ index)))
501
+ (js-call parts :push
502
+ (emit-tail-node (js-nth last-index nodes) target))
503
+ (join-fragments parts "\n"))))
504
+
505
+ (defun emit-tail-cond (clauses target)
506
+ (if (= (get clauses :length) 0)
507
+ (text-fragment "return null;")
508
+ (let* ((clause (js-nth 0 clauses))
509
+ (nodes (children clause))
510
+ (test (js-nth 0 nodes))
511
+ (body (js-call nodes :slice 1)))
512
+ (native-locate
513
+ (ir-span clause)
514
+ (if (and (= (ir-kind test) "literal")
515
+ (eq (ir-value test) t))
516
+ (emit-tail-body body target)
517
+ (combine
518
+ (js-array "if (__eliscript_truthy(" (emit-expression test)
519
+ ")) {\n" (indent (emit-tail-body body target))
520
+ "\n} else {\n"
521
+ (indent
522
+ (emit-tail-cond (js-call clauses :slice 1) target))
523
+ "\n}")))))))
524
+
525
+ (defun emit-tail-let (node target)
526
+ (let* ((parts (split-counted-children node "bindingCount"))
527
+ (bindings (js-nth 0 parts))
528
+ (body (js-nth 1 parts))
529
+ (sequential (ir-property node "sequential"))
530
+ (statements (js-array))
531
+ (temporaries (js-array))
532
+ (index 0))
533
+ (if sequential
534
+ (while (< index (get bindings :length))
535
+ (let* ((binding (js-nth index bindings))
536
+ (initializer (binding-initializer binding)))
537
+ (js-call statements :push
538
+ (combine
539
+ (js-array "let " (emit-binding-target binding) " = "
540
+ (if initializer
541
+ (emit-expression initializer)
542
+ (binding-default-value binding))
543
+ ";")))
544
+ (when (map-binding-pattern-p binding)
545
+ (js-call statements :push (emit-binding-setup binding))))
546
+ (setq index (1+ index)))
547
+ (progn
548
+ (while (< index (get bindings :length))
549
+ (let* ((binding (js-nth index bindings))
550
+ (initializer (binding-initializer binding))
551
+ (temporary (fresh-name)))
552
+ (js-call temporaries :push temporary)
553
+ (js-call statements :push
554
+ (combine
555
+ (js-array "const " temporary " = "
556
+ (if initializer
557
+ (emit-expression initializer)
558
+ (binding-default-value binding))
559
+ ";"))))
560
+ (setq index (1+ index)))
561
+ (setq index 0)
562
+ (while (< index (get bindings :length))
563
+ (js-call statements :push
564
+ (combine
565
+ (js-array "let " (emit-binding-target (js-nth index bindings)) " = "
566
+ (js-nth index temporaries) ";")))
567
+ (when (map-binding-pattern-p (js-nth index bindings))
568
+ (js-call statements :push
569
+ (emit-binding-setup (js-nth index bindings))))
570
+ (setq index (1+ index)))))
571
+ (js-call statements :push (emit-tail-body body target))
572
+ (native-locate
573
+ (ir-span node)
574
+ (combine
575
+ (js-array "{\n" (indent (join-fragments statements "\n")) "\n}")))))
576
+
577
+ (defun emit-tail-short-circuit (nodes kind target)
578
+ (cond
579
+ ((= (get nodes :length) 0)
580
+ (text-fragment (if (= kind "and") "return true;" "return null;")))
581
+ ((= (get nodes :length) 1)
582
+ (emit-tail-node (js-nth 0 nodes) target))
583
+ (t
584
+ (let* ((temporary (fresh-name))
585
+ (first (emit-expression (js-nth 0 nodes)))
586
+ (rest (js-call nodes :slice 1))
587
+ (truthy
588
+ (if (= kind "and")
589
+ (emit-tail-short-circuit rest kind target)
590
+ (combine (js-array "return " temporary ";"))))
591
+ (falsey
592
+ (if (= kind "and")
593
+ (combine (js-array "return " temporary ";"))
594
+ (emit-tail-short-circuit rest kind target))))
595
+ (combine
596
+ (js-array "const " temporary " = " first ";\n"
597
+ "if (__eliscript_truthy(" temporary ")) {\n"
598
+ (indent truthy) "\n} else {\n" (indent falsey) "\n}"))))))
599
+
600
+ (defun emit-tail-node (node target)
601
+ (let ((kind (ir-kind node))
602
+ (nodes (children node)))
603
+ (cond
604
+ ((= kind "recur") (emit-recur-tail node target))
605
+ ((= kind "conditional")
606
+ (combine
607
+ (js-array "if (__eliscript_truthy(" (emit-expression (js-nth 0 nodes))
608
+ ")) {\n" (indent (emit-tail-node (js-nth 1 nodes) target))
609
+ "\n} else {\n"
610
+ (indent
611
+ (if (> (get nodes :length) 2)
612
+ (emit-tail-node (js-nth 2 nodes) target)
613
+ (text-fragment "return null;")))
614
+ "\n}")))
615
+ ((= kind "conditional-sugar")
616
+ (combine
617
+ (js-array "if (" (if (= (ir-value node) "when") "" "!")
618
+ "__eliscript_truthy(" (emit-expression (js-nth 0 nodes))
619
+ ")) {\n"
620
+ (indent (emit-tail-body (js-call nodes :slice 1) target))
621
+ "\n} else {\n return null;\n}")))
622
+ ((= kind "conditional-chain") (emit-tail-cond nodes target))
623
+ ((= kind "sequence") (emit-tail-body nodes target))
624
+ ((= kind "lexical-bindings") (emit-tail-let node target))
625
+ ((= kind "short-circuit")
626
+ (emit-tail-short-circuit nodes (ir-value node) target))
627
+ (t (combine (js-array "return " (emit-expression node) ";"))))))
628
+
629
+ (defun emit-function-body (parameters body)
630
+ (let ((setups (emit-binding-setups parameters)))
631
+ (if (not (contains-recur-target-p body "function"))
632
+ (if (= (get (get setups :text) :length) 0)
633
+ (emit-returning-body body)
634
+ (combine (js-array setups "\n" (emit-returning-body body))))
635
+ (let* ((label (fresh-name))
636
+ (target
637
+ (js-object
638
+ :kind "function" :bindings parameters :label label)))
639
+ (combine
640
+ (js-array label ": while (true) {\n"
641
+ (indent
642
+ (if (= (get (get setups :text) :length) 0)
643
+ (emit-tail-body body target)
644
+ (combine
645
+ (js-array setups "\n" (emit-tail-body body target)))))
646
+ "\n}"))))))
647
+
648
+ (defun emit-if (nodes)
649
+ (combine
650
+ (js-array "(__eliscript_truthy("
651
+ (emit-expression (js-nth 0 nodes))
652
+ ") ? "
653
+ (emit-expression (js-nth 1 nodes))
654
+ " : "
655
+ (if (> (get nodes :length) 2)
656
+ (emit-expression (js-nth 2 nodes))
657
+ "null")
658
+ ")")))
659
+
660
+ (defun emit-conditional-sugar (node)
661
+ (let* ((nodes (children node))
662
+ (test (emit-expression (js-nth 0 nodes)))
663
+ (body (emit-do (js-call nodes :slice 1))))
664
+ (if (= (ir-value node) "when")
665
+ (combine (js-array "(__eliscript_truthy(" test ") ? " body " : null)"))
666
+ (combine
667
+ (js-array "(__eliscript_truthy((!__eliscript_truthy("
668
+ test "))) ? " body " : null)")))))
669
+
670
+ (defun emit-cond (clauses)
671
+ (if (= (get clauses :length) 0)
672
+ (text-fragment "null")
673
+ (let* ((clause (js-nth 0 clauses))
674
+ (nodes (children clause))
675
+ (test (js-nth 0 nodes))
676
+ (body (js-call nodes :slice 1)))
677
+ (native-locate
678
+ (ir-span clause)
679
+ (if (and (= (ir-kind test) "literal") (= (ir-value test) t))
680
+ (emit-do body)
681
+ (combine
682
+ (js-array "(__eliscript_truthy("
683
+ (emit-expression test)
684
+ ") ? "
685
+ (emit-do body)
686
+ " : "
687
+ (emit-cond (js-call clauses :slice 1))
688
+ ")")))))))
689
+
690
+ (defun emit-throw (node)
691
+ (let ((temporary (fresh-name)))
692
+ (combine
693
+ (js-array "((" temporary ") => { throw " temporary "; })("
694
+ (emit-expression (js-nth 0 (children node))) ")"))))
695
+
696
+ (defun emit-catch (node)
697
+ (let* ((nodes (children node))
698
+ (binding (js-nth 0 nodes))
699
+ (body (js-call nodes :slice 1)))
700
+ (native-locate
701
+ (ir-span node)
702
+ (combine
703
+ (js-array " catch (" (emit-binding-target binding) ") {\n"
704
+ (indent
705
+ (if (map-binding-pattern-p binding)
706
+ (combine
707
+ (js-array (emit-binding-setup binding) "\n"
708
+ (emit-returning-body body)))
709
+ (emit-returning-body body)))
710
+ "\n}")))))
711
+
712
+ (defun emit-finally (node)
713
+ (native-locate
714
+ (ir-span node)
715
+ (combine
716
+ (js-array " finally {\n"
717
+ (indent (emit-statement-body (children node)))
718
+ "\n}"))))
719
+
720
+ (defun emit-try (node)
721
+ (let* ((parts (split-counted-children node "bodyCount"))
722
+ (body (js-nth 0 parts))
723
+ (clauses (js-nth 1 parts))
724
+ (catch-clause
725
+ (js-call clauses :find
726
+ (lambda (clause) (= (ir-kind clause) "catch-clause"))))
727
+ (finally-clause
728
+ (js-call clauses :find
729
+ (lambda (clause) (= (ir-kind clause) "finally-clause"))))
730
+ (try-block
731
+ (combine
732
+ (js-array "try {\n"
733
+ (indent (emit-returning-body body))
734
+ "\n}"
735
+ (if catch-clause (emit-catch catch-clause) "")
736
+ (if finally-clause (emit-finally finally-clause) "")))))
737
+ (emit-iife
738
+ "" (indent try-block) "" (contains-await-p node))))
739
+
740
+ (defun emit-binding-values (bindings)
741
+ (let ((result (js-array)))
742
+ (js-call bindings :forEach
743
+ (lambda (binding)
744
+ (let ((initializer (binding-initializer binding)))
745
+ (js-call result :push
746
+ (if initializer
747
+ (emit-expression initializer)
748
+ (binding-default-value binding))))))
749
+ result))
750
+
751
+ (defun emit-parallel-let (bindings body)
752
+ (let* ((names
753
+ (join-fragments
754
+ (js-call bindings :map
755
+ (lambda (binding) (emit-binding-target binding)))
756
+ ", "))
757
+ (values (join-fragments (emit-binding-values bindings) ", "))
758
+ (setups (emit-binding-setups bindings))
759
+ (body-output
760
+ (indent
761
+ (if (= (get (get setups :text) :length) 0)
762
+ (emit-returning-body body)
763
+ (combine
764
+ (js-array setups "\n" (emit-returning-body body)))))))
765
+ (emit-iife names body-output values (contains-await-p body))))
766
+
767
+ (defun emit-sequential-let (bindings body)
768
+ (if (= (get bindings :length) 0)
769
+ (emit-do body)
770
+ (let ((binding (js-nth 0 bindings)))
771
+ (let* ((name (emit-binding-target binding))
772
+ (initializer (js-nth 0 (emit-binding-values (js-array binding))))
773
+ (tail (emit-sequential-let (js-call bindings :slice 1) body)))
774
+ (emit-iife
775
+ name
776
+ (indent
777
+ (combine
778
+ (js-array
779
+ (if (map-binding-pattern-p binding)
780
+ (combine (js-array (emit-binding-setup binding) "\n"))
781
+ "")
782
+ "return " tail ";")))
783
+ initializer
784
+ (or
785
+ (contains-await-p (js-call bindings :slice 1))
786
+ (contains-await-p body)))))))
787
+
788
+ (defun emit-let (node)
789
+ (let* ((parts (split-counted-children node "bindingCount"))
790
+ (bindings (js-nth 0 parts))
791
+ (body (js-nth 1 parts)))
792
+ (if (ir-property node "sequential")
793
+ (emit-sequential-let bindings body)
794
+ (emit-parallel-let bindings body))))
795
+
796
+ (defun emit-assignment (node)
797
+ (let ((assignments (js-array)))
798
+ (js-call (children node) :forEach
799
+ (lambda (pair)
800
+ (js-call assignments :push
801
+ (native-locate
802
+ (ir-span pair)
803
+ (combine
804
+ (js-array "("
805
+ (emit-binding-name pair)
806
+ " = "
807
+ (emit-expression (js-nth 0 (children pair)))
808
+ ")"))))))
809
+ (if (= (ir-value node) "set!")
810
+ (js-nth 0 assignments)
811
+ (combine (js-array "(" (join-fragments assignments ", ") ")")))))
812
+
813
+ (defun emit-loop (node)
814
+ (let* ((nodes (children node))
815
+ (test (js-nth 0 nodes))
816
+ (body (js-call nodes :slice 1))
817
+ (test-output (emit-expression test))
818
+ (statements (js-array)))
819
+ (js-call body :forEach
820
+ (lambda (child)
821
+ (js-call statements :push (combine (js-array (emit-expression child) ";")))))
822
+ (emit-iife
823
+ ""
824
+ (indent
825
+ (combine
826
+ (js-array "while (__eliscript_truthy("
827
+ test-output
828
+ ")) {\n"
829
+ (indent (join-fragments statements "\n"))
830
+ "\n}\nreturn null;")))
831
+ ""
832
+ (contains-await-p nodes))))
833
+
834
+ (defun emit-binding-loop (node)
835
+ (let* ((parts (split-counted-children node "bindingCount"))
836
+ (bindings (js-nth 0 parts))
837
+ (body (js-nth 1 parts))
838
+ (label (fresh-name))
839
+ (target
840
+ (js-object :kind "loop" :bindings bindings :label label))
841
+ (parameters
842
+ (join-fragments
843
+ (js-call bindings :map
844
+ (lambda (binding) (emit-binding-target binding)))
845
+ ", "))
846
+ (initializers
847
+ (join-fragments
848
+ (js-call bindings :map
849
+ (lambda (binding)
850
+ (emit-expression (binding-initializer binding))))
851
+ ", "))
852
+ (setups (emit-binding-setups bindings)))
853
+ (emit-iife
854
+ parameters
855
+ (indent
856
+ (combine
857
+ (js-array label ": while (true) {\n"
858
+ (indent
859
+ (if (= (get (get setups :text) :length) 0)
860
+ (emit-tail-body body target)
861
+ (combine
862
+ (js-array setups "\n" (emit-tail-body body target)))))
863
+ "\n}")))
864
+ initializers
865
+ (contains-await-p body))))
866
+
867
+ (defun emit-short-circuit (nodes kind)
868
+ (cond
869
+ ((= (get nodes :length) 0)
870
+ (text-fragment (if (= kind "and") "true" "null")))
871
+ ((= (get nodes :length) 1) (emit-expression (js-nth 0 nodes)))
872
+ (t
873
+ (let ((temporary (fresh-name))
874
+ (first (emit-expression (js-nth 0 nodes)))
875
+ (rest (emit-short-circuit (js-call nodes :slice 1) kind)))
876
+ (let ((body
877
+ (combine
878
+ (js-array "(__eliscript_truthy(" temporary ") ? "
879
+ (if (= kind "and") rest temporary)
880
+ " : "
881
+ (if (= kind "and") temporary rest)
882
+ ")"))))
883
+ (if (contains-await-p (js-call nodes :slice 1))
884
+ (combine
885
+ (js-array "(await (async (" temporary ") => " body ")(" first "))"))
886
+ (combine (js-array "((" temporary ") => " body ")(" first ")"))))))))
887
+
888
+ (defun emit-infix (name nodes operator minimum)
889
+ (require-arity name nodes minimum nil)
890
+ (combine
891
+ (js-array "("
892
+ (join-fragments
893
+ (js-call nodes :map (lambda (node) (emit-expression node)))
894
+ (str " " operator " "))
895
+ ")")))
896
+
897
+ (defun emit-binary-infix (name nodes operator)
898
+ (require-arity name nodes 2 2)
899
+ (combine
900
+ (js-array "(" (emit-expression (js-nth 0 nodes)) " " operator " "
901
+ (emit-expression (js-nth 1 nodes)) ")")))
902
+
903
+ (defun temporary-names (nodes)
904
+ (let ((result (js-array)))
905
+ (js-call nodes :forEach
906
+ (lambda (_node) (js-call result :push (fresh-name))))
907
+ result))
908
+
909
+ (defun emit-comparison (name nodes operator)
910
+ (require-arity name nodes 2 nil)
911
+ (if (= (get nodes :length) 2)
912
+ (combine
913
+ (js-array "(" (emit-expression (js-nth 0 nodes)) " " operator " "
914
+ (emit-expression (js-nth 1 nodes)) ")"))
915
+ (let ((parameters (temporary-names nodes))
916
+ (comparisons (js-array))
917
+ (index 0))
918
+ (while (< index (1- (get parameters :length)))
919
+ (js-call comparisons :push
920
+ (str (js-nth index parameters) " " operator " "
921
+ (js-nth (1+ index) parameters)))
922
+ (setq index (1+ index)))
923
+ (combine
924
+ (js-array "(("
925
+ (js-call parameters :join ", ")
926
+ ") => ("
927
+ (js-call comparisons :join " && ")
928
+ "))("
929
+ (emit-arguments nodes)
930
+ ")")))))
931
+
932
+ (defun emit-distinct (name nodes)
933
+ (require-arity name nodes 2 nil)
934
+ (let ((parameters (temporary-names nodes))
935
+ (comparisons (js-array))
936
+ (left 0))
937
+ (while (< left (get parameters :length))
938
+ (let ((right (1+ left)))
939
+ (while (< right (get parameters :length))
940
+ (js-call comparisons :push
941
+ (str (js-nth left parameters) " !== " (js-nth right parameters)))
942
+ (setq right (1+ right))))
943
+ (setq left (1+ left)))
944
+ (combine
945
+ (js-array "(("
946
+ (js-call parameters :join ", ")
947
+ ") => ("
948
+ (js-call comparisons :join " && ")
949
+ "))("
950
+ (emit-arguments nodes)
951
+ ")"))))
952
+
953
+ (defun emit-intrinsic (node)
954
+ (let ((name (ir-value node))
955
+ (nodes (children node)))
956
+ (cond
957
+ ((= name "not")
958
+ (require-node-arity node 1 1)
959
+ (combine (js-array "(!__eliscript_truthy(" (emit-expression (js-nth 0 nodes)) "))")))
960
+ ((= name "+")
961
+ (if (= (get nodes :length) 0)
962
+ (text-fragment "0")
963
+ (combine
964
+ (js-array "("
965
+ (join-fragments
966
+ (js-call nodes :map (lambda (node) (emit-expression node)))
967
+ " + ")
968
+ ")"))))
969
+ ((= name "*")
970
+ (if (= (get nodes :length) 0)
971
+ (text-fragment "1")
972
+ (combine
973
+ (js-array "("
974
+ (join-fragments
975
+ (js-call nodes :map (lambda (node) (emit-expression node)))
976
+ " * ")
977
+ ")"))))
978
+ ((= name "-")
979
+ (require-node-arity node 1 nil)
980
+ (if (= (get nodes :length) 1)
981
+ (combine (js-array "(-" (emit-expression (js-nth 0 nodes)) ")"))
982
+ (emit-infix "-" nodes "-" 2)))
983
+ ((= name "/")
984
+ (require-node-arity node 1 nil)
985
+ (if (= (get nodes :length) 1)
986
+ (combine (js-array "(1 / " (emit-expression (js-nth 0 nodes)) ")"))
987
+ (emit-infix "/" nodes "/" 2)))
988
+ ((or (= name "%") (= name "mod")) (emit-infix name nodes "%" 2))
989
+ ((= name "int32")
990
+ (require-node-arity node 1 1)
991
+ (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " | 0)")))
992
+ ((= name "uint32")
993
+ (require-node-arity node 1 1)
994
+ (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " >>> 0)")))
995
+ ((= name "imul32")
996
+ (require-node-arity node 2 2)
997
+ (combine
998
+ (js-array "Math.imul(" (emit-expression (js-nth 0 nodes)) ", "
999
+ (emit-expression (js-nth 1 nodes)) ")")))
1000
+ ((= name "bit-and") (emit-binary-infix name nodes "&"))
1001
+ ((= name "bit-or") (emit-binary-infix name nodes "|"))
1002
+ ((= name "bit-xor") (emit-binary-infix name nodes "^"))
1003
+ ((= name "bit-not")
1004
+ (require-node-arity node 1 1)
1005
+ (combine (js-array "(~" (emit-expression (js-nth 0 nodes)) ")")))
1006
+ ((= name "bit-shift-left") (emit-binary-infix name nodes "<<"))
1007
+ ((= name "bit-shift-right") (emit-binary-infix name nodes ">>"))
1008
+ ((= name "unsigned-bit-shift-right")
1009
+ (emit-binary-infix name nodes ">>>"))
1010
+ ((= name "value-type")
1011
+ (require-node-arity node 1 1)
1012
+ (combine
1013
+ (js-array "((__eliscript_value) => { if (__eliscript_value === null) return \"null\"; const __eliscript_host_type = typeof __eliscript_value; if (__eliscript_host_type !== \"object\" && __eliscript_host_type !== \"function\") return __eliscript_host_type; try { const __eliscript_type = Object.getOwnPropertyDescriptor(__eliscript_value, Symbol.for(\"eliscript.value.type\")); if (__eliscript_type && Object.prototype.hasOwnProperty.call(__eliscript_type, \"value\") && (__eliscript_type.value === \"keyword\" || __eliscript_type.value === \"symbol\")) return __eliscript_type.value; const __eliscript_kind = Object.getOwnPropertyDescriptor(__eliscript_value, \"kind\"); if (__eliscript_kind && Object.prototype.hasOwnProperty.call(__eliscript_kind, \"value\")) { if (__eliscript_kind.value === \"eliscript/keyword\") return \"keyword\"; if (__eliscript_kind.value === \"eliscript/symbol\") return \"symbol\"; } return __eliscript_host_type; } catch { return __eliscript_host_type; } })("
1014
+ (emit-expression (js-nth 0 nodes)) ")")))
1015
+ ((= name "host-identity-token")
1016
+ (require-node-arity node 1 1)
1017
+ (combine
1018
+ (js-array "__eliscript_host_identity_token("
1019
+ (emit-expression (js-nth 0 nodes)) ")")))
1020
+ ((= name "string-code-unit-at")
1021
+ (require-node-arity node 2 2)
1022
+ (combine
1023
+ (js-array "(" (emit-expression (js-nth 0 nodes)) ").charCodeAt("
1024
+ (emit-expression (js-nth 1 nodes)) ")")))
1025
+ ((= name "string-from-code-unit")
1026
+ (require-node-arity node 1 1)
1027
+ (combine
1028
+ (js-array "String.fromCharCode(" (emit-expression (js-nth 0 nodes)) ")")))
1029
+ ((= name "string-to-number")
1030
+ (require-node-arity node 1 1)
1031
+ (combine (js-array "Number(" (emit-expression (js-nth 0 nodes)) ")")))
1032
+ ((= name "string-to-bigint")
1033
+ (require-node-arity node 1 1)
1034
+ (combine
1035
+ (js-array "((__eliscript_text) => { try { return BigInt(__eliscript_text); } catch { return null; } })("
1036
+ (emit-expression (js-nth 0 nodes)) ")")))
1037
+ ((= name "number-float64-words")
1038
+ (require-node-arity node 1 1)
1039
+ (combine
1040
+ (js-array "((__eliscript_number) => { const __eliscript_bytes = new DataView(new ArrayBuffer(8)); __eliscript_bytes.setFloat64(0, __eliscript_number === 0 ? 0 : __eliscript_number, true); return [__eliscript_bytes.getUint32(0, true), __eliscript_bytes.getUint32(4, true)]; })("
1041
+ (emit-expression (js-nth 0 nodes)) ")")))
1042
+ ((= name "=") (emit-comparison name nodes "==="))
1043
+ ((or (= name "/=") (= name "not=")) (emit-distinct name nodes))
1044
+ ((= name "<") (emit-comparison name nodes "<"))
1045
+ ((= name "<=") (emit-comparison name nodes "<="))
1046
+ ((= name ">") (emit-comparison name nodes ">"))
1047
+ ((= name ">=") (emit-comparison name nodes ">="))
1048
+ ((= name "1+")
1049
+ (require-node-arity node 1 1)
1050
+ (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " + 1)")))
1051
+ ((= name "1-")
1052
+ (require-node-arity node 1 1)
1053
+ (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " - 1)")))
1054
+ ((= name "eq")
1055
+ (require-node-arity node 2 2)
1056
+ (combine
1057
+ (js-array "(" (emit-expression (js-nth 0 nodes)) " === "
1058
+ (emit-expression (js-nth 1 nodes)) ")")))
1059
+ ((= name "equal")
1060
+ (require-node-arity node 2 2)
1061
+ (combine
1062
+ (js-array "__eliscript_equal(" (emit-expression (js-nth 0 nodes)) ", "
1063
+ (emit-expression (js-nth 1 nodes)) ")")))
1064
+ ((= name "nil?")
1065
+ (require-node-arity node 1 1)
1066
+ (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " === null)")))
1067
+ ((= name "undefined?")
1068
+ (require-node-arity node 1 1)
1069
+ (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " === undefined)")))
1070
+ ((or (= name "null") (= name "nullish?"))
1071
+ (require-node-arity node 1 1)
1072
+ (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " == null)")))
1073
+ ((= name "js-array")
1074
+ (combine (js-array "[" (emit-arguments nodes) "]")))
1075
+ ((= name "car")
1076
+ (require-node-arity node 1 1)
1077
+ (combine (js-array "__eliscript_first(" (emit-expression (js-nth 0 nodes)) ")")))
1078
+ ((= name "cdr")
1079
+ (require-node-arity node 1 1)
1080
+ (combine (js-array "__eliscript_rest(" (emit-expression (js-nth 0 nodes)) ")")))
1081
+ ((= name "cons")
1082
+ (require-node-arity node 2 2)
1083
+ (combine
1084
+ (js-array "__eliscript_cons(" (emit-expression (js-nth 0 nodes)) ", "
1085
+ (emit-expression (js-nth 1 nodes)) ")")))
1086
+ ((= name "js-cons")
1087
+ (require-node-arity node 2 2)
1088
+ (combine
1089
+ (js-array "[" (emit-expression (js-nth 0 nodes)) ", ...(("
1090
+ (emit-expression (js-nth 1 nodes)) ") ?? [])]")))
1091
+ ((= name "nth")
1092
+ (require-node-arity node 2 2)
1093
+ (combine
1094
+ (js-array "__eliscript_nth(" (emit-expression (js-nth 1 nodes)) ", "
1095
+ (emit-expression (js-nth 0 nodes)) ", null)")))
1096
+ ((= name "js-nth")
1097
+ (require-node-arity node 2 2)
1098
+ (combine
1099
+ (js-array "((((" (emit-expression (js-nth 1 nodes)) ") ?? [])["
1100
+ (emit-expression (js-nth 0 nodes)) "]) ?? null)")))
1101
+ ((= name "aref")
1102
+ (require-node-arity node 2 2)
1103
+ (combine
1104
+ (js-array "(" (emit-expression (js-nth 0 nodes)) ")["
1105
+ (emit-property-key (js-nth 1 nodes)) "]")))
1106
+ ((= name "length")
1107
+ (require-node-arity node 1 1)
1108
+ (combine (js-array "__eliscript_count(" (emit-expression (js-nth 0 nodes)) ")")))
1109
+ ((= name "js-length")
1110
+ (require-node-arity node 1 1)
1111
+ (combine (js-array "((" (emit-expression (js-nth 0 nodes)) ") ?? []).length")))
1112
+ ((= name "__eliscript_arity_error")
1113
+ (require-node-arity node 2 2)
1114
+ (combine
1115
+ (js-array
1116
+ "((label, arity) => { throw new TypeError(label + \" received unsupported arity: \" + arity); })("
1117
+ (emit-expression (js-nth 0 nodes)) ", "
1118
+ (emit-expression (js-nth 1 nodes)) ")")))
1119
+ ((= name "object-keys")
1120
+ (require-node-arity node 1 1)
1121
+ (combine (js-array "Object.keys((" (emit-expression (js-nth 0 nodes)) ") ?? {})")))
1122
+ ((= name "object-has?")
1123
+ (require-node-arity node 2 2)
1124
+ (combine
1125
+ (js-array "Object.prototype.hasOwnProperty.call(("
1126
+ (emit-expression (js-nth 0 nodes)) ") ?? {}, "
1127
+ (emit-property-key (js-nth 1 nodes)) ")")))
1128
+ ((= name "object-assoc")
1129
+ (require-node-arity node 3 3)
1130
+ (combine
1131
+ (js-array "({...((" (emit-expression (js-nth 0 nodes)) ") ?? {}), ["
1132
+ (emit-property-key (js-nth 1 nodes)) "]: "
1133
+ (emit-expression (js-nth 2 nodes)) "})")))
1134
+ (t (fail (str "unknown IR intrinsic: " name))))))
1135
+
1136
+ (defun emit-object-properties (properties)
1137
+ (let ((result (js-array)))
1138
+ (js-call properties :forEach
1139
+ (lambda (property)
1140
+ (let ((nodes (children property)))
1141
+ (js-call result :push
1142
+ (native-locate
1143
+ (ir-span property)
1144
+ (if (ir-property property "computed")
1145
+ (combine
1146
+ (js-array "[" (emit-expression (js-nth 0 nodes)) "]: "
1147
+ (emit-expression (js-nth 1 nodes))))
1148
+ (combine
1149
+ (js-array (json-string (ir-value property)) ": "
1150
+ (emit-expression (js-nth 0 nodes))))))))))
1151
+ (join-fragments result ", ")))
1152
+
1153
+ (defun emit-object (node)
1154
+ (combine (js-array "({" (emit-object-properties (children node)) "})")))
1155
+
1156
+ (defun literal-kind (node)
1157
+ (ir-property node "literalKind"))
1158
+
1159
+ (defun emit-literal (node)
1160
+ (let ((value (ir-value node))
1161
+ (kind (literal-kind node)))
1162
+ (cond
1163
+ ((= kind "undefined") (text-fragment "undefined"))
1164
+ ((= kind "keyword")
1165
+ (combine
1166
+ (js-array "__eliscript_keyword(" (json-string value) ")")))
1167
+ ((null value) (text-fragment "null"))
1168
+ ((eq value t) (text-fragment "true"))
1169
+ ((eq value false) (text-fragment "false"))
1170
+ ((= (js* "typeof value") "number")
1171
+ (unless (Number/isFinite value)
1172
+ (fail (str "non-finite numbers are not supported: " value)))
1173
+ (text-fragment (str value)))
1174
+ ((= (js* "typeof value") "string")
1175
+ (text-fragment (json-string value)))
1176
+ (t (fail (str "unsupported IR literal: " value))))))
1177
+
1178
+ (defun datum-kind (value)
1179
+ (and value
1180
+ (= (js* "typeof value") "object")
1181
+ (get value :kind)))
1182
+
1183
+ (defun emit-quoted (value)
1184
+ (let ((kind (datum-kind value)))
1185
+ (cond
1186
+ ((null value) (text-fragment "__eliscript_list()"))
1187
+ ((eq value t) (text-fragment "true"))
1188
+ ((eq value false)
1189
+ (text-fragment "__eliscript_symbol(\"false\")"))
1190
+ ((= (js* "typeof value") "number") (text-fragment (str value)))
1191
+ ((= (js* "typeof value") "string")
1192
+ (text-fragment (json-string value)))
1193
+ ((= kind "undefined") (text-fragment "undefined"))
1194
+ ((= kind "symbol")
1195
+ (combine
1196
+ (js-array "__eliscript_symbol("
1197
+ (text-fragment (json-string (get value :name))) ")")))
1198
+ ((= kind "keyword")
1199
+ (combine
1200
+ (js-array "__eliscript_keyword("
1201
+ (text-fragment (json-string (get value :name))) ")")))
1202
+ ((= kind "list")
1203
+ (combine
1204
+ (js-array "__eliscript_list("
1205
+ (join-fragments
1206
+ (js-call (get value :items) :map
1207
+ (lambda (item) (emit-quoted item)))
1208
+ ", ")
1209
+ ")")))
1210
+ ((= kind "vector")
1211
+ (combine
1212
+ (js-array "__eliscript_vector("
1213
+ (join-fragments
1214
+ (js-call (get value :items) :map
1215
+ (lambda (item) (emit-quoted item)))
1216
+ ", ")
1217
+ ")")))
1218
+ (t (fail "cannot emit quoted IR datum")))))
1219
+
1220
+ (defun quoted-uses-literal-runtime-p (value)
1221
+ (let ((kind (datum-kind value)))
1222
+ (or (null value)
1223
+ (= value false)
1224
+ (= kind "symbol")
1225
+ (= kind "keyword")
1226
+ (= kind "list")
1227
+ (= kind "vector"))))
1228
+
1229
+ (defun emit-property-key (node)
1230
+ (native-locate
1231
+ (ir-span node)
1232
+ (if (and (= (ir-kind node) "literal")
1233
+ (or (= (literal-kind node) "keyword")
1234
+ (= (js* "typeof node.value") "string")))
1235
+ (text-fragment (json-string (ir-value node)))
1236
+ (emit-expression node))))
1237
+
1238
+ (defun emit-expression (node)
1239
+ (unless (node-p node)
1240
+ (fail "expected an IR expression node"))
1241
+ (let ((kind (ir-kind node))
1242
+ (nodes (children node)))
1243
+ (native-locate
1244
+ (ir-span node)
1245
+ (cond
1246
+ ((= kind "literal") (emit-literal node))
1247
+ ((= kind "reference") (text-fragment (reference-name (ir-value node))))
1248
+ ((= kind "array-literal") (combine (js-array "[" (emit-arguments nodes) "]")))
1249
+ ((= kind "persistent-list-literal")
1250
+ (combine (js-array "__eliscript_list(" (emit-arguments nodes) ")")))
1251
+ ((= kind "persistent-vector-literal")
1252
+ (combine (js-array "__eliscript_vector(" (emit-arguments nodes) ")")))
1253
+ ((= kind "persistent-map-literal")
1254
+ (combine (js-array "__eliscript_hash_map(" (emit-arguments nodes) ")")))
1255
+ ((= kind "persistent-set-literal")
1256
+ (combine (js-array "__eliscript_hash_set(" (emit-arguments nodes) ")")))
1257
+ ((= kind "persistent-queue-literal")
1258
+ (combine (js-array "__eliscript_queue(" (emit-arguments nodes) ")")))
1259
+ ((= kind "quoted-literal") (emit-quoted (ir-value node)))
1260
+ ((= kind "function-expression") (emit-function node))
1261
+ ((= kind "await-expression")
1262
+ (combine (js-array "await (" (emit-expression (js-nth 0 nodes)) ")")))
1263
+ ((= kind "throw-expression") (emit-throw node))
1264
+ ((= kind "try-expression") (emit-try node))
1265
+ ((= kind "conditional") (emit-if nodes))
1266
+ ((= kind "conditional-sugar") (emit-conditional-sugar node))
1267
+ ((= kind "conditional-chain") (emit-cond nodes))
1268
+ ((= kind "sequence") (emit-do nodes))
1269
+ ((= kind "lexical-bindings") (emit-let node))
1270
+ ((= kind "assignment") (emit-assignment node))
1271
+ ((= kind "loop") (emit-loop node))
1272
+ ((= kind "binding-loop") (emit-binding-loop node))
1273
+ ((= kind "recur")
1274
+ (fail "recur is only valid in an analyzed tail position"))
1275
+ ((= kind "short-circuit") (emit-short-circuit nodes (ir-value node)))
1276
+ ((= kind "intrinsic") (emit-intrinsic node))
1277
+ ((= kind "object-literal") (emit-object node))
1278
+ ((= kind "property-read")
1279
+ (let ((access
1280
+ (combine
1281
+ (js-array "(" (emit-expression (js-nth 0 nodes)) ")["
1282
+ (emit-property-key (js-nth 1 nodes)) "]"))))
1283
+ (if (= (get nodes :length) 3)
1284
+ (combine (js-array "(" access " ?? " (emit-expression (js-nth 2 nodes)) ")"))
1285
+ access)))
1286
+ ((= kind "property-write")
1287
+ (combine
1288
+ (js-array "((" (emit-expression (js-nth 0 nodes)) ")["
1289
+ (emit-property-key (js-nth 1 nodes)) "] = "
1290
+ (emit-expression (js-nth 2 nodes)) ")")))
1291
+ ((= kind "method-call")
1292
+ (combine
1293
+ (js-array "(" (emit-expression (js-nth 0 nodes)) ")["
1294
+ (emit-property-key (js-nth 1 nodes)) "]("
1295
+ (emit-arguments (js-call nodes :slice 2)) ")")))
1296
+ ((= kind "constructor-call")
1297
+ (combine
1298
+ (js-array "new " (emit-expression (js-nth 0 nodes)) "("
1299
+ (emit-arguments (js-call nodes :slice 1)) ")")))
1300
+ ((= kind "raw-javascript")
1301
+ ;; The raw form is exactly one string. Accepting several arguments
1302
+ ;; emitted only the first, so the module's braces did not close and
1303
+ ;; the JavaScript was not what the source said. The reference
1304
+ ;; implementation reports this at emission, so this one does too.
1305
+ (require-arity "js*" nodes 1 1)
1306
+ (unless (= (ir-kind (js-nth 0 nodes)) "literal")
1307
+ (fail "js* expects a string literal"))
1308
+ (text-fragment (ir-value (js-nth 0 nodes))))
1309
+ ((= kind "print") (combine (js-array "console.log(" (emit-arguments nodes) ")")))
1310
+ ((= kind "string-concat")
1311
+ (if (= (get nodes :length) 0)
1312
+ (text-fragment "\"\"")
1313
+ (let ((parts (js-array)))
1314
+ (js-call nodes :forEach
1315
+ (lambda (child)
1316
+ (js-call parts :push
1317
+ (combine (js-array "String(" (emit-expression child) ")")))))
1318
+ (combine (js-array "(" (join-fragments parts " + ") ")")))))
1319
+ ((= kind "invoke")
1320
+ (combine
1321
+ (js-array (emit-callee (js-nth 0 nodes)) "("
1322
+ (emit-arguments (js-call nodes :slice 1)) ")")))
1323
+ ((= kind "apply-call")
1324
+ (combine
1325
+ (js-array (emit-callee (js-nth 0 nodes)) "(..."
1326
+ (emit-expression (js-nth 1 nodes)) ")")))
1327
+ ((= kind "call")
1328
+ (combine
1329
+ (js-array (emit-callee (js-nth 0 nodes)) "("
1330
+ (emit-arguments (js-call nodes :slice 1)) ")")))
1331
+ (t (fail (str "IR node is not valid in expression position: " kind)))))))
1332
+
1333
+ (defun emit-import (node)
1334
+ (let ((default-binding nil)
1335
+ (namespace-binding nil)
1336
+ (named-bindings (js-array)))
1337
+ (js-call (children node) :forEach
1338
+ (lambda (specifier)
1339
+ (let ((name (emit-binding-name specifier))
1340
+ (kind (ir-kind specifier)))
1341
+ (cond
1342
+ ((= kind "import-default") (setq default-binding name))
1343
+ ((= kind "import-namespace") (setq namespace-binding name))
1344
+ ((= kind "import-named") (js-call named-bindings :push name))
1345
+ (t (fail (str "invalid IR import specifier: " kind)))))))
1346
+ (when (and namespace-binding (> (get named-bindings :length) 0))
1347
+ (fail "namespace and named imports cannot be combined"))
1348
+ (let ((clauses (js-array)))
1349
+ (when default-binding (js-call clauses :push default-binding))
1350
+ (when namespace-binding
1351
+ (js-call clauses :push (combine (js-array "* as " namespace-binding))))
1352
+ (when (> (get named-bindings :length) 0)
1353
+ (js-call clauses :push
1354
+ (combine (js-array "{" (join-fragments named-bindings ", ") "}"))))
1355
+ (if (= (get clauses :length) 0)
1356
+ (text-fragment (str "import " (json-string (ir-value node)) ";"))
1357
+ (combine
1358
+ (js-array "import " (join-fragments clauses ", ") " from "
1359
+ (json-string (ir-value node)) ";"))))))
1360
+
1361
+ (defun emit-top-level (node)
1362
+ (unless (node-p node)
1363
+ (fail "expected a top-level IR node"))
1364
+ (let ((kind (ir-kind node))
1365
+ (nodes (children node)))
1366
+ (native-locate
1367
+ (ir-span node)
1368
+ (cond
1369
+ ((= kind "module-declaration")
1370
+ (join-fragments
1371
+ (js-call nodes :map (lambda (child) (emit-top-level child)))
1372
+ "\n"))
1373
+ ((= kind "import-declaration") (emit-import node))
1374
+ ((= kind "variable-declaration")
1375
+ (combine
1376
+ (js-array (if (ir-property node "mutable") "let " "const ")
1377
+ (binding-name (ir-value node))
1378
+ " = "
1379
+ (if (> (get nodes :length) 0)
1380
+ (emit-expression (js-nth 0 nodes))
1381
+ "null")
1382
+ ";")))
1383
+ ((= kind "function-declaration")
1384
+ (let* ((parts (split-counted-children node "parameterCount"))
1385
+ (parameters (js-nth 0 parts))
1386
+ (body (js-nth 1 parts)))
1387
+ (combine
1388
+ (js-array (if (ir-property node "async") "async " "")
1389
+ "function " (binding-name (ir-value node)) "("
1390
+ (join-fragments
1391
+ (js-call parameters :map
1392
+ (lambda (parameter) (emit-parameter parameter)))
1393
+ ", ")
1394
+ ") {\n"
1395
+ (indent (emit-function-body parameters body))
1396
+ "\n}"))))
1397
+ ((= kind "export-declaration")
1398
+ (combine
1399
+ (js-array "export {"
1400
+ (join-fragments
1401
+ (js-call nodes :map
1402
+ (lambda (reference) (emit-binding-name reference)))
1403
+ ", ")
1404
+ "};")))
1405
+ ((= kind "export-default")
1406
+ (combine (js-array "export default " (emit-expression (js-nth 0 nodes)) ";")))
1407
+ ((= kind "expression-statement")
1408
+ (combine (js-array (emit-expression (js-nth 0 nodes)) ";")))
1409
+ (t (fail (str "IR node is not valid at module top level: " kind)))))))
1410
+
1411
+ (defun node-uses-host-identity-token-p (node)
1412
+ (if (and (= (ir-kind node) "intrinsic")
1413
+ (= (ir-value node) "host-identity-token"))
1414
+ t
1415
+ (let ((found false))
1416
+ (js-call (children node) :forEach
1417
+ (lambda (child)
1418
+ (when (node-uses-host-identity-token-p child)
1419
+ (setq found t))))
1420
+ found)))
1421
+
1422
+ (defun node-directly-uses-literal-runtime-p (node)
1423
+ (let ((kind (ir-kind node)))
1424
+ (or (= kind "persistent-list-literal")
1425
+ (= kind "persistent-vector-literal")
1426
+ (= kind "persistent-map-literal")
1427
+ (= kind "persistent-set-literal")
1428
+ (= kind "persistent-queue-literal")
1429
+ (and (= kind "quoted-literal")
1430
+ (quoted-uses-literal-runtime-p (ir-value node)))
1431
+ (and (= kind "literal")
1432
+ (= (literal-kind node) "keyword")))))
1433
+
1434
+ (defun literal-runtime-static-key-index (node)
1435
+ (let ((kind (ir-kind node)))
1436
+ (cond
1437
+ ((or (= kind "property-read")
1438
+ (= kind "property-write")
1439
+ (= kind "method-call")) 1)
1440
+ ((and (= kind "intrinsic")
1441
+ (or (= (ir-value node) "aref")
1442
+ (= (ir-value node) "object-has?")
1443
+ (= (ir-value node) "object-assoc"))) 1)
1444
+ (t nil))))
1445
+
1446
+ (defun node-uses-literal-runtime-p (node)
1447
+ (let ((nodes (children node)))
1448
+ (cond
1449
+ ((node-directly-uses-literal-runtime-p node) t)
1450
+ (t
1451
+ (children-use-literal-runtime-p
1452
+ nodes (literal-runtime-static-key-index node))))))
1453
+
1454
+ (defun children-use-literal-runtime-p (nodes static-key-index)
1455
+ (let ((found false)
1456
+ (index 0))
1457
+ (while (< index (get nodes :length))
1458
+ (let ((child (js-nth index nodes)))
1459
+ (unless (and static-key-index
1460
+ (= index static-key-index)
1461
+ (= (ir-kind child) "literal")
1462
+ (= (literal-kind child) "keyword"))
1463
+ (when (node-uses-literal-runtime-p child)
1464
+ (setq found t))))
1465
+ (setq index (1+ index)))
1466
+ found))
1467
+
1468
+ (defun node-uses-collection-runtime-p (node)
1469
+ (if (and (= (ir-kind node) "intrinsic")
1470
+ (or (= (ir-value node) "nth") (= (ir-value node) "length")))
1471
+ t
1472
+ (let ((found false))
1473
+ (js-call (children node) :forEach
1474
+ (lambda (child)
1475
+ (when (node-uses-collection-runtime-p child)
1476
+ (setq found t))))
1477
+ found)))
1478
+
1479
+ (defun node-uses-binding-runtime-p (node)
1480
+ (if (= (ir-kind node) "map-binding-pattern")
1481
+ t
1482
+ (let ((found false))
1483
+ (js-call (children node) :forEach
1484
+ (lambda (child)
1485
+ (when (node-uses-binding-runtime-p child)
1486
+ (setq found t))))
1487
+ found)))
1488
+
1489
+ (defun node-uses-list-runtime-p (node)
1490
+ (if (and (= (ir-kind node) "intrinsic")
1491
+ (or (= (ir-value node) "car")
1492
+ (= (ir-value node) "cdr")
1493
+ (= (ir-value node) "cons")))
1494
+ t
1495
+ (let ((found false))
1496
+ (js-call (children node) :forEach
1497
+ (lambda (child)
1498
+ (when (node-uses-list-runtime-p child)
1499
+ (setq found t))))
1500
+ found)))
1501
+
1502
+ (defun node-uses-value-runtime-p (node)
1503
+ (if (and (= (ir-kind node) "intrinsic")
1504
+ (= (ir-value node) "equal"))
1505
+ t
1506
+ (let ((found false))
1507
+ (js-call (children node) :forEach
1508
+ (lambda (child)
1509
+ (when (node-uses-value-runtime-p child)
1510
+ (setq found t))))
1511
+ found)))
1512
+
1513
+ (defun program-uses-host-identity-token-p (program)
1514
+ (let ((found false))
1515
+ (js-call (program-body program) :forEach
1516
+ (lambda (node)
1517
+ (when (node-uses-host-identity-token-p node)
1518
+ (setq found t))))
1519
+ found))
1520
+
1521
+ (defun program-uses-literal-runtime-p (program)
1522
+ (let ((found false))
1523
+ (js-call (program-body program) :forEach
1524
+ (lambda (node)
1525
+ (when (node-uses-literal-runtime-p node)
1526
+ (setq found t))))
1527
+ found))
1528
+
1529
+ (defun program-uses-collection-runtime-p (program)
1530
+ (let ((found false))
1531
+ (js-call (program-body program) :forEach
1532
+ (lambda (node)
1533
+ (when (node-uses-collection-runtime-p node)
1534
+ (setq found t))))
1535
+ found))
1536
+
1537
+ (defun program-uses-binding-runtime-p (program)
1538
+ (let ((found false))
1539
+ (js-call (program-body program) :forEach
1540
+ (lambda (node)
1541
+ (when (node-uses-binding-runtime-p node)
1542
+ (setq found t))))
1543
+ found))
1544
+
1545
+ (defun program-uses-list-runtime-p (program)
1546
+ (let ((found false))
1547
+ (js-call (program-body program) :forEach
1548
+ (lambda (node)
1549
+ (when (node-uses-list-runtime-p node)
1550
+ (setq found t))))
1551
+ found))
1552
+
1553
+ (defun program-uses-value-runtime-p (program)
1554
+ (let ((found false))
1555
+ (js-call (program-body program) :forEach
1556
+ (lambda (node)
1557
+ (when (node-uses-value-runtime-p node)
1558
+ (setq found t))))
1559
+ found))
1560
+
1561
+ (defun reference-runtime-requirements (program)
1562
+ (js-object
1563
+ :hostIdentityToken (program-uses-host-identity-token-p program)
1564
+ :literal (program-uses-literal-runtime-p program)
1565
+ :collection (program-uses-collection-runtime-p program)
1566
+ :binding (program-uses-binding-runtime-p program)
1567
+ :list (program-uses-list-runtime-p program)
1568
+ :value (program-uses-value-runtime-p program)))
1569
+
1570
+ (defun static-literal-key-p (node index static-key-index)
1571
+ (and static-key-index
1572
+ (= index static-key-index)
1573
+ (= (ir-kind node) "literal")
1574
+ (= (literal-kind node) "keyword")))
1575
+
1576
+ (defun collect-runtime-requirements
1577
+ (node requirements suppress-literal)
1578
+ (let ((kind (ir-kind node))
1579
+ (value (ir-value node))
1580
+ (nodes (children node))
1581
+ (static-key-index (literal-runtime-static-key-index node))
1582
+ (index 0)
1583
+ (child nil))
1584
+ (cond
1585
+ ((= kind "map-binding-pattern")
1586
+ (put requirements :binding t))
1587
+ ((and (not suppress-literal)
1588
+ (or (= kind "persistent-list-literal")
1589
+ (= kind "persistent-vector-literal")
1590
+ (= kind "persistent-map-literal")
1591
+ (= kind "persistent-set-literal")
1592
+ (= kind "persistent-queue-literal")))
1593
+ (put requirements :literal t))
1594
+ ((and (not suppress-literal)
1595
+ (= kind "quoted-literal")
1596
+ (quoted-uses-literal-runtime-p value))
1597
+ (put requirements :literal t))
1598
+ ((and (not suppress-literal)
1599
+ (= kind "literal")
1600
+ (= (literal-kind node) "keyword"))
1601
+ (put requirements :literal t))
1602
+ ((= kind "intrinsic")
1603
+ (cond
1604
+ ((= value "host-identity-token")
1605
+ (put requirements :hostIdentityToken t))
1606
+ ((or (= value "nth") (= value "length"))
1607
+ (put requirements :collection t))
1608
+ ((or (= value "car") (= value "cdr") (= value "cons"))
1609
+ (put requirements :list t))
1610
+ ((= value "equal")
1611
+ (put requirements :value t)))))
1612
+ (while (< index (get nodes :length))
1613
+ (setq child (js-nth index nodes))
1614
+ (collect-runtime-requirements
1615
+ child
1616
+ requirements
1617
+ (or suppress-literal
1618
+ (static-literal-key-p child index static-key-index)))
1619
+ (setq index (1+ index))))
1620
+ requirements)
1621
+
1622
+ (defun runtime-requirements (program)
1623
+ (let ((requirements
1624
+ (js-object
1625
+ :hostIdentityToken false
1626
+ :literal false
1627
+ :collection false
1628
+ :binding false
1629
+ :list false
1630
+ :value false)))
1631
+ (js-call (program-body program) :forEach
1632
+ (lambda (node)
1633
+ (collect-runtime-requirements node requirements false)))
1634
+ requirements))
1635
+
1636
+ (defun collect-portable-metadata (node portable exported)
1637
+ (when (and (= (ir-kind node) "function-declaration")
1638
+ (ir-property node "portable"))
1639
+ (js-call portable :push node))
1640
+ (when (= (ir-kind node) "export-declaration")
1641
+ (js-call (children node) :forEach
1642
+ (lambda (reference)
1643
+ (js-call exported :push (ir-value reference)))))
1644
+ (js-call (children node) :forEach
1645
+ (lambda (child)
1646
+ (collect-portable-metadata child portable exported))))
1647
+
1648
+ (defun emit-portable-manifest (program)
1649
+ (let ((portable (js-array))
1650
+ (exported (js-array)))
1651
+ (js-call (program-body program) :forEach
1652
+ (lambda (node)
1653
+ (collect-portable-metadata node portable exported)))
1654
+ (if (= (get portable :length) 0)
1655
+ (text-fragment "")
1656
+ (let ((implicit
1657
+ (js-call portable :filter
1658
+ (lambda (node)
1659
+ (not (js-call exported :includes (ir-value node)))))))
1660
+ (combine
1661
+ (js-array (if (> (get implicit :length) 0)
1662
+ (combine
1663
+ (js-array "\n\nexport {"
1664
+ (join-fragments
1665
+ (js-call implicit :map
1666
+ (lambda (node) (binding-name (ir-value node))))
1667
+ ", ")
1668
+ "};"))
1669
+ "")
1670
+ "\nexport const __eliscript_portable__ = Object.freeze(Object.fromEntries(["
1671
+ (join-fragments
1672
+ (js-call portable :map
1673
+ (lambda (node)
1674
+ (combine
1675
+ (js-array "[" (json-string (ir-value node)) ", "
1676
+ (binding-name (ir-value node)) "]"))))
1677
+ ", ")
1678
+ "]));"))))))
1679
+
1680
+ (defun emit-module-artifact (program)
1681
+ (setq temporary-counter 0)
1682
+ (setq binding-temporaries (new globalThis.Map))
1683
+ (let ((requirements (runtime-requirements program))
1684
+ (body
1685
+ (join-fragments
1686
+ (js-call (program-body program) :map
1687
+ (lambda (node) (emit-top-level node)))
1688
+ "\n\n")))
1689
+ (combine
1690
+ (js-array "// Generated by Eliscript. Do not edit.\n"
1691
+ (if (get requirements :literal)
1692
+ literal-runtime-import
1693
+ "")
1694
+ (if (get requirements :collection)
1695
+ collection-runtime-import
1696
+ "")
1697
+ (if (get requirements :binding)
1698
+ binding-runtime
1699
+ "")
1700
+ (if (get requirements :list)
1701
+ list-runtime-import
1702
+ "")
1703
+ (if (get requirements :value)
1704
+ value-runtime-import
1705
+ "")
1706
+ "const __eliscript_truthy = (value) => value !== false && value != null;\n"
1707
+ (if (get requirements :hostIdentityToken)
1708
+ host-identity-token-helper
1709
+ "")
1710
+ "\n"
1711
+ body
1712
+ (emit-portable-manifest program)
1713
+ "\n"))))
1714
+
1715
+ (defun emit-module (program)
1716
+ (get (emit-module-artifact program) :text))
1717
+
1718
+ (defun emit-module-with-source-map
1719
+ (program source generated-name source-name)
1720
+ (let* ((artifact (emit-module-artifact program))
1721
+ (javascript (get artifact :text))
1722
+ (resolved-source-name
1723
+ (or source-name (program-filename program) "<string>")))
1724
+ (js-object
1725
+ :javascript javascript
1726
+ :sourceMap
1727
+ (create-source-map
1728
+ javascript
1729
+ (get artifact :marks)
1730
+ source
1731
+ resolved-source-name
1732
+ generated-name))))
1733
+
1734
+ (export
1735
+ indent
1736
+ reference-indent
1737
+ locate
1738
+ reference-locate
1739
+ emit-expression
1740
+ emit-top-level
1741
+ emit-module-artifact
1742
+ emit-module
1743
+ emit-module-with-source-map
1744
+ runtime-requirements
1745
+ reference-runtime-requirements))