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,1351 @@
1
+ ;;; eliscript-emitter.el --- ECMAScript emitter for Eliscript -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; The version-zero emitter compiles a deliberately small Lisp language to
6
+ ;; readable ECMAScript modules. It preserves Lisp truthiness: false, null, and
7
+ ;; undefined are false; every other JavaScript value is true.
8
+
9
+ ;;; Code:
10
+
11
+ (require 'cl-lib)
12
+ (require 'json)
13
+ (require 'subr-x)
14
+ (require 'eliscript-diagnostic)
15
+ (require 'eliscript-parameters)
16
+
17
+ (defconst eliscript-emitter--host-identity-token-helper
18
+ "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"
19
+ "Generated module helper for process-local opaque host identities.")
20
+ (defconst eliscript-emitter--literal-runtime-import
21
+ "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"
22
+ "Generated import for canonical language literal construction.")
23
+ (defconst eliscript-emitter--collection-runtime-import
24
+ "import { count as __eliscript_count, nth as __eliscript_nth } from \"eliscript/runtime/core/collection.mjs\";\n"
25
+ "Generated import for protocol-driven collection operations.")
26
+ (defconst eliscript-emitter--list-runtime-import
27
+ "import { cons as __eliscript_cons, first as __eliscript_first, rest as __eliscript_rest } from \"eliscript/runtime/core/list.mjs\";\n"
28
+ "Generated import for canonical persistent List operations.")
29
+ (defconst eliscript-emitter--value-runtime-import
30
+ "import { equalValues as __eliscript_equal } from \"eliscript/runtime/core/value.mjs\";\n"
31
+ "Generated import for canonical language value equality.")
32
+ (require 'eliscript-symbol)
33
+
34
+ (defalias 'eliscript-emitter--munge-segment
35
+ #'eliscript-symbol-munge-segment)
36
+ (defalias 'eliscript-emitter--binding-name
37
+ #'eliscript-symbol-binding-name)
38
+ (defalias 'eliscript-emitter--reference-name
39
+ #'eliscript-symbol-reference-name)
40
+
41
+ (defvar eliscript-emitter--temporary-counter 0)
42
+
43
+ (defun eliscript-emitter--fail (format-string &rest arguments)
44
+ "Signal a compiler error using FORMAT-STRING and ARGUMENTS."
45
+ (apply #'eliscript-diagnostic-signal
46
+ 'eliscript-compile-error "ELI-E0001" "emission"
47
+ nil nil format-string arguments))
48
+
49
+ (defun eliscript-emitter--json-string (value)
50
+ "Encode string VALUE as an ECMAScript string literal."
51
+ (let ((encoded (json-serialize value)))
52
+ ;; Emacs 30 returns UTF-8 JSON strings as unibyte data. Decode before
53
+ ;; composing them with multibyte emitter fragments so non-ASCII values do
54
+ ;; not turn into one replacement character per encoded byte.
55
+ (if (multibyte-string-p encoded)
56
+ encoded
57
+ (decode-coding-string encoded 'utf-8-unix))))
58
+
59
+ (defun eliscript-emitter--fresh-name ()
60
+ "Return a fresh internal ECMAScript identifier."
61
+ (setq eliscript-emitter--temporary-counter
62
+ (1+ eliscript-emitter--temporary-counter))
63
+ (format "__eliscript_value_%d" eliscript-emitter--temporary-counter))
64
+
65
+ (defun eliscript-emitter--indent (text &optional level)
66
+ "Indent every non-empty line in TEXT by LEVEL two-space units."
67
+ (let ((prefix (make-string (* 2 (or level 1)) ?\s)))
68
+ (mapconcat (lambda (line)
69
+ (if (string-empty-p line) line (concat prefix line)))
70
+ (split-string text "\n")
71
+ "\n")))
72
+
73
+ (defun eliscript-emitter--require-arity (name arguments minimum &optional maximum)
74
+ "Validate that NAME receives an allowed number of ARGUMENTS."
75
+ (let ((count (length arguments)))
76
+ (when (or (< count minimum)
77
+ (and maximum (> count maximum)))
78
+ (eliscript-emitter--fail
79
+ "%s expects %s argument%s, got %d"
80
+ name
81
+ (if (and maximum (= minimum maximum))
82
+ (number-to-string minimum)
83
+ (format "%d%s" minimum
84
+ (if maximum (format "..%d" maximum) "+")))
85
+ (if (and maximum (= minimum maximum) (= minimum 1)) "" "s")
86
+ count))))
87
+
88
+ (defun eliscript-emitter--emit-arguments (arguments)
89
+ "Emit comma-separated ARGUMENTS."
90
+ (mapconcat #'eliscript-emitter-emit-expression arguments ", "))
91
+
92
+ (defun eliscript-emitter--emit-callee (form)
93
+ "Emit FORM in a position where ECMAScript expects a callable value."
94
+ (let ((output (eliscript-emitter-emit-expression form)))
95
+ (if (symbolp form) output (format "(%s)" output))))
96
+
97
+ (defun eliscript-emitter--binding-pattern-p (form)
98
+ "Return non-nil when FORM is a vector binding pattern."
99
+ (vectorp (eliscript-form-value form)))
100
+
101
+ (defun eliscript-emitter--emit-binding-target (form)
102
+ "Emit symbol or vector binding FORM as JavaScript syntax."
103
+ (let ((value (eliscript-form-value form)))
104
+ (cond
105
+ ((and value (symbolp value))
106
+ (eliscript-emitter--binding-name value))
107
+ ((vectorp value)
108
+ (let (parts rest-next last-hole)
109
+ (dolist (element (append value nil))
110
+ (let ((element-value (eliscript-form-value element)))
111
+ (cond
112
+ ((eq element-value '&rest) (setq rest-next t))
113
+ (t
114
+ (setq last-hole (null element-value))
115
+ (push
116
+ (if rest-next
117
+ (concat "..."
118
+ (eliscript-emitter--emit-binding-target element))
119
+ (if last-hole
120
+ ""
121
+ (eliscript-emitter--emit-binding-target element)))
122
+ parts)
123
+ (setq rest-next nil)))))
124
+ (format "[%s%s]"
125
+ (string-join (nreverse parts) ", ")
126
+ (if last-hole "," ""))))
127
+ (t (eliscript-emitter--fail
128
+ "invalid binding pattern: %S" (eliscript-form-strip form))))))
129
+
130
+ (defun eliscript-emitter--emit-returning-body (forms)
131
+ "Emit FORMS as a block body that returns its final value."
132
+ (if (null forms)
133
+ "return null;"
134
+ (let ((initial (butlast forms))
135
+ (final (car (last forms))))
136
+ (mapconcat #'identity
137
+ (append
138
+ (mapcar (lambda (form)
139
+ (concat (eliscript-emitter-emit-expression form) ";"))
140
+ initial)
141
+ (list (concat "return "
142
+ (eliscript-emitter-emit-expression final)
143
+ ";")))
144
+ "\n"))))
145
+
146
+ (defun eliscript-emitter--emit-statement-body (forms)
147
+ "Emit FORMS as statements whose values are discarded."
148
+ (mapconcat
149
+ (lambda (form)
150
+ (concat (eliscript-emitter-emit-expression form) ";"))
151
+ forms
152
+ "\n"))
153
+
154
+ (defun eliscript-emitter--contains-await-p (form)
155
+ "Return non-nil when FORM contains `await' in the current function."
156
+ (cond
157
+ ((vectorp form)
158
+ (cl-some #'eliscript-emitter--contains-await-p (append form nil)))
159
+ ((consp form)
160
+ (let ((operator (car form)))
161
+ (cond
162
+ ((eq operator 'await) t)
163
+ ((memq operator '(lambda fn async)) nil)
164
+ (t (cl-some #'eliscript-emitter--contains-await-p form)))))
165
+ (t nil)))
166
+
167
+ (defun eliscript-emitter--emit-iife
168
+ (parameters body arguments asynchronous)
169
+ "Emit an IIFE with PARAMETERS, BODY, ARGUMENTS, and ASYNCHRONOUS mode."
170
+ (if asynchronous
171
+ (format "(await (async (%s) => {\n%s\n})(%s))"
172
+ parameters body arguments)
173
+ (format "((%s) => {\n%s\n})(%s)" parameters body arguments)))
174
+
175
+ (defun eliscript-emitter--emit-do (forms)
176
+ "Emit FORMS as one value-producing expression."
177
+ (pcase (length forms)
178
+ (0 "null")
179
+ (1 (eliscript-emitter-emit-expression (car forms)))
180
+ (_ (eliscript-emitter--emit-iife
181
+ ""
182
+ (eliscript-emitter--indent
183
+ (eliscript-emitter--emit-returning-body forms))
184
+ ""
185
+ (eliscript-emitter--contains-await-p forms)))))
186
+
187
+ (defun eliscript-emitter--parse-binding (binding)
188
+ "Return the name and initializer represented by BINDING."
189
+ (cond
190
+ ((or (symbolp binding) (vectorp binding)) (list binding nil))
191
+ ((and (listp binding) (<= 1 (length binding)) (<= (length binding) 2)
192
+ (or (symbolp (car binding)) (vectorp (car binding))))
193
+ (list (car binding) (cadr binding)))
194
+ (t (eliscript-emitter--fail "invalid let binding: %S" binding))))
195
+
196
+ (defun eliscript-emitter--emit-let (arguments sequential)
197
+ "Emit let ARGUMENTS, using sequential semantics when SEQUENTIAL is non-nil."
198
+ (eliscript-emitter--require-arity (if sequential "let*" "let") arguments 1)
199
+ (let ((bindings (car arguments))
200
+ (body (cdr arguments)))
201
+ (unless (listp bindings)
202
+ (eliscript-emitter--fail "let bindings must be a list"))
203
+ (if sequential
204
+ (if (null bindings)
205
+ (eliscript-emitter--emit-do body)
206
+ (eliscript-emitter--emit-let
207
+ (list (list (car bindings))
208
+ (cons 'let* (cons (cdr bindings) body)))
209
+ nil))
210
+ (let* ((parsed (mapcar #'eliscript-emitter--parse-binding bindings))
211
+ (names (mapcar (lambda (item)
212
+ (eliscript-emitter--emit-binding-target
213
+ (car item)))
214
+ parsed))
215
+ (values (mapcar (lambda (item)
216
+ (if (and (null (cadr item))
217
+ (eliscript-emitter--binding-pattern-p
218
+ (car item)))
219
+ "[]"
220
+ (eliscript-emitter-emit-expression
221
+ (cadr item))))
222
+ parsed)))
223
+ (eliscript-emitter--emit-iife
224
+ (string-join names ", ")
225
+ (eliscript-emitter--indent
226
+ (eliscript-emitter--emit-returning-body body))
227
+ (string-join values ", ")
228
+ (eliscript-emitter--contains-await-p body))))))
229
+
230
+ (defun eliscript-emitter--emit-function (arguments body &optional asynchronous)
231
+ "Emit a function with ARGUMENTS and BODY.
232
+
233
+ Prefix the function with `async' when ASYNCHRONOUS is non-nil."
234
+ (let ((parameters
235
+ (eliscript-parameters-parse
236
+ arguments
237
+ (lambda (_form message) (eliscript-emitter--fail "%s" message)))))
238
+ (format "%s(%s) => {\n%s\n}"
239
+ (if asynchronous "async " "")
240
+ (mapconcat #'eliscript-emitter--emit-parameter parameters ", ")
241
+ (eliscript-emitter--indent
242
+ (eliscript-emitter--emit-function-body parameters body)))))
243
+
244
+ (defun eliscript-emitter--contains-function-recur-p (form)
245
+ "Return non-nil when FORM recurs to its current function."
246
+ (cond
247
+ ((vectorp form)
248
+ (cl-some #'eliscript-emitter--contains-function-recur-p
249
+ (append form nil)))
250
+ ((consp form)
251
+ (let ((operator (car form)))
252
+ (cond
253
+ ((eq operator 'recur) t)
254
+ ((memq operator '(lambda fn async loop)) nil)
255
+ (t
256
+ (cl-some #'eliscript-emitter--contains-function-recur-p form)))))
257
+ (t nil)))
258
+
259
+ (defun eliscript-emitter--emit-rebinding-target (binding temporary)
260
+ "Assign TEMPORARY to mutable BINDING, including a vector pattern."
261
+ (let ((target (eliscript-emitter--emit-binding-target binding)))
262
+ (if (eliscript-emitter--binding-pattern-p binding)
263
+ (format "(%s = %s);" target temporary)
264
+ (format "%s = %s;" target temporary))))
265
+
266
+ (defun eliscript-emitter--emit-recur-tail (arguments target)
267
+ "Emit tail recur ARGUMENTS for TARGET."
268
+ (let ((bindings (plist-get target :bindings)))
269
+ (unless (= (length arguments) (length bindings))
270
+ (eliscript-emitter--fail
271
+ "recur expects %d arguments, got %d"
272
+ (length bindings) (length arguments)))
273
+ (let ((temporaries
274
+ (mapcar (lambda (_argument) (eliscript-emitter--fresh-name))
275
+ arguments))
276
+ parts)
277
+ (cl-mapc
278
+ (lambda (temporary argument)
279
+ (push (format "const %s = %s;"
280
+ temporary
281
+ (eliscript-emitter-emit-expression argument))
282
+ parts))
283
+ temporaries arguments)
284
+ (cl-mapc
285
+ (lambda (binding temporary)
286
+ (push (eliscript-emitter--emit-rebinding-target binding temporary)
287
+ parts))
288
+ bindings temporaries)
289
+ (push (format "continue %s;" (plist-get target :label)) parts)
290
+ (string-join (nreverse parts) "\n"))))
291
+
292
+ (defun eliscript-emitter--emit-tail-body (forms target)
293
+ "Emit FORMS so their final value returns or recurs to TARGET."
294
+ (if (null forms)
295
+ "return null;"
296
+ (string-join
297
+ (append
298
+ (mapcar
299
+ (lambda (form)
300
+ (concat (eliscript-emitter-emit-expression form) ";"))
301
+ (butlast forms))
302
+ (list (eliscript-emitter--emit-tail-form (car (last forms)) target)))
303
+ "\n")))
304
+
305
+ (defun eliscript-emitter--emit-tail-cond (clauses target)
306
+ "Emit conditional CLAUSES in recur-aware tail position for TARGET."
307
+ (if (null clauses)
308
+ "return null;"
309
+ (let ((clause (car clauses)))
310
+ (unless (and (listp clause) clause)
311
+ (eliscript-emitter--fail "cond clause must be a non-empty list"))
312
+ (let ((test (car clause))
313
+ (body (cdr clause)))
314
+ (if (eq test t)
315
+ (eliscript-emitter--emit-tail-body body target)
316
+ (format "if (__eliscript_truthy(%s)) {\n%s\n} else {\n%s\n}"
317
+ (eliscript-emitter-emit-expression test)
318
+ (eliscript-emitter--indent
319
+ (eliscript-emitter--emit-tail-body body target))
320
+ (eliscript-emitter--indent
321
+ (eliscript-emitter--emit-tail-cond
322
+ (cdr clauses) target))))))))
323
+
324
+ (defun eliscript-emitter--emit-tail-let (arguments sequential target)
325
+ "Emit lexical ARGUMENTS in tail position for TARGET."
326
+ (eliscript-emitter--require-arity (if sequential "let*" "let") arguments 1)
327
+ (let ((bindings (car arguments))
328
+ (body (cdr arguments))
329
+ parts)
330
+ (unless (listp bindings)
331
+ (eliscript-emitter--fail "let bindings must be a list"))
332
+ (let ((parsed (mapcar #'eliscript-emitter--parse-binding bindings)))
333
+ (if sequential
334
+ (dolist (binding parsed)
335
+ (let ((target-form (car binding))
336
+ (initializer (cadr binding)))
337
+ (push
338
+ (format "let %s = %s;"
339
+ (eliscript-emitter--emit-binding-target target-form)
340
+ (if initializer
341
+ (eliscript-emitter-emit-expression initializer)
342
+ (if (eliscript-emitter--binding-pattern-p target-form)
343
+ "[]" "null")))
344
+ parts)))
345
+ (let ((temporaries
346
+ (mapcar (lambda (_binding) (eliscript-emitter--fresh-name))
347
+ parsed)))
348
+ (cl-mapc
349
+ (lambda (binding temporary)
350
+ (let ((target-form (car binding))
351
+ (initializer (cadr binding)))
352
+ (push
353
+ (format "const %s = %s;"
354
+ temporary
355
+ (if initializer
356
+ (eliscript-emitter-emit-expression initializer)
357
+ (if (eliscript-emitter--binding-pattern-p target-form)
358
+ "[]" "null")))
359
+ parts)))
360
+ parsed temporaries)
361
+ (cl-mapc
362
+ (lambda (binding temporary)
363
+ (push
364
+ (format "let %s = %s;"
365
+ (eliscript-emitter--emit-binding-target (car binding))
366
+ temporary)
367
+ parts))
368
+ parsed temporaries)))
369
+ (push (eliscript-emitter--emit-tail-body body target) parts)
370
+ (format "{\n%s\n}"
371
+ (eliscript-emitter--indent
372
+ (string-join (nreverse parts) "\n"))))))
373
+
374
+ (defun eliscript-emitter--emit-tail-short-circuit (arguments kind target)
375
+ "Emit recur-aware short-circuit ARGUMENTS of KIND for TARGET."
376
+ (cond
377
+ ((null arguments)
378
+ (format "return %s;" (if (eq kind 'and) "true" "null")))
379
+ ((null (cdr arguments))
380
+ (eliscript-emitter--emit-tail-form (car arguments) target))
381
+ (t
382
+ (let ((temporary (eliscript-emitter--fresh-name)))
383
+ (format
384
+ "const %s = %s;\nif (__eliscript_truthy(%s)) {\n%s\n} else {\n%s\n}"
385
+ temporary
386
+ (eliscript-emitter-emit-expression (car arguments))
387
+ temporary
388
+ (eliscript-emitter--indent
389
+ (if (eq kind 'and)
390
+ (eliscript-emitter--emit-tail-short-circuit
391
+ (cdr arguments) kind target)
392
+ (format "return %s;" temporary)))
393
+ (eliscript-emitter--indent
394
+ (if (eq kind 'and)
395
+ (format "return %s;" temporary)
396
+ (eliscript-emitter--emit-tail-short-circuit
397
+ (cdr arguments) kind target))))))))
398
+
399
+ (defun eliscript-emitter--emit-tail-form (form target)
400
+ "Emit FORM in a tail position controlled by TARGET."
401
+ (if (not (consp form))
402
+ (format "return %s;" (eliscript-emitter-emit-expression form))
403
+ (let ((operator (car form))
404
+ (arguments (cdr form)))
405
+ (pcase operator
406
+ ('recur (eliscript-emitter--emit-recur-tail arguments target))
407
+ ('if
408
+ (eliscript-emitter--require-arity "if" arguments 2 3)
409
+ (format "if (__eliscript_truthy(%s)) {\n%s\n} else {\n%s\n}"
410
+ (eliscript-emitter-emit-expression (nth 0 arguments))
411
+ (eliscript-emitter--indent
412
+ (eliscript-emitter--emit-tail-form
413
+ (nth 1 arguments) target))
414
+ (eliscript-emitter--indent
415
+ (if (nth 2 arguments)
416
+ (eliscript-emitter--emit-tail-form
417
+ (nth 2 arguments) target)
418
+ "return null;"))))
419
+ ((or 'when 'unless)
420
+ (eliscript-emitter--require-arity (symbol-name operator) arguments 1)
421
+ (format "if (%s__eliscript_truthy(%s)) {\n%s\n} else {\n return null;\n}"
422
+ (if (eq operator 'when) "" "!")
423
+ (eliscript-emitter-emit-expression (car arguments))
424
+ (eliscript-emitter--indent
425
+ (eliscript-emitter--emit-tail-body
426
+ (cdr arguments) target))))
427
+ ('cond (eliscript-emitter--emit-tail-cond arguments target))
428
+ ((or 'progn 'do)
429
+ (eliscript-emitter--emit-tail-body arguments target))
430
+ ('let (eliscript-emitter--emit-tail-let arguments nil target))
431
+ ('let* (eliscript-emitter--emit-tail-let arguments t target))
432
+ ('and
433
+ (eliscript-emitter--emit-tail-short-circuit arguments 'and target))
434
+ ('or
435
+ (eliscript-emitter--emit-tail-short-circuit arguments 'or target))
436
+ (_ (format "return %s;"
437
+ (eliscript-emitter-emit-expression form)))))))
438
+
439
+ (defun eliscript-emitter--emit-function-body (parameters body)
440
+ "Emit function BODY with recur-aware PARAMETER rebinding when required."
441
+ (if (not (eliscript-emitter--contains-function-recur-p body))
442
+ (eliscript-emitter--emit-returning-body body)
443
+ (let* ((label (eliscript-emitter--fresh-name))
444
+ (target
445
+ (list :bindings
446
+ (mapcar #'eliscript-parameter-form parameters)
447
+ :label label)))
448
+ (format "%s: while (true) {\n%s\n}"
449
+ label
450
+ (eliscript-emitter--indent
451
+ (eliscript-emitter--emit-tail-body body target))))))
452
+
453
+ (defun eliscript-emitter--emit-binding-loop (arguments)
454
+ "Emit recur-capable lexical binding loop ARGUMENTS."
455
+ (eliscript-emitter--require-arity "loop" arguments 1)
456
+ (let ((bindings (car arguments))
457
+ (body (cdr arguments)))
458
+ (unless (listp bindings)
459
+ (eliscript-emitter--fail "loop bindings must be a list"))
460
+ (let (targets initializers)
461
+ (dolist (binding bindings)
462
+ (unless (and (listp binding) (= (length binding) 2)
463
+ (or (symbolp (car binding)) (vectorp (car binding))))
464
+ (eliscript-emitter--fail
465
+ "loop binding must contain a target and initializer: %S" binding))
466
+ (push (car binding) targets)
467
+ (push (cadr binding) initializers))
468
+ (setq targets (nreverse targets)
469
+ initializers (nreverse initializers))
470
+ (let* ((label (eliscript-emitter--fresh-name))
471
+ (target (list :bindings targets :label label)))
472
+ (eliscript-emitter--emit-iife
473
+ (mapconcat #'eliscript-emitter--emit-binding-target targets ", ")
474
+ (eliscript-emitter--indent
475
+ (format "%s: while (true) {\n%s\n}"
476
+ label
477
+ (eliscript-emitter--indent
478
+ (eliscript-emitter--emit-tail-body body target))))
479
+ (mapconcat #'eliscript-emitter-emit-expression initializers ", ")
480
+ (eliscript-emitter--contains-await-p body))))))
481
+
482
+ (defun eliscript-emitter--emit-parameter (parameter)
483
+ "Emit parsed function PARAMETER."
484
+ (let* ((form (eliscript-parameter-form parameter))
485
+ (name (eliscript-emitter--emit-binding-target form))
486
+ (pattern (eliscript-emitter--binding-pattern-p form)))
487
+ (pcase (eliscript-parameter-kind parameter)
488
+ ('optional (format "%s = %s" name (if pattern "[]" "null")))
489
+ ('rest (format "...%s" name))
490
+ (_ name))))
491
+
492
+ (defun eliscript-emitter--emit-if (arguments)
493
+ "Emit an if expression from ARGUMENTS."
494
+ (eliscript-emitter--require-arity "if" arguments 2 3)
495
+ (format "(__eliscript_truthy(%s) ? %s : %s)"
496
+ (eliscript-emitter-emit-expression (nth 0 arguments))
497
+ (eliscript-emitter-emit-expression (nth 1 arguments))
498
+ (eliscript-emitter-emit-expression (nth 2 arguments))))
499
+
500
+ (defun eliscript-emitter--emit-cond (clauses)
501
+ "Emit nested conditional expressions for CLAUSES."
502
+ (if (null clauses)
503
+ "null"
504
+ (let ((clause (car clauses)))
505
+ (unless (and (listp clause) clause)
506
+ (eliscript-emitter--fail "invalid cond clause: %S" clause))
507
+ (let ((test (car clause))
508
+ (body (cdr clause)))
509
+ (if (eq test t)
510
+ (eliscript-emitter--emit-do body)
511
+ (format "(__eliscript_truthy(%s) ? %s : %s)"
512
+ (eliscript-emitter-emit-expression test)
513
+ (eliscript-emitter--emit-do body)
514
+ (eliscript-emitter--emit-cond (cdr clauses))))))))
515
+
516
+ (defun eliscript-emitter--try-clause-operator (form)
517
+ "Return FORM's try clause operator, or nil."
518
+ (and (proper-list-p form)
519
+ form
520
+ (symbolp (car form))
521
+ (car form)))
522
+
523
+ (defun eliscript-emitter--emit-throw (arguments)
524
+ "Emit a throw expression from ARGUMENTS."
525
+ (eliscript-emitter--require-arity "throw" arguments 1 1)
526
+ (let ((temporary (eliscript-emitter--fresh-name)))
527
+ (format "((%s) => { throw %s; })(%s)"
528
+ temporary temporary
529
+ (eliscript-emitter-emit-expression (car arguments)))))
530
+
531
+ (defun eliscript-emitter--emit-try (arguments)
532
+ "Emit value-producing try ARGUMENTS."
533
+ (let (body catch-name catch-body finally-body
534
+ clauses-started catch-seen finally-seen)
535
+ (dolist (argument arguments)
536
+ (pcase (eliscript-emitter--try-clause-operator argument)
537
+ ('catch
538
+ (when catch-seen
539
+ (eliscript-emitter--fail
540
+ "try accepts at most one catch clause"))
541
+ (when finally-seen
542
+ (eliscript-emitter--fail "catch must precede finally"))
543
+ (unless (and (cdr argument)
544
+ (let ((binding (cadr argument)))
545
+ (or (and binding (symbolp binding))
546
+ (vectorp binding))))
547
+ (eliscript-emitter--fail
548
+ "catch requires a binding symbol or vector"))
549
+ (setq clauses-started t
550
+ catch-seen t
551
+ catch-name (cadr argument)
552
+ catch-body (cddr argument)))
553
+ ('finally
554
+ (when finally-seen
555
+ (eliscript-emitter--fail
556
+ "try accepts at most one finally clause"))
557
+ (setq clauses-started t
558
+ finally-seen t
559
+ finally-body (cdr argument)))
560
+ (_
561
+ (if clauses-started
562
+ (eliscript-emitter--fail
563
+ "try body forms must precede catch and finally clauses")
564
+ (setq body (append body (list argument)))))))
565
+ (unless (or catch-seen finally-seen)
566
+ (eliscript-emitter--fail "try requires a catch or finally clause"))
567
+ (let ((try-block
568
+ (concat
569
+ "try {\n"
570
+ (eliscript-emitter--indent
571
+ (eliscript-emitter--emit-returning-body body))
572
+ "\n}"
573
+ (if catch-seen
574
+ (concat
575
+ " catch ("
576
+ (eliscript-emitter--emit-binding-target catch-name)
577
+ ") {\n"
578
+ (eliscript-emitter--indent
579
+ (eliscript-emitter--emit-returning-body catch-body))
580
+ "\n}")
581
+ "")
582
+ (if finally-seen
583
+ (concat
584
+ " finally {\n"
585
+ (eliscript-emitter--indent
586
+ (eliscript-emitter--emit-statement-body finally-body))
587
+ "\n}")
588
+ ""))))
589
+ (eliscript-emitter--emit-iife
590
+ "" (eliscript-emitter--indent try-block) ""
591
+ (eliscript-emitter--contains-await-p arguments)))))
592
+
593
+ (defun eliscript-emitter--emit-while (arguments)
594
+ "Emit a value-producing while expression from ARGUMENTS."
595
+ (eliscript-emitter--require-arity "while" arguments 1)
596
+ (let ((test (car arguments))
597
+ (body (cdr arguments)))
598
+ (eliscript-emitter--emit-iife
599
+ ""
600
+ (eliscript-emitter--indent
601
+ (concat
602
+ (format "while (__eliscript_truthy(%s)) {\n"
603
+ (eliscript-emitter-emit-expression test))
604
+ (eliscript-emitter--indent
605
+ (mapconcat (lambda (form)
606
+ (concat (eliscript-emitter-emit-expression form) ";"))
607
+ body "\n"))
608
+ "\n}\nreturn null;"))
609
+ ""
610
+ (eliscript-emitter--contains-await-p arguments))))
611
+
612
+ (defun eliscript-emitter--emit-setq (arguments)
613
+ "Emit assignments represented by setq ARGUMENTS."
614
+ (when (or (null arguments) (= (% (length arguments) 2) 1))
615
+ (eliscript-emitter--fail "setq expects one or more name/value pairs"))
616
+ (let (assignments)
617
+ (while arguments
618
+ (let ((name (pop arguments))
619
+ (value (pop arguments)))
620
+ (push (format "(%s = %s)"
621
+ (eliscript-emitter--binding-name name)
622
+ (eliscript-emitter-emit-expression value))
623
+ assignments)))
624
+ (format "(%s)" (string-join (nreverse assignments) ", "))))
625
+
626
+ (defun eliscript-emitter--emit-short-circuit (arguments kind)
627
+ "Emit Lisp-style short-circuit ARGUMENTS for KIND, either and or or."
628
+ (cond
629
+ ((null arguments) (if (eq kind 'and) "true" "null"))
630
+ ((null (cdr arguments))
631
+ (eliscript-emitter-emit-expression (car arguments)))
632
+ (t
633
+ (let ((temporary (eliscript-emitter--fresh-name))
634
+ (first (eliscript-emitter-emit-expression (car arguments)))
635
+ (rest (eliscript-emitter--emit-short-circuit (cdr arguments) kind)))
636
+ (if (eliscript-emitter--contains-await-p (cdr arguments))
637
+ (format
638
+ "(await (async (%s) => (__eliscript_truthy(%s) ? %s : %s))(%s))"
639
+ temporary temporary
640
+ (if (eq kind 'and) rest temporary)
641
+ (if (eq kind 'and) temporary rest)
642
+ first)
643
+ (format "((%s) => (__eliscript_truthy(%s) ? %s : %s))(%s)"
644
+ temporary temporary
645
+ (if (eq kind 'and) rest temporary)
646
+ (if (eq kind 'and) temporary rest)
647
+ first))))))
648
+
649
+ (defun eliscript-emitter--emit-infix (name arguments operator &optional minimum)
650
+ "Emit ARGUMENTS joined by OPERATOR for Lisp function NAME."
651
+ (eliscript-emitter--require-arity name arguments (or minimum 2))
652
+ (format "(%s)"
653
+ (mapconcat #'eliscript-emitter-emit-expression
654
+ arguments
655
+ (format " %s " operator))))
656
+
657
+ (defun eliscript-emitter--emit-binary-infix (name arguments operator)
658
+ "Emit exact binary NAME over ARGUMENTS using JavaScript OPERATOR."
659
+ (eliscript-emitter--require-arity name arguments 2 2)
660
+ (format "(%s %s %s)"
661
+ (eliscript-emitter-emit-expression (nth 0 arguments))
662
+ operator
663
+ (eliscript-emitter-emit-expression (nth 1 arguments))))
664
+
665
+ (defun eliscript-emitter--emit-comparison (name arguments operator)
666
+ "Emit one-evaluation n-ary comparison NAME using OPERATOR."
667
+ (eliscript-emitter--require-arity name arguments 2)
668
+ (if (= (length arguments) 2)
669
+ (format "(%s %s %s)"
670
+ (eliscript-emitter-emit-expression (nth 0 arguments))
671
+ operator
672
+ (eliscript-emitter-emit-expression (nth 1 arguments)))
673
+ (let ((parameters (mapcar (lambda (_argument)
674
+ (eliscript-emitter--fresh-name))
675
+ arguments))
676
+ comparisons)
677
+ (cl-loop for left on parameters
678
+ while (cdr left)
679
+ do (push (format "%s %s %s" (car left) operator (cadr left))
680
+ comparisons))
681
+ (format "((%s) => (%s))(%s)"
682
+ (string-join parameters ", ")
683
+ (string-join (nreverse comparisons) " && ")
684
+ (eliscript-emitter--emit-arguments arguments)))))
685
+
686
+ (defun eliscript-emitter--emit-distinct (name arguments)
687
+ "Emit a one-evaluation pairwise distinct comparison for NAME ARGUMENTS."
688
+ (eliscript-emitter--require-arity name arguments 2)
689
+ (let ((parameters (mapcar (lambda (_argument)
690
+ (eliscript-emitter--fresh-name))
691
+ arguments))
692
+ comparisons)
693
+ (cl-loop for left-index from 0 below (length parameters)
694
+ do (cl-loop for right-index from (1+ left-index)
695
+ below (length parameters)
696
+ do (push
697
+ (format "%s !== %s"
698
+ (nth left-index parameters)
699
+ (nth right-index parameters))
700
+ comparisons)))
701
+ (format "((%s) => (%s))(%s)"
702
+ (string-join parameters ", ")
703
+ (string-join (nreverse comparisons) " && ")
704
+ (eliscript-emitter--emit-arguments arguments))))
705
+
706
+ (defun eliscript-emitter--emit-object (arguments)
707
+ "Emit an object literal from alternating key/value ARGUMENTS."
708
+ (when (= (% (length arguments) 2) 1)
709
+ (eliscript-emitter--fail "object expects key/value pairs"))
710
+ (let (properties)
711
+ (while arguments
712
+ (let ((key (pop arguments))
713
+ (value (pop arguments)))
714
+ (push
715
+ (format "%s: %s"
716
+ (cond
717
+ ((keywordp key)
718
+ (eliscript-emitter--json-string
719
+ (substring (symbol-name key) 1)))
720
+ ((stringp key) (eliscript-emitter--json-string key))
721
+ ((symbolp key)
722
+ (eliscript-emitter--json-string (symbol-name key)))
723
+ (t (format "[%s]"
724
+ (eliscript-emitter-emit-expression key))))
725
+ (eliscript-emitter-emit-expression value))
726
+ properties)))
727
+ (format "({%s})" (string-join (nreverse properties) ", "))))
728
+
729
+ (defun eliscript-emitter--emit-property-key (key)
730
+ "Emit KEY for bracket-style JavaScript property access."
731
+ (cond
732
+ ((keywordp key)
733
+ (eliscript-emitter--json-string (substring (symbol-name key) 1)))
734
+ ((stringp key) (eliscript-emitter--json-string key))
735
+ (t (eliscript-emitter-emit-expression key))))
736
+
737
+ (defun eliscript-emitter--emit-get (arguments)
738
+ "Emit property access from get ARGUMENTS."
739
+ (eliscript-emitter--require-arity "get" arguments 2 3)
740
+ (let ((access (format "(%s)[%s]"
741
+ (eliscript-emitter-emit-expression (nth 0 arguments))
742
+ (eliscript-emitter--emit-property-key (nth 1 arguments)))))
743
+ (if (= (length arguments) 3)
744
+ (format "(%s ?? %s)"
745
+ access
746
+ (eliscript-emitter-emit-expression (nth 2 arguments)))
747
+ access)))
748
+
749
+ (defun eliscript-emitter--emit-js-call (arguments)
750
+ "Emit a receiver-preserving JavaScript method call from ARGUMENTS."
751
+ (eliscript-emitter--require-arity "js-call" arguments 2)
752
+ (format "(%s)[%s](%s)"
753
+ (eliscript-emitter-emit-expression (nth 0 arguments))
754
+ (eliscript-emitter--emit-property-key (nth 1 arguments))
755
+ (eliscript-emitter--emit-arguments (nthcdr 2 arguments))))
756
+
757
+ (defun eliscript-emitter--emit-quoted (value)
758
+ "Emit quoted Eliscript VALUE as canonical persistent data."
759
+ (cond
760
+ ((null value) "__eliscript_list()")
761
+ ((eq value t) "true")
762
+ ((eq value 'undefined) "undefined")
763
+ ((numberp value) (number-to-string value))
764
+ ((stringp value) (eliscript-emitter--json-string value))
765
+ ((keywordp value)
766
+ (format "__eliscript_keyword(%s)"
767
+ (eliscript-emitter--json-string
768
+ (substring (symbol-name value) 1))))
769
+ ((symbolp value)
770
+ (format "__eliscript_symbol(%s)"
771
+ (eliscript-emitter--json-string (symbol-name value))))
772
+ ((vectorp value)
773
+ (format "__eliscript_vector(%s)"
774
+ (mapconcat #'eliscript-emitter--emit-quoted (append value nil) ", ")))
775
+ ((consp value)
776
+ (unless (proper-list-p value)
777
+ (eliscript-emitter--fail "dotted quoted lists are not supported yet"))
778
+ (format "__eliscript_list(%s)"
779
+ (mapconcat #'eliscript-emitter--emit-quoted value ", ")))
780
+ (t (eliscript-emitter--fail "cannot quote value: %S" value))))
781
+
782
+ (defun eliscript-emitter--quoted-uses-literal-runtime-p (value)
783
+ "Return non-nil when quoted VALUE constructs a persistent runtime value."
784
+ (cond
785
+ ((null value) t)
786
+ ((or (eq value t) (eq value 'undefined)
787
+ (numberp value) (stringp value)) nil)
788
+ ((or (keywordp value) (symbolp value)
789
+ (vectorp value) (consp value)) t)
790
+ (t nil)))
791
+
792
+ (defun eliscript-emitter--emit-call (form)
793
+ "Emit list FORM as an expression."
794
+ (let ((operator (car form))
795
+ (arguments (cdr form)))
796
+ (pcase operator
797
+ ((or 'lambda 'fn)
798
+ (eliscript-emitter--require-arity (symbol-name operator) arguments 1)
799
+ (eliscript-emitter--emit-function (car arguments) (cdr arguments)))
800
+ ('async
801
+ (eliscript-emitter--require-arity "async" arguments 1)
802
+ (eliscript-emitter--emit-function
803
+ (car arguments) (cdr arguments) t))
804
+ ('await
805
+ (eliscript-emitter--require-arity "await" arguments 1 1)
806
+ (format "await (%s)"
807
+ (eliscript-emitter-emit-expression (car arguments))))
808
+ ('throw (eliscript-emitter--emit-throw arguments))
809
+ ('try (eliscript-emitter--emit-try arguments))
810
+ ((or 'catch 'finally)
811
+ (eliscript-emitter--fail
812
+ "%s is only valid as a try clause" operator))
813
+ ('if (eliscript-emitter--emit-if arguments))
814
+ ('when
815
+ (eliscript-emitter--require-arity "when" arguments 1)
816
+ (eliscript-emitter--emit-if
817
+ (list (car arguments) (cons 'progn (cdr arguments)) nil)))
818
+ ('unless
819
+ (eliscript-emitter--require-arity "unless" arguments 1)
820
+ (eliscript-emitter--emit-if
821
+ (list (list 'not (car arguments)) (cons 'progn (cdr arguments)) nil)))
822
+ ('cond (eliscript-emitter--emit-cond arguments))
823
+ ((or 'progn 'do) (eliscript-emitter--emit-do arguments))
824
+ ('let (eliscript-emitter--emit-let arguments nil))
825
+ ('let* (eliscript-emitter--emit-let arguments t))
826
+ ('setq (eliscript-emitter--emit-setq arguments))
827
+ ('set!
828
+ (eliscript-emitter--require-arity "set!" arguments 2 2)
829
+ (format "(%s = %s)"
830
+ (eliscript-emitter--binding-name (car arguments))
831
+ (eliscript-emitter-emit-expression (cadr arguments))))
832
+ ('while (eliscript-emitter--emit-while arguments))
833
+ ('loop (eliscript-emitter--emit-binding-loop arguments))
834
+ ('recur
835
+ (eliscript-emitter--fail "recur is only valid in tail position"))
836
+ ('and (eliscript-emitter--emit-short-circuit arguments 'and))
837
+ ('or (eliscript-emitter--emit-short-circuit arguments 'or))
838
+ ('not
839
+ (eliscript-emitter--require-arity "not" arguments 1 1)
840
+ (format "(!__eliscript_truthy(%s))"
841
+ (eliscript-emitter-emit-expression (car arguments))))
842
+ ('quote
843
+ (eliscript-emitter--require-arity "quote" arguments 1 1)
844
+ (eliscript-emitter--emit-quoted (car arguments)))
845
+ ('+ (if arguments
846
+ (format "(%s)" (mapconcat #'eliscript-emitter-emit-expression
847
+ arguments " + "))
848
+ "0"))
849
+ ('* (if arguments
850
+ (format "(%s)" (mapconcat #'eliscript-emitter-emit-expression
851
+ arguments " * "))
852
+ "1"))
853
+ ('-
854
+ (eliscript-emitter--require-arity "-" arguments 1)
855
+ (if (= (length arguments) 1)
856
+ (format "(-%s)" (eliscript-emitter-emit-expression (car arguments)))
857
+ (eliscript-emitter--emit-infix "-" arguments "-")))
858
+ ('/
859
+ (eliscript-emitter--require-arity "/" arguments 1)
860
+ (if (= (length arguments) 1)
861
+ (format "(1 / %s)" (eliscript-emitter-emit-expression (car arguments)))
862
+ (eliscript-emitter--emit-infix "/" arguments "/")))
863
+ ((or '% 'mod) (eliscript-emitter--emit-infix (symbol-name operator)
864
+ arguments "%"))
865
+ ('int32
866
+ (eliscript-emitter--require-arity "int32" arguments 1 1)
867
+ (format "(%s | 0)"
868
+ (eliscript-emitter-emit-expression (car arguments))))
869
+ ('uint32
870
+ (eliscript-emitter--require-arity "uint32" arguments 1 1)
871
+ (format "(%s >>> 0)"
872
+ (eliscript-emitter-emit-expression (car arguments))))
873
+ ('imul32
874
+ (eliscript-emitter--require-arity "imul32" arguments 2 2)
875
+ (format "Math.imul(%s, %s)"
876
+ (eliscript-emitter-emit-expression (nth 0 arguments))
877
+ (eliscript-emitter-emit-expression (nth 1 arguments))))
878
+ ('bit-and
879
+ (eliscript-emitter--emit-binary-infix "bit-and" arguments "&"))
880
+ ('bit-or
881
+ (eliscript-emitter--emit-binary-infix "bit-or" arguments "|"))
882
+ ('bit-xor
883
+ (eliscript-emitter--emit-binary-infix "bit-xor" arguments "^"))
884
+ ('bit-not
885
+ (eliscript-emitter--require-arity "bit-not" arguments 1 1)
886
+ (format "(~%s)"
887
+ (eliscript-emitter-emit-expression (car arguments))))
888
+ ('bit-shift-left
889
+ (eliscript-emitter--emit-binary-infix
890
+ "bit-shift-left" arguments "<<"))
891
+ ('bit-shift-right
892
+ (eliscript-emitter--emit-binary-infix
893
+ "bit-shift-right" arguments ">>"))
894
+ ('unsigned-bit-shift-right
895
+ (eliscript-emitter--emit-binary-infix
896
+ "unsigned-bit-shift-right" arguments ">>>"))
897
+ ('value-type
898
+ (eliscript-emitter--require-arity "value-type" arguments 1 1)
899
+ (format
900
+ "((__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; } })(%s)"
901
+ (eliscript-emitter-emit-expression (car arguments))))
902
+ ('host-identity-token
903
+ (eliscript-emitter--require-arity
904
+ "host-identity-token" arguments 1 1)
905
+ (format "__eliscript_host_identity_token(%s)"
906
+ (eliscript-emitter-emit-expression (car arguments))))
907
+ ('string-code-unit-at
908
+ (eliscript-emitter--require-arity
909
+ "string-code-unit-at" arguments 2 2)
910
+ (format "(%s).charCodeAt(%s)"
911
+ (eliscript-emitter-emit-expression (nth 0 arguments))
912
+ (eliscript-emitter-emit-expression (nth 1 arguments))))
913
+ ('string-from-code-unit
914
+ (eliscript-emitter--require-arity
915
+ "string-from-code-unit" arguments 1 1)
916
+ (format "String.fromCharCode(%s)"
917
+ (eliscript-emitter-emit-expression (car arguments))))
918
+ ('string-to-number
919
+ (eliscript-emitter--require-arity "string-to-number" arguments 1 1)
920
+ (format "Number(%s)"
921
+ (eliscript-emitter-emit-expression (car arguments))))
922
+ ('string-to-bigint
923
+ (eliscript-emitter--require-arity "string-to-bigint" arguments 1 1)
924
+ (format
925
+ "((__eliscript_text) => { try { return BigInt(__eliscript_text); } catch { return null; } })(%s)"
926
+ (eliscript-emitter-emit-expression (car arguments))))
927
+ ('number-float64-words
928
+ (eliscript-emitter--require-arity
929
+ "number-float64-words" arguments 1 1)
930
+ (format
931
+ "((__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)]; })(%s)"
932
+ (eliscript-emitter-emit-expression (car arguments))))
933
+ ('= (eliscript-emitter--emit-comparison "=" arguments "==="))
934
+ ((or '/= 'not=)
935
+ (eliscript-emitter--emit-distinct (symbol-name operator) arguments))
936
+ ('< (eliscript-emitter--emit-comparison "<" arguments "<"))
937
+ ('<= (eliscript-emitter--emit-comparison "<=" arguments "<="))
938
+ ('> (eliscript-emitter--emit-comparison ">" arguments ">"))
939
+ ('>= (eliscript-emitter--emit-comparison ">=" arguments ">="))
940
+ ('1+
941
+ (eliscript-emitter--require-arity "1+" arguments 1 1)
942
+ (format "(%s + 1)" (eliscript-emitter-emit-expression (car arguments))))
943
+ ('1-
944
+ (eliscript-emitter--require-arity "1-" arguments 1 1)
945
+ (format "(%s - 1)" (eliscript-emitter-emit-expression (car arguments))))
946
+ ('eq
947
+ (eliscript-emitter--require-arity (symbol-name operator) arguments 2 2)
948
+ (format "(%s === %s)"
949
+ (eliscript-emitter-emit-expression (nth 0 arguments))
950
+ (eliscript-emitter-emit-expression (nth 1 arguments))))
951
+ ('equal
952
+ (eliscript-emitter--require-arity "equal" arguments 2 2)
953
+ (format "__eliscript_equal(%s, %s)"
954
+ (eliscript-emitter-emit-expression (nth 0 arguments))
955
+ (eliscript-emitter-emit-expression (nth 1 arguments))))
956
+ ('nil?
957
+ (eliscript-emitter--require-arity "nil?" arguments 1 1)
958
+ (format "(%s === null)"
959
+ (eliscript-emitter-emit-expression (car arguments))))
960
+ ('undefined?
961
+ (eliscript-emitter--require-arity "undefined?" arguments 1 1)
962
+ (format "(%s === undefined)"
963
+ (eliscript-emitter-emit-expression (car arguments))))
964
+ ((or 'null 'nullish?)
965
+ (eliscript-emitter--require-arity
966
+ (symbol-name operator) arguments 1 1)
967
+ (format "(%s == null)"
968
+ (eliscript-emitter-emit-expression (car arguments))))
969
+ ('vector
970
+ (format "__eliscript_vector(%s)"
971
+ (eliscript-emitter--emit-arguments arguments)))
972
+ ('hash-map
973
+ (when (= (% (length arguments) 2) 1)
974
+ (eliscript-emitter--fail
975
+ "hash-map expects complete key/value pairs"))
976
+ (format "__eliscript_hash_map(%s)"
977
+ (eliscript-emitter--emit-arguments arguments)))
978
+ ('hash-set
979
+ (format "__eliscript_hash_set(%s)"
980
+ (eliscript-emitter--emit-arguments arguments)))
981
+ ('queue
982
+ (format "__eliscript_queue(%s)"
983
+ (eliscript-emitter--emit-arguments arguments)))
984
+ ('list
985
+ (format "__eliscript_list(%s)"
986
+ (eliscript-emitter--emit-arguments arguments)))
987
+ ('js-array
988
+ (format "[%s]" (eliscript-emitter--emit-arguments arguments)))
989
+ ('car
990
+ (eliscript-emitter--require-arity "car" arguments 1 1)
991
+ (format "__eliscript_first(%s)"
992
+ (eliscript-emitter-emit-expression (car arguments))))
993
+ ('cdr
994
+ (eliscript-emitter--require-arity "cdr" arguments 1 1)
995
+ (format "__eliscript_rest(%s)"
996
+ (eliscript-emitter-emit-expression (car arguments))))
997
+ ('cons
998
+ (eliscript-emitter--require-arity "cons" arguments 2 2)
999
+ (format "__eliscript_cons(%s, %s)"
1000
+ (eliscript-emitter-emit-expression (nth 0 arguments))
1001
+ (eliscript-emitter-emit-expression (nth 1 arguments))))
1002
+ ('js-cons
1003
+ (eliscript-emitter--require-arity "js-cons" arguments 2 2)
1004
+ (format "[%s, ...((%s) ?? [])]"
1005
+ (eliscript-emitter-emit-expression (nth 0 arguments))
1006
+ (eliscript-emitter-emit-expression (nth 1 arguments))))
1007
+ ('nth
1008
+ (eliscript-emitter--require-arity "nth" arguments 2 2)
1009
+ (format "__eliscript_nth(%s, %s, null)"
1010
+ (eliscript-emitter-emit-expression (nth 1 arguments))
1011
+ (eliscript-emitter-emit-expression (nth 0 arguments))))
1012
+ ('js-nth
1013
+ (eliscript-emitter--require-arity "js-nth" arguments 2 2)
1014
+ (format "((((%s) ?? [])[%s]) ?? null)"
1015
+ (eliscript-emitter-emit-expression (nth 1 arguments))
1016
+ (eliscript-emitter-emit-expression (nth 0 arguments))))
1017
+ ('aref
1018
+ (eliscript-emitter--require-arity "aref" arguments 2 2)
1019
+ (format "(%s)[%s]"
1020
+ (eliscript-emitter-emit-expression (nth 0 arguments))
1021
+ (eliscript-emitter--emit-property-key (nth 1 arguments))))
1022
+ ('length
1023
+ (eliscript-emitter--require-arity "length" arguments 1 1)
1024
+ (format "__eliscript_count(%s)"
1025
+ (eliscript-emitter-emit-expression (car arguments))))
1026
+ ('js-length
1027
+ (eliscript-emitter--require-arity "js-length" arguments 1 1)
1028
+ (format "((%s) ?? []).length"
1029
+ (eliscript-emitter-emit-expression (car arguments))))
1030
+ ('__eliscript_arity_error
1031
+ (eliscript-emitter--require-arity
1032
+ "__eliscript_arity_error" arguments 2 2)
1033
+ (format
1034
+ "((label, arity) => { throw new TypeError(label + \" received unsupported arity: \" + arity); })(%s, %s)"
1035
+ (eliscript-emitter-emit-expression (nth 0 arguments))
1036
+ (eliscript-emitter-emit-expression (nth 1 arguments))))
1037
+ ('object-keys
1038
+ (eliscript-emitter--require-arity "object-keys" arguments 1 1)
1039
+ (format "Object.keys((%s) ?? {})"
1040
+ (eliscript-emitter-emit-expression (car arguments))))
1041
+ ('object-has?
1042
+ (eliscript-emitter--require-arity "object-has?" arguments 2 2)
1043
+ (format "Object.prototype.hasOwnProperty.call((%s) ?? {}, %s)"
1044
+ (eliscript-emitter-emit-expression (nth 0 arguments))
1045
+ (eliscript-emitter--emit-property-key (nth 1 arguments))))
1046
+ ('object-assoc
1047
+ (eliscript-emitter--require-arity "object-assoc" arguments 3 3)
1048
+ (format "({...((%s) ?? {}), [%s]: %s})"
1049
+ (eliscript-emitter-emit-expression (nth 0 arguments))
1050
+ (eliscript-emitter--emit-property-key (nth 1 arguments))
1051
+ (eliscript-emitter-emit-expression (nth 2 arguments))))
1052
+ ('js-object
1053
+ (eliscript-emitter--emit-object arguments))
1054
+ ('get (eliscript-emitter--emit-get arguments))
1055
+ ('put
1056
+ (eliscript-emitter--require-arity "put" arguments 3 3)
1057
+ (format "((%s)[%s] = %s)"
1058
+ (eliscript-emitter-emit-expression (nth 0 arguments))
1059
+ (eliscript-emitter--emit-property-key (nth 1 arguments))
1060
+ (eliscript-emitter-emit-expression (nth 2 arguments))))
1061
+ ('js-call (eliscript-emitter--emit-js-call arguments))
1062
+ ('new
1063
+ (eliscript-emitter--require-arity "new" arguments 1)
1064
+ (format "new %s(%s)"
1065
+ (eliscript-emitter-emit-expression (car arguments))
1066
+ (eliscript-emitter--emit-arguments (cdr arguments))))
1067
+ ('js*
1068
+ (eliscript-emitter--require-arity "js*" arguments 1 1)
1069
+ (unless (stringp (car arguments))
1070
+ (eliscript-emitter--fail "js* expects a string literal"))
1071
+ (car arguments))
1072
+ ('print
1073
+ (format "console.log(%s)"
1074
+ (eliscript-emitter--emit-arguments arguments)))
1075
+ ('str
1076
+ (if (null arguments)
1077
+ "\"\""
1078
+ (format "(%s)"
1079
+ (mapconcat
1080
+ (lambda (argument)
1081
+ (format "String(%s)"
1082
+ (eliscript-emitter-emit-expression argument)))
1083
+ arguments " + "))))
1084
+ ('funcall
1085
+ (eliscript-emitter--require-arity "funcall" arguments 1)
1086
+ (format "%s(%s)"
1087
+ (eliscript-emitter--emit-callee (car arguments))
1088
+ (eliscript-emitter--emit-arguments (cdr arguments))))
1089
+ ('apply
1090
+ (eliscript-emitter--require-arity "apply" arguments 2 2)
1091
+ (format "%s(...%s)"
1092
+ (eliscript-emitter--emit-callee (car arguments))
1093
+ (eliscript-emitter-emit-expression (cadr arguments))))
1094
+ (_
1095
+ (when (memq operator '(defun defn defvar defconst export import module))
1096
+ (eliscript-emitter--fail "%s is only valid at module top level" operator))
1097
+ (format "%s(%s)"
1098
+ (eliscript-emitter--emit-callee operator)
1099
+ (eliscript-emitter--emit-arguments arguments))))))
1100
+
1101
+ (defun eliscript-emitter-emit-expression (form)
1102
+ "Emit Eliscript FORM as an ECMAScript expression."
1103
+ (cond
1104
+ ((null form) "null")
1105
+ ((eq form t) "true")
1106
+ ((numberp form)
1107
+ (let ((text (number-to-string form)))
1108
+ (when (string-match-p "INF\\|NaN" text)
1109
+ (eliscript-emitter--fail "non-finite numbers are not supported: %S" form))
1110
+ text))
1111
+ ((stringp form) (eliscript-emitter--json-string form))
1112
+ ((keywordp form)
1113
+ (format "__eliscript_keyword(%s)"
1114
+ (eliscript-emitter--json-string
1115
+ (substring (symbol-name form) 1))))
1116
+ ((eq form 'false) "false")
1117
+ ((eq form 'undefined) "undefined")
1118
+ ((symbolp form) (eliscript-emitter--reference-name form))
1119
+ ((vectorp form)
1120
+ (format "__eliscript_vector(%s)"
1121
+ (mapconcat #'eliscript-emitter-emit-expression
1122
+ (append form nil) ", ")))
1123
+ ((consp form)
1124
+ (unless (proper-list-p form)
1125
+ (eliscript-emitter--fail "dotted call forms are not supported: %S" form))
1126
+ (eliscript-emitter--emit-call form))
1127
+ (t (eliscript-emitter--fail "unsupported form: %S" form))))
1128
+
1129
+ (defun eliscript-emitter--emit-import (arguments)
1130
+ "Emit an ESM import declaration from ARGUMENTS."
1131
+ (eliscript-emitter--require-arity "import" arguments 1)
1132
+ (let ((module-name (car arguments))
1133
+ (specifiers (cdr arguments))
1134
+ default-binding
1135
+ namespace-binding
1136
+ named-bindings)
1137
+ (unless (stringp module-name)
1138
+ (eliscript-emitter--fail "import module must be a string"))
1139
+ (while specifiers
1140
+ (let ((specifier (pop specifiers)))
1141
+ (pcase specifier
1142
+ (:default
1143
+ (unless specifiers
1144
+ (eliscript-emitter--fail ":default requires a binding"))
1145
+ (setq default-binding
1146
+ (eliscript-emitter--binding-name (pop specifiers))))
1147
+ (:as
1148
+ (unless specifiers
1149
+ (eliscript-emitter--fail ":as requires a namespace binding"))
1150
+ (setq namespace-binding
1151
+ (eliscript-emitter--binding-name (pop specifiers))))
1152
+ ((pred symbolp)
1153
+ (push (eliscript-emitter--binding-name specifier) named-bindings))
1154
+ (_ (eliscript-emitter--fail "invalid import specifier: %S" specifier)))))
1155
+ (when (and namespace-binding named-bindings)
1156
+ (eliscript-emitter--fail "namespace and named imports cannot be combined"))
1157
+ (let ((clause
1158
+ (string-join
1159
+ (delq nil
1160
+ (list default-binding
1161
+ (and namespace-binding (concat "* as " namespace-binding))
1162
+ (and named-bindings
1163
+ (format "{%s}"
1164
+ (string-join (nreverse named-bindings) ", ")))))
1165
+ ", ")))
1166
+ (if (string-empty-p clause)
1167
+ (format "import %s;" (eliscript-emitter--json-string module-name))
1168
+ (format "import %s from %s;"
1169
+ clause (eliscript-emitter--json-string module-name))))))
1170
+
1171
+ (defun eliscript-emitter-emit-top-level (form)
1172
+ "Emit top-level Eliscript FORM as an ECMAScript declaration or statement."
1173
+ (if (not (consp form))
1174
+ (concat (eliscript-emitter-emit-expression form) ";")
1175
+ (let ((operator (car form))
1176
+ (arguments (cdr form)))
1177
+ (pcase operator
1178
+ ('module
1179
+ (eliscript-emitter--require-arity "module" arguments 1)
1180
+ (unless (symbolp (car arguments))
1181
+ (eliscript-emitter--fail "module name must be a symbol"))
1182
+ (mapconcat #'eliscript-emitter-emit-top-level (cdr arguments) "\n"))
1183
+ ((or 'import 'import-portable)
1184
+ (eliscript-emitter--emit-import arguments))
1185
+ ((or 'defvar 'defconst)
1186
+ (eliscript-emitter--require-arity (symbol-name operator) arguments 1 2)
1187
+ (format "%s %s = %s;"
1188
+ (if (eq operator 'defconst) "const" "let")
1189
+ (eliscript-emitter--binding-name (car arguments))
1190
+ (eliscript-emitter-emit-expression (cadr arguments))))
1191
+ ((or 'defun 'defn 'defasync)
1192
+ (eliscript-emitter--require-arity (symbol-name operator) arguments 2)
1193
+ (let ((name (nth 0 arguments))
1194
+ (parameter-forms (nth 1 arguments))
1195
+ (body (nthcdr 2 arguments)))
1196
+ (unless (symbolp name)
1197
+ (eliscript-emitter--fail "function name must be a symbol"))
1198
+ (let ((parameters
1199
+ (eliscript-parameters-parse
1200
+ parameter-forms
1201
+ (lambda (_form message)
1202
+ (eliscript-emitter--fail "%s" message)))))
1203
+ (format "%sfunction %s(%s) {\n%s\n}"
1204
+ (if (eq operator 'defasync) "async " "")
1205
+ (eliscript-emitter--binding-name name)
1206
+ (mapconcat
1207
+ #'eliscript-emitter--emit-parameter parameters ", ")
1208
+ (eliscript-emitter--indent
1209
+ (eliscript-emitter--emit-function-body
1210
+ parameters body))))))
1211
+ ('export
1212
+ (eliscript-emitter--require-arity "export" arguments 1)
1213
+ (format "export {%s};"
1214
+ (mapconcat #'eliscript-emitter--binding-name arguments ", ")))
1215
+ ('export-default
1216
+ (eliscript-emitter--require-arity "export-default" arguments 1 1)
1217
+ (format "export default %s;"
1218
+ (eliscript-emitter-emit-expression (car arguments))))
1219
+ (_ (concat (eliscript-emitter-emit-expression form) ";"))))))
1220
+
1221
+ (defun eliscript-emitter--uses-host-identity-token-p (forms)
1222
+ "Return non-nil when FORMS contain a host identity token call."
1223
+ (cl-labels
1224
+ ((walk
1225
+ (form)
1226
+ (cond
1227
+ ((vectorp form) (cl-some #'walk (append form nil)))
1228
+ ((consp form)
1229
+ (or (eq (car form) 'host-identity-token)
1230
+ (cl-some #'walk form)))
1231
+ (t nil))))
1232
+ (cl-some #'walk forms)))
1233
+
1234
+ (defun eliscript-emitter--uses-literal-runtime-p (forms)
1235
+ "Return non-nil when FORMS construct canonical language literal values."
1236
+ (cl-labels
1237
+ ((walk-object
1238
+ (arguments)
1239
+ (let (found)
1240
+ (while arguments
1241
+ (let ((key (pop arguments))
1242
+ (value (pop arguments)))
1243
+ (when (or (and (not (or (keywordp key)
1244
+ (stringp key)
1245
+ (symbolp key)))
1246
+ (walk key))
1247
+ (walk value))
1248
+ (setq found t))))
1249
+ found))
1250
+ (walk-static-key-call
1251
+ (arguments key-index)
1252
+ (cl-loop
1253
+ for argument in arguments
1254
+ for index from 0
1255
+ thereis
1256
+ (and (not (and (= index key-index) (keywordp argument)))
1257
+ (walk argument))))
1258
+ (walk
1259
+ (form)
1260
+ (cond
1261
+ ((keywordp form) t)
1262
+ ((vectorp form) t)
1263
+ ((consp form)
1264
+ (let ((operator (car form))
1265
+ (arguments (cdr form)))
1266
+ (cond
1267
+ ((eq operator 'quote)
1268
+ (eliscript-emitter--quoted-uses-literal-runtime-p
1269
+ (car arguments)))
1270
+ ((memq operator '(import import-portable)) nil)
1271
+ ((memq operator '(list vector hash-map hash-set queue)) t)
1272
+ ((eq operator 'js-object) (walk-object arguments))
1273
+ ((memq operator '(get put js-call aref object-has? object-assoc))
1274
+ (walk-static-key-call arguments 1))
1275
+ (t (cl-some #'walk form)))))
1276
+ (t nil))))
1277
+ (cl-some #'walk forms)))
1278
+
1279
+ (defun eliscript-emitter--uses-collection-runtime-p (forms)
1280
+ "Return non-nil when FORMS use protocol-driven collection operations."
1281
+ (cl-labels
1282
+ ((walk
1283
+ (form)
1284
+ (cond
1285
+ ((vectorp form) (cl-some #'walk (append form nil)))
1286
+ ((consp form)
1287
+ (cond
1288
+ ((eq (car form) 'quote) nil)
1289
+ ((memq (car form) '(nth length)) t)
1290
+ (t (cl-some #'walk form))))
1291
+ (t nil))))
1292
+ (cl-some #'walk forms)))
1293
+
1294
+ (defun eliscript-emitter--uses-list-runtime-p (forms)
1295
+ "Return non-nil when FORMS use canonical persistent List operations."
1296
+ (cl-labels
1297
+ ((walk
1298
+ (form)
1299
+ (cond
1300
+ ((vectorp form) (cl-some #'walk (append form nil)))
1301
+ ((consp form)
1302
+ (cond
1303
+ ((eq (car form) 'quote) nil)
1304
+ ((memq (car form) '(car cdr cons)) t)
1305
+ (t (cl-some #'walk form))))
1306
+ (t nil))))
1307
+ (cl-some #'walk forms)))
1308
+
1309
+ (defun eliscript-emitter--uses-value-runtime-p (forms)
1310
+ "Return non-nil when FORMS use canonical language value equality."
1311
+ (cl-labels
1312
+ ((walk
1313
+ (form)
1314
+ (cond
1315
+ ((vectorp form) (cl-some #'walk (append form nil)))
1316
+ ((consp form)
1317
+ (cond
1318
+ ((eq (car form) 'quote) nil)
1319
+ ((eq (car form) 'equal) t)
1320
+ (t (cl-some #'walk form))))
1321
+ (t nil))))
1322
+ (cl-some #'walk forms)))
1323
+
1324
+ (defun eliscript-emit-module (forms)
1325
+ "Emit FORMS as one ECMAScript module."
1326
+ (let ((eliscript-emitter--temporary-counter 0))
1327
+ (concat
1328
+ "// Generated by Eliscript. Do not edit.\n"
1329
+ (if (eliscript-emitter--uses-literal-runtime-p forms)
1330
+ eliscript-emitter--literal-runtime-import
1331
+ "")
1332
+ (if (eliscript-emitter--uses-collection-runtime-p forms)
1333
+ eliscript-emitter--collection-runtime-import
1334
+ "")
1335
+ (if (eliscript-emitter--uses-list-runtime-p forms)
1336
+ eliscript-emitter--list-runtime-import
1337
+ "")
1338
+ (if (eliscript-emitter--uses-value-runtime-p forms)
1339
+ eliscript-emitter--value-runtime-import
1340
+ "")
1341
+ "const __eliscript_truthy = (value) => value !== false && value != null;\n"
1342
+ (if (eliscript-emitter--uses-host-identity-token-p forms)
1343
+ eliscript-emitter--host-identity-token-helper
1344
+ "")
1345
+ "\n"
1346
+ (mapconcat #'eliscript-emitter-emit-top-level forms "\n\n")
1347
+ "\n")))
1348
+
1349
+ (provide 'eliscript-emitter)
1350
+
1351
+ ;;; eliscript-emitter.el ends here