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,492 @@
1
+ ;;; eliscript-portable.el --- Portable function validation -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; Portable declarations form a closed, statically checked subset suitable for
6
+ ;; execution in a JavaScript worker over explicit versioned transport values.
7
+
8
+ ;;; Code:
9
+
10
+ (require 'cl-lib)
11
+ (require 'eliscript-diagnostic)
12
+ (require 'eliscript-form)
13
+ (require 'eliscript-parameters)
14
+
15
+ (cl-defstruct (eliscript-portable--declaration
16
+ (:constructor eliscript-portable--declaration-create))
17
+ name
18
+ kind
19
+ form
20
+ initializer
21
+ parameters
22
+ body)
23
+
24
+ (defconst eliscript-portable--operators
25
+ '(if when unless progn do while and or not
26
+ + * - / % mod = /= not= < <= > >= 1+ 1-
27
+ int32 uint32 imul32 bit-and bit-or bit-xor bit-not
28
+ bit-shift-left bit-shift-right unsigned-bit-shift-right
29
+ value-type host-identity-token string-code-unit-at string-from-code-unit
30
+ string-to-number string-to-bigint number-float64-words
31
+ eq equal null nil? undefined? nullish?
32
+ list vector hash-map hash-set js-array car cdr cons js-cons nth js-nth aref
33
+ length js-length
34
+ object-keys object-has? object-assoc __eliscript_arity_error
35
+ str funcall apply js-object get cond lambda fn let let* setq set!
36
+ quote))
37
+
38
+ (defconst eliscript-portable--forbidden-operators
39
+ '((js* . "raw JavaScript")
40
+ (print . "console output")
41
+ (put . "object mutation")
42
+ (js-call . "host method calls")
43
+ (new . "host constructors")
44
+ (async . "asynchronous functions")
45
+ (await . "asynchronous suspension")
46
+ (throw . "exception control flow")
47
+ (try . "exception control flow")))
48
+
49
+ (defvar eliscript-portable--filename nil)
50
+ (defvar eliscript-portable--entry nil)
51
+ (defvar eliscript-portable--current-span nil)
52
+
53
+ (defun eliscript-portable--fail (format-string &rest arguments)
54
+ "Signal a portable analysis error using FORMAT-STRING and ARGUMENTS."
55
+ (apply #'eliscript-diagnostic-signal
56
+ 'eliscript-analyze-error "ELI-P0001" "portable-analysis"
57
+ eliscript-portable--filename eliscript-portable--current-span
58
+ format-string arguments))
59
+
60
+ (defun eliscript-portable--value (form)
61
+ "Return the reader value represented by FORM."
62
+ (eliscript-form-value form))
63
+
64
+ (defun eliscript-portable--flatten (forms)
65
+ "Return top-level FORMS with module wrappers removed."
66
+ (apply
67
+ #'append
68
+ (mapcar
69
+ (lambda (form)
70
+ (let ((value (eliscript-portable--value form)))
71
+ (if (and (consp value)
72
+ (eq (eliscript-portable--value (car value)) 'module))
73
+ (eliscript-portable--flatten (cddr value))
74
+ (list form))))
75
+ forms)))
76
+
77
+ (defun eliscript-portable--declarations (forms)
78
+ "Collect portable-relevant declarations from FORMS."
79
+ (let ((table (make-hash-table :test #'eq)))
80
+ (dolist (form (eliscript-portable--flatten forms))
81
+ (let ((value (eliscript-portable--value form)))
82
+ (when (consp value)
83
+ (let ((operator (eliscript-portable--value (car value)))
84
+ (arguments (cdr value)))
85
+ (pcase operator
86
+ ('defconst
87
+ (puthash
88
+ (eliscript-portable--value (car arguments))
89
+ (eliscript-portable--declaration-create
90
+ :name (eliscript-portable--value (car arguments))
91
+ :kind 'constant :form form :initializer (cadr arguments))
92
+ table))
93
+ ('defvar
94
+ (puthash
95
+ (eliscript-portable--value (car arguments))
96
+ (eliscript-portable--declaration-create
97
+ :name (eliscript-portable--value (car arguments))
98
+ :kind 'variable :form form :initializer (cadr arguments))
99
+ table))
100
+ ((or 'defun 'defn 'defportable 'defasync)
101
+ (puthash
102
+ (eliscript-portable--value (car arguments))
103
+ (eliscript-portable--declaration-create
104
+ :name (eliscript-portable--value (car arguments))
105
+ :kind (if (eq operator 'defportable) 'portable 'function)
106
+ :form form
107
+ :parameters (cadr arguments)
108
+ :body (cddr arguments))
109
+ table))
110
+ ('import
111
+ (dolist (name (eliscript-portable--import-names arguments))
112
+ (puthash
113
+ name
114
+ (eliscript-portable--declaration-create
115
+ :name name :kind 'import :form form)
116
+ table)))
117
+ ('import-portable
118
+ (dolist (name (eliscript-portable--import-names arguments))
119
+ (puthash
120
+ name
121
+ (eliscript-portable--declaration-create
122
+ :name name :kind 'portable-import :form form)
123
+ table))))))))
124
+ table))
125
+
126
+ (defun eliscript-portable--import-names (arguments)
127
+ "Return local import names from import ARGUMENTS."
128
+ (let ((specifiers (cdr arguments))
129
+ names)
130
+ (while specifiers
131
+ (let ((specifier (eliscript-portable--value (pop specifiers))))
132
+ (if (memq specifier '(:default :as))
133
+ (push (eliscript-portable--value (pop specifiers)) names)
134
+ (push specifier names))))
135
+ names))
136
+
137
+ (defun eliscript-portable--local-scope (&optional names)
138
+ "Create a local-name table initialized with NAMES."
139
+ (let ((scope (make-hash-table :test #'eq)))
140
+ (dolist (name names)
141
+ (puthash (eliscript-portable--value name) t scope))
142
+ scope))
143
+
144
+ (defun eliscript-portable--copy-scope (scope)
145
+ "Return a shallow copy of local SCOPE."
146
+ (copy-hash-table scope))
147
+
148
+ (defun eliscript-portable--parameter-forms (parameters)
149
+ "Return binding forms from function PARAMETERS."
150
+ (apply
151
+ #'append
152
+ (mapcar
153
+ (lambda (parameter)
154
+ (eliscript-binding-names
155
+ (eliscript-parameter-form parameter)
156
+ (lambda (_form message) (eliscript-portable--fail "%s" message))))
157
+ (eliscript-parameters-parse
158
+ parameters
159
+ (lambda (_form message) (eliscript-portable--fail "%s" message))))))
160
+
161
+ (defun eliscript-portable--binding-name (binding)
162
+ "Return the target form from lexical BINDING."
163
+ (if (eliscript-binding-pattern-p binding)
164
+ binding
165
+ (car (eliscript-portable--value binding))))
166
+
167
+ (defun eliscript-portable--add-pattern (pattern scope)
168
+ "Add every name in binding PATTERN to portable SCOPE."
169
+ (dolist
170
+ (name
171
+ (eliscript-binding-names
172
+ pattern
173
+ (lambda (_form message) (eliscript-portable--fail "%s" message))))
174
+ (puthash (eliscript-portable--value name) t scope)))
175
+
176
+ (defun eliscript-portable--binding-value (binding)
177
+ "Return the initializer from lexical BINDING, or nil."
178
+ (let ((value (eliscript-portable--value binding)))
179
+ (and (consp value) (cadr value))))
180
+
181
+ (defun eliscript-portable--pattern-defaults
182
+ (pattern scope declarations dependencies)
183
+ "Validate defaults nested in PATTERN using portable lexical SCOPE."
184
+ (dolist
185
+ (default
186
+ (eliscript-binding-defaults
187
+ pattern
188
+ (lambda (_form message) (eliscript-portable--fail "%s" message))))
189
+ (eliscript-portable--expression default scope declarations dependencies)))
190
+
191
+ (defun eliscript-portable--reference
192
+ (form scope declarations dependencies)
193
+ "Validate symbol FORM and record it in DEPENDENCIES when top-level."
194
+ (let ((name (eliscript-portable--value form)))
195
+ (unless (gethash name scope)
196
+ (when (string-match-p "[./]" (symbol-name name))
197
+ (eliscript-portable--fail
198
+ "portable function %s uses qualified host reference: %s"
199
+ eliscript-portable--entry name))
200
+ (let ((declaration (gethash name declarations)))
201
+ (when declaration
202
+ (pcase (eliscript-portable--declaration-kind declaration)
203
+ ((or 'portable 'constant 'portable-import)
204
+ (puthash name t dependencies))
205
+ (kind
206
+ (eliscript-portable--fail
207
+ "portable function %s depends on non-portable %s: %s"
208
+ eliscript-portable--entry kind name))))))))
209
+
210
+ (defun eliscript-portable--expression
211
+ (form scope declarations dependencies)
212
+ "Validate portable expression FORM in lexical SCOPE."
213
+ (when form
214
+ (let* ((eliscript-portable--current-span
215
+ (or (eliscript-form-span form) eliscript-portable--current-span))
216
+ (value (eliscript-portable--value form)))
217
+ (cond
218
+ ((or (null value) (eq value t) (numberp value) (stringp value)
219
+ (memq value '(false undefined))) nil)
220
+ ((keywordp value) nil)
221
+ ((symbolp value)
222
+ (eliscript-portable--reference form scope declarations dependencies))
223
+ ((vectorp value)
224
+ (eliscript-portable--sequence
225
+ (append value nil) scope declarations dependencies))
226
+ ((consp value)
227
+ (eliscript-portable--call form scope declarations dependencies))))))
228
+
229
+ (defun eliscript-portable--sequence
230
+ (forms scope declarations dependencies)
231
+ "Validate portable FORMS in lexical SCOPE."
232
+ (dolist (form forms)
233
+ (eliscript-portable--expression form scope declarations dependencies)))
234
+
235
+ (defun eliscript-portable--function
236
+ (arguments scope declarations dependencies)
237
+ "Validate a lambda-like ARGUMENT list."
238
+ (let* ((parameters (eliscript-portable--parameter-forms (car arguments)))
239
+ (child (eliscript-portable--copy-scope scope)))
240
+ (dolist (parameter
241
+ (eliscript-parameters-parse
242
+ (car arguments)
243
+ (lambda (_form message) (eliscript-portable--fail "%s" message))))
244
+ (eliscript-portable--pattern-defaults
245
+ (eliscript-parameter-form parameter) scope declarations dependencies))
246
+ (dolist (parameter parameters)
247
+ (puthash (eliscript-portable--value parameter) t child))
248
+ (eliscript-portable--sequence
249
+ (cdr arguments) child declarations dependencies)))
250
+
251
+ (defun eliscript-portable--let
252
+ (arguments scope sequential declarations dependencies)
253
+ "Validate let ARGUMENTS, honoring SEQUENTIAL binding visibility."
254
+ (let* ((bindings (eliscript-portable--value (car arguments)))
255
+ (child (eliscript-portable--copy-scope scope)))
256
+ (if sequential
257
+ (dolist (binding bindings)
258
+ (eliscript-portable--expression
259
+ (eliscript-portable--binding-value binding)
260
+ child declarations dependencies)
261
+ (eliscript-portable--pattern-defaults
262
+ (eliscript-portable--binding-name binding)
263
+ child declarations dependencies)
264
+ (eliscript-portable--add-pattern
265
+ (eliscript-portable--binding-name binding) child))
266
+ (progn
267
+ (dolist (binding bindings)
268
+ (eliscript-portable--expression
269
+ (eliscript-portable--binding-value binding)
270
+ scope declarations dependencies))
271
+ (dolist (binding bindings)
272
+ (eliscript-portable--pattern-defaults
273
+ (eliscript-portable--binding-name binding)
274
+ scope declarations dependencies))
275
+ (dolist (binding bindings)
276
+ (eliscript-portable--add-pattern
277
+ (eliscript-portable--binding-name binding) child))))
278
+ (eliscript-portable--sequence
279
+ (cdr arguments) child declarations dependencies)))
280
+
281
+ (defun eliscript-portable--assignment
282
+ (arguments scope declarations dependencies)
283
+ "Validate assignment ARGUMENTS as local-only mutation."
284
+ (while arguments
285
+ (let* ((name-form (pop arguments))
286
+ (name (eliscript-portable--value name-form))
287
+ (value (pop arguments)))
288
+ (unless (gethash name scope)
289
+ (let ((eliscript-portable--current-span
290
+ (or (eliscript-form-span name-form)
291
+ eliscript-portable--current-span)))
292
+ (eliscript-portable--fail
293
+ "portable function %s may only assign local bindings: %s"
294
+ eliscript-portable--entry name)))
295
+ (eliscript-portable--expression
296
+ value scope declarations dependencies))))
297
+
298
+ (defun eliscript-portable--object
299
+ (arguments scope declarations dependencies)
300
+ "Validate object ARGUMENTS without treating literal keys as references."
301
+ (while arguments
302
+ (let* ((key (pop arguments))
303
+ (key-value (eliscript-portable--value key))
304
+ (value (pop arguments)))
305
+ (unless (or (keywordp key-value) (stringp key-value)
306
+ (symbolp key-value))
307
+ (eliscript-portable--expression
308
+ key scope declarations dependencies))
309
+ (eliscript-portable--expression
310
+ value scope declarations dependencies))))
311
+
312
+ (defun eliscript-portable--host-key-call
313
+ (arguments scope declarations dependencies)
314
+ "Validate host-key ARGUMENTS while keeping Keyword keys syntactic."
315
+ (cl-loop
316
+ for argument in arguments
317
+ for index from 0
318
+ unless (and (= index 1)
319
+ (keywordp (eliscript-portable--value argument)))
320
+ do (eliscript-portable--expression
321
+ argument scope declarations dependencies)))
322
+
323
+ (defun eliscript-portable--call (form scope declarations dependencies)
324
+ "Validate portable call FORM."
325
+ (let* ((items (eliscript-portable--value form))
326
+ (operator-form (car items))
327
+ (operator (eliscript-portable--value operator-form))
328
+ (arguments (cdr items))
329
+ (forbidden (and (symbolp operator)
330
+ (assq operator
331
+ eliscript-portable--forbidden-operators))))
332
+ (when forbidden
333
+ (eliscript-portable--fail
334
+ "portable function %s cannot use %s (%s)"
335
+ eliscript-portable--entry operator (cdr forbidden)))
336
+ (pcase operator
337
+ ('quote nil)
338
+ ((or 'lambda 'fn)
339
+ (eliscript-portable--function
340
+ arguments scope declarations dependencies))
341
+ ('let
342
+ (eliscript-portable--let
343
+ arguments scope nil declarations dependencies))
344
+ ('let*
345
+ (eliscript-portable--let
346
+ arguments scope t declarations dependencies))
347
+ ((or 'setq 'set!)
348
+ (eliscript-portable--assignment
349
+ arguments scope declarations dependencies))
350
+ ('js-object
351
+ (eliscript-portable--object
352
+ arguments scope declarations dependencies))
353
+ ((or 'get 'aref 'object-has? 'object-assoc)
354
+ (eliscript-portable--host-key-call
355
+ arguments scope declarations dependencies))
356
+ ((pred (lambda (name) (memq name eliscript-portable--operators)))
357
+ (eliscript-portable--sequence
358
+ arguments scope declarations dependencies))
359
+ (_
360
+ (eliscript-portable--expression
361
+ operator-form scope declarations dependencies)
362
+ (eliscript-portable--sequence
363
+ arguments scope declarations dependencies)))))
364
+
365
+ (defun eliscript-portable--dependencies (declaration declarations)
366
+ "Return validated direct dependencies for DECLARATION."
367
+ (let* ((eliscript-portable--entry
368
+ (eliscript-portable--declaration-name declaration))
369
+ (eliscript-portable--current-span
370
+ (eliscript-form-span (eliscript-portable--declaration-form declaration)))
371
+ (dependencies (make-hash-table :test #'eq))
372
+ (scope
373
+ (eliscript-portable--local-scope
374
+ (eliscript-portable--parameter-forms
375
+ (eliscript-portable--declaration-parameters declaration)))))
376
+ (pcase (eliscript-portable--declaration-kind declaration)
377
+ ('portable
378
+ (eliscript-portable--sequence
379
+ (eliscript-portable--declaration-body declaration)
380
+ scope declarations dependencies))
381
+ ('constant
382
+ (eliscript-portable--expression
383
+ (eliscript-portable--declaration-initializer declaration)
384
+ scope declarations dependencies)))
385
+ (let (names)
386
+ (maphash (lambda (name _present) (push name names)) dependencies)
387
+ (nreverse names))))
388
+
389
+ (defun eliscript-portable--closure
390
+ (forms entries filename &optional allow-portable-imports)
391
+ "Validate ENTRIES in FORMS and return their transitive declaration names.
392
+
393
+ ALLOW-PORTABLE-IMPORTS defers cross-module proof to the project builder."
394
+ (let* ((eliscript-portable--filename filename)
395
+ (declarations (eliscript-portable--declarations forms))
396
+ (visited (make-hash-table :test #'eq))
397
+ closure)
398
+ (cl-labels
399
+ ((visit
400
+ (name root)
401
+ (unless (gethash name visited)
402
+ (puthash name t visited)
403
+ (let ((declaration (gethash name declarations)))
404
+ (unless declaration
405
+ (setq eliscript-portable--entry root)
406
+ (eliscript-portable--fail
407
+ "unknown portable entry: %s" name))
408
+ (unless (memq (eliscript-portable--declaration-kind declaration)
409
+ '(portable constant portable-import))
410
+ (setq eliscript-portable--entry root
411
+ eliscript-portable--current-span
412
+ (eliscript-form-span
413
+ (eliscript-portable--declaration-form declaration)))
414
+ (eliscript-portable--fail
415
+ "portable entry %s is not declared with defportable" name))
416
+ (when (and (eq (eliscript-portable--declaration-kind declaration)
417
+ 'portable-import)
418
+ (not allow-portable-imports))
419
+ (setq eliscript-portable--entry root
420
+ eliscript-portable--current-span
421
+ (eliscript-form-span
422
+ (eliscript-portable--declaration-form declaration)))
423
+ (eliscript-portable--fail
424
+ "portable import %s requires a project build" name))
425
+ (push name closure)
426
+ (dolist (dependency
427
+ (eliscript-portable--dependencies
428
+ declaration declarations))
429
+ (visit dependency root))))))
430
+ (dolist (entry entries)
431
+ (let ((declaration (gethash entry declarations)))
432
+ (unless (and declaration
433
+ (eq (eliscript-portable--declaration-kind declaration)
434
+ 'portable))
435
+ (setq eliscript-portable--entry entry)
436
+ (eliscript-portable--fail
437
+ "portable entry %s is not declared with defportable" entry)))
438
+ (visit entry entry)))
439
+ (nreverse closure)))
440
+
441
+ (defun eliscript-portable-validate-module (forms &optional filename)
442
+ "Validate every portable declaration in FORMS from FILENAME."
443
+ (let (entries)
444
+ (dolist (form (eliscript-portable--flatten forms))
445
+ (let ((value (eliscript-portable--value form)))
446
+ (when (and (consp value)
447
+ (eq (eliscript-portable--value (car value)) 'defportable))
448
+ (push (eliscript-portable--value (cadr value)) entries))))
449
+ (eliscript-portable--closure forms (nreverse entries) filename t)
450
+ forms))
451
+
452
+ (defun eliscript-portable-select-module
453
+ (forms entries &optional filename allow-portable-imports)
454
+ "Return the declaration closure for portable ENTRIES in FORMS.
455
+
456
+ ALLOW-PORTABLE-IMPORTS is reserved for graph-aware project builds."
457
+ (let* ((entry-symbols
458
+ (mapcar (lambda (entry)
459
+ (if (symbolp entry) entry (intern entry)))
460
+ entries))
461
+ (closure
462
+ (eliscript-portable--closure
463
+ forms entry-symbols filename allow-portable-imports))
464
+ (selected (make-hash-table :test #'eq)))
465
+ (dolist (name closure) (puthash name t selected))
466
+ (delq
467
+ nil
468
+ (mapcar
469
+ (lambda (form)
470
+ (let ((value (eliscript-portable--value form)))
471
+ (when (consp value)
472
+ (let ((operator (eliscript-portable--value (car value))))
473
+ (if (eq operator 'import-portable)
474
+ (let ((names
475
+ (cl-remove-if-not
476
+ (lambda (name-form)
477
+ (gethash
478
+ (eliscript-portable--value name-form) selected))
479
+ (cddr value))))
480
+ (when names
481
+ (eliscript-form-inherit
482
+ (append (list (car value) (cadr value)) names)
483
+ form)))
484
+ (and (memq operator '(defconst defportable))
485
+ (gethash
486
+ (eliscript-portable--value (cadr value)) selected)
487
+ form))))))
488
+ (eliscript-portable--flatten forms)))))
489
+
490
+ (provide 'eliscript-portable)
491
+
492
+ ;;; eliscript-portable.el ends here
@@ -0,0 +1,156 @@
1
+ ;;; eliscript-project-cli.el --- Batch project builder -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; Run with:
6
+ ;; emacs --batch -Q --script compiler/eliscript-project-cli.el -- ENTRY --out-dir DIR
7
+
8
+ ;;; Code:
9
+
10
+ (add-to-list 'load-path
11
+ (file-name-directory (or load-file-name buffer-file-name)))
12
+ (require 'eliscript-project)
13
+
14
+ (defconst eliscript-project-cli--usage
15
+ (concat
16
+ "Usage: eliscript-build [--config FILE] [--root DIR] [--portable NAME]\n"
17
+ " [--no-cache]\n"
18
+ " [--json] [--diagnostic-format human|json]\n"
19
+ " --out-dir DIR ENTRY...\n\n"
20
+ "Compile ENTRIES and their relative .eli imports into an ESM directory tree.\n"
21
+ "Use --config FILE for a versioned project request. Explicit build flags\n"
22
+ "and ENTRIES override configured values; --no-cache always disables reuse.\n"
23
+ "Write eliscript-project.json with deterministic graph content digests.\n"
24
+ "Reuse verified modules by default; --no-cache forces complete compilation.\n"
25
+ "Use --json for a machine-readable build decision report on stdout.\n"
26
+ "Use JSON diagnostics for failures on stderr.\n"
27
+ "Repeat --portable to emit a verified, dependency-pruned portable graph.\n"))
28
+
29
+ (defun eliscript-project-cli--requested-diagnostic-format (arguments)
30
+ "Return a usable diagnostic format requested by raw ARGUMENTS."
31
+ (let ((remaining arguments)
32
+ (format-name "human"))
33
+ (while remaining
34
+ (when (and (equal (car remaining) "--diagnostic-format")
35
+ (cdr remaining)
36
+ (equal (cadr remaining) "json"))
37
+ (setq format-name "json"))
38
+ (setq remaining (cdr remaining)))
39
+ format-name))
40
+
41
+ (defun eliscript-project-cli--parse (arguments)
42
+ "Parse ARGUMENTS into a project request and presentation fields."
43
+ (when (equal (car arguments) "--")
44
+ (setq arguments (cdr arguments)))
45
+ (let ((use-cache t)
46
+ (json-report nil)
47
+ (diagnostic-format "human")
48
+ entries out-dir root portable-entries configuration)
49
+ (while arguments
50
+ (let ((argument (pop arguments)))
51
+ (cond
52
+ ((member argument '("-h" "--help"))
53
+ (princ eliscript-project-cli--usage)
54
+ (kill-emacs 0))
55
+ ((equal argument "--out-dir")
56
+ (unless arguments
57
+ (error "%s requires a directory" argument))
58
+ (setq out-dir (pop arguments)))
59
+ ((equal argument "--config")
60
+ (unless arguments
61
+ (error "%s requires a file" argument))
62
+ (when configuration
63
+ (error "multiple --config files are not supported"))
64
+ (setq configuration (pop arguments)))
65
+ ((equal argument "--root")
66
+ (unless arguments
67
+ (error "%s requires a directory" argument))
68
+ (setq root (pop arguments)))
69
+ ((equal argument "--portable")
70
+ (unless arguments
71
+ (error "%s requires an entry name" argument))
72
+ (push (pop arguments) portable-entries))
73
+ ((equal argument "--no-cache")
74
+ (setq use-cache nil))
75
+ ((equal argument "--json")
76
+ (setq json-report t))
77
+ ((equal argument "--diagnostic-format")
78
+ (unless arguments
79
+ (error "%s requires human or json" argument))
80
+ (setq diagnostic-format (pop arguments))
81
+ (unless (member diagnostic-format '("human" "json"))
82
+ (error "unsupported diagnostic format: %s" diagnostic-format)))
83
+ ((string-prefix-p "-" argument)
84
+ (error "unknown option: %s" argument))
85
+ (t (push argument entries)))))
86
+ (setq entries (nreverse entries))
87
+ (let ((request
88
+ (if configuration
89
+ (let ((configured
90
+ (eliscript-project-read-configuration configuration)))
91
+ (when entries
92
+ (if (= (length entries) 1)
93
+ (progn
94
+ (setf (eliscript-project-request-entry configured)
95
+ (car entries))
96
+ (setf (eliscript-project-request-entries configured)
97
+ nil))
98
+ (setf (eliscript-project-request-entry configured) nil)
99
+ (setf (eliscript-project-request-entries configured)
100
+ entries)))
101
+ (when out-dir
102
+ (setf (eliscript-project-request-out-dir configured) out-dir))
103
+ (when root
104
+ (setf (eliscript-project-request-root configured) root))
105
+ (when portable-entries
106
+ (setf (eliscript-project-request-portable-entries configured)
107
+ (nreverse portable-entries)))
108
+ (unless use-cache
109
+ (setf (eliscript-project-request-use-cache configured) nil))
110
+ configured)
111
+ (progn
112
+ (unless entries
113
+ (error "missing entry file"))
114
+ (unless out-dir
115
+ (error "missing --out-dir"))
116
+ (eliscript-project-request-create
117
+ :entry (and (= (length entries) 1) (car entries))
118
+ :entries (and (> (length entries) 1) entries)
119
+ :out-dir out-dir
120
+ :root root
121
+ :portable-entries (nreverse portable-entries)
122
+ :use-cache use-cache)))))
123
+ (list request json-report diagnostic-format))))
124
+
125
+ (defun eliscript-project-cli-main (arguments)
126
+ "Build an Eliscript project according to command-line ARGUMENTS."
127
+ (let ((requested-format
128
+ (eliscript-project-cli--requested-diagnostic-format arguments)))
129
+ (condition-case error-data
130
+ (pcase-let ((`(,request ,json-report ,diagnostic-format)
131
+ (eliscript-project-cli--parse arguments)))
132
+ (setq requested-format diagnostic-format)
133
+ (let ((result (eliscript-project-execute request)))
134
+ (princ
135
+ (if json-report
136
+ (json-serialize
137
+ (eliscript-project-build-report result)
138
+ :false-object :false)
139
+ (mapconcat #'identity
140
+ (eliscript-project-build-result-entry-outputs result)
141
+ "\n")))
142
+ (princ "\n")))
143
+ (error
144
+ (if (equal requested-format "json")
145
+ (message
146
+ "%s"
147
+ (eliscript-diagnostic-to-json
148
+ (eliscript-diagnostic-from-error error-data)))
149
+ (message
150
+ "eliscript-build: %s"
151
+ (eliscript-diagnostic-condition-message error-data)))
152
+ (kill-emacs 1)))))
153
+
154
+ (eliscript-project-cli-main command-line-args-left)
155
+
156
+ ;;; eliscript-project-cli.el ends here