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,817 @@
1
+ ;;; eliscript-transient-analysis.el --- Transient ownership analysis -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; This pass recognizes only the canonical transient modules. It proves that
6
+ ;; editable builders remain local to one synchronous ownership region and that
7
+ ;; completion is a one-way state transition before ordinary lowering begins.
8
+
9
+ ;;; Code:
10
+
11
+ (require 'cl-lib)
12
+ (require 'eliscript-diagnostic)
13
+ (require 'eliscript-form)
14
+ (require 'eliscript-parameters)
15
+
16
+ (cl-defstruct (eliscript-transient--binding
17
+ (:constructor eliscript-transient--binding-create))
18
+ name
19
+ operation
20
+ namespace
21
+ resource)
22
+
23
+ (cl-defstruct (eliscript-transient--resource
24
+ (:constructor eliscript-transient--resource-create))
25
+ name
26
+ function-id
27
+ span)
28
+
29
+ (cl-defstruct (eliscript-transient--scope
30
+ (:constructor eliscript-transient--scope-create))
31
+ parent
32
+ names)
33
+
34
+ (defvar eliscript-transient--filename nil)
35
+ (defvar eliscript-transient--current-span nil)
36
+ (defvar eliscript-transient--state nil)
37
+ (defvar eliscript-transient--function-id 0)
38
+ (defvar eliscript-transient--next-function-id 0)
39
+ (defvar eliscript-transient--loop-protected nil)
40
+ (defvar eliscript-transient--try-prefix-states nil)
41
+
42
+ (defconst eliscript-transient--trusted-core-sources
43
+ '("stdlib/core/data.eli"
44
+ "stdlib/core/seq.eli"
45
+ "stdlib/core/set.eli"
46
+ "stdlib/core/transient.eli")
47
+ "Core implementation sources that currently require unchecked sync borrows.")
48
+
49
+ (defun eliscript-transient--fail (format-string &rest arguments)
50
+ "Signal a transient ownership error using FORMAT-STRING and ARGUMENTS."
51
+ (apply #'eliscript-diagnostic-signal
52
+ 'eliscript-analyze-error "ELI-A0001" "analysis"
53
+ eliscript-transient--filename eliscript-transient--current-span
54
+ format-string arguments))
55
+
56
+ (defun eliscript-transient--make-scope (&optional parent)
57
+ "Create a transient-analysis scope below PARENT."
58
+ (eliscript-transient--scope-create
59
+ :parent parent :names (make-hash-table :test #'eq)))
60
+
61
+ (defun eliscript-transient--trusted-core-source-p (filename)
62
+ "Return non-nil when FILENAME names an exact trusted core source."
63
+ (and
64
+ (stringp filename)
65
+ (cl-some
66
+ (lambda (suffix)
67
+ (or (string= filename suffix)
68
+ (string-suffix-p (concat "/" suffix) filename)))
69
+ eliscript-transient--trusted-core-sources)))
70
+
71
+ (defun eliscript-transient--declare (scope name &optional operation namespace)
72
+ "Declare NAME in SCOPE with optional OPERATION or NAMESPACE provenance."
73
+ (let* ((value (eliscript-form-value name))
74
+ (binding (eliscript-transient--binding-create
75
+ :name value :operation operation :namespace namespace)))
76
+ (puthash value binding (eliscript-transient--scope-names scope))
77
+ binding))
78
+
79
+ (defun eliscript-transient--lookup (scope name)
80
+ "Resolve NAME from SCOPE and its parents."
81
+ (let (binding)
82
+ (while (and scope (not binding))
83
+ (setq binding (gethash name (eliscript-transient--scope-names scope))
84
+ scope (eliscript-transient--scope-parent scope)))
85
+ binding))
86
+
87
+ (defun eliscript-transient--canonical-module-family (source)
88
+ "Return the canonical transient module family for SOURCE, or nil."
89
+ (when (and (stringp source)
90
+ (string-match-p
91
+ "\\(?:\\`\\|/\\)\\(?:stdlib\\|runtime\\)/core/transient\\.\\(?:mjs\\|eli\\)\\'"
92
+ source))
93
+ (if (string-match-p "\\(?:\\`\\|/\\)runtime/core/" source)
94
+ 'runtime
95
+ 'stdlib)))
96
+
97
+ (defun eliscript-transient--operation (family name)
98
+ "Return the transient operation represented by FAMILY and NAME."
99
+ (pcase family
100
+ ('stdlib
101
+ (pcase name
102
+ ('transient 'construct)
103
+ ((or 'conj! 'assoc! 'dissoc!) 'update)
104
+ ('persistent! 'complete)))
105
+ ('runtime
106
+ (pcase name
107
+ ('transient 'construct)
108
+ ((or 'conjBang 'assocBang 'dissocBang) 'update)
109
+ ('persistentBang 'complete)))))
110
+
111
+ (defun eliscript-transient--operator-binding (operator scope)
112
+ "Resolve transient OPERATOR provenance in SCOPE."
113
+ (let ((value (eliscript-form-value operator)))
114
+ (when (symbolp value)
115
+ (or
116
+ (let ((binding (eliscript-transient--lookup scope value)))
117
+ (and binding
118
+ (eliscript-transient--binding-operation binding)
119
+ binding))
120
+ (let ((name (symbol-name value)))
121
+ (when (string-match "\\`\\([^/]+\\)/\\([^/]+\\)\\'" name)
122
+ (let* ((namespace
123
+ (eliscript-transient--lookup
124
+ scope (intern (match-string 1 name))))
125
+ (family
126
+ (and namespace
127
+ (eliscript-transient--binding-namespace namespace)))
128
+ (operation
129
+ (and family
130
+ (eliscript-transient--operation
131
+ family (intern (match-string 2 name))))))
132
+ (and operation
133
+ (eliscript-transient--binding-create
134
+ :name value :operation operation)))))))))
135
+
136
+ (defun eliscript-transient--copy-state ()
137
+ "Return a shallow copy of the current ownership state."
138
+ (copy-hash-table eliscript-transient--state))
139
+
140
+ (defun eliscript-transient--merge-state (baseline branches)
141
+ "Merge visible resource states from BRANCHES using BASELINE as the key set."
142
+ (let ((result (copy-hash-table baseline)))
143
+ (maphash
144
+ (lambda (resource original)
145
+ (let ((states
146
+ (mapcar (lambda (branch)
147
+ (gethash resource branch original))
148
+ branches)))
149
+ (puthash
150
+ resource
151
+ (if (cl-every (lambda (state) (eq state (car states)))
152
+ (cdr states))
153
+ (car states)
154
+ 'maybe)
155
+ result)))
156
+ baseline)
157
+ result))
158
+
159
+ (defun eliscript-transient--run-with-state (state function)
160
+ "Run FUNCTION with a copy of STATE and return the resulting state."
161
+ (let ((eliscript-transient--state (copy-hash-table state)))
162
+ (funcall function)
163
+ eliscript-transient--state))
164
+
165
+ (defun eliscript-transient--retain-state-keys (baseline)
166
+ "Discard local resources absent from BASELINE while preserving updates."
167
+ (let ((result (make-hash-table :test #'eq)))
168
+ (maphash
169
+ (lambda (resource original)
170
+ (puthash resource
171
+ (gethash resource eliscript-transient--state original)
172
+ result))
173
+ baseline)
174
+ (setq eliscript-transient--state result)))
175
+
176
+ (defun eliscript-transient--record-try-prefix ()
177
+ "Record the current state as one possible exceptional TRY exit."
178
+ (when eliscript-transient--try-prefix-states
179
+ (push (eliscript-transient--copy-state)
180
+ eliscript-transient--try-prefix-states)))
181
+
182
+ (defun eliscript-transient--resource-state (resource)
183
+ "Return RESOURCE's current ownership state."
184
+ (gethash resource eliscript-transient--state 'active))
185
+
186
+ (defun eliscript-transient--check-owner (binding)
187
+ "Return BINDING's resource after validating function ownership and state."
188
+ (let* ((resource (eliscript-transient--binding-resource binding))
189
+ (name (eliscript-transient--binding-name binding))
190
+ (state (and resource
191
+ (eliscript-transient--resource-state resource))))
192
+ (unless resource
193
+ (eliscript-transient--fail
194
+ "transient operation requires a directly bound transient value"))
195
+ (unless (= (eliscript-transient--resource-function-id resource)
196
+ eliscript-transient--function-id)
197
+ (eliscript-transient--fail
198
+ "transient binding %s cannot cross a function boundary" name))
199
+ (pcase state
200
+ ('active resource)
201
+ ('consumed
202
+ (eliscript-transient--fail
203
+ "transient binding %s is no longer editable after persistent!" name))
204
+ (_
205
+ (eliscript-transient--fail
206
+ "transient binding %s may already be persistent after conditional control flow"
207
+ name)))))
208
+
209
+ (defun eliscript-transient--direct-resource (form scope)
210
+ "Return the active transient resource named directly by FORM in SCOPE."
211
+ (let* ((value (eliscript-form-value form))
212
+ (binding (and (symbolp value)
213
+ (eliscript-transient--lookup scope value))))
214
+ (unless (and binding (eliscript-transient--binding-resource binding))
215
+ (eliscript-transient--fail
216
+ "transient operation requires a directly bound transient value"))
217
+ (eliscript-transient--check-owner binding)))
218
+
219
+ (defun eliscript-transient--analyze-reference (form scope)
220
+ "Validate a reference FORM in SCOPE against transient ownership."
221
+ (let* ((name (eliscript-form-value form))
222
+ (operation-binding
223
+ (eliscript-transient--operator-binding form scope))
224
+ (binding (eliscript-transient--lookup scope name)))
225
+ (when operation-binding
226
+ (eliscript-transient--fail
227
+ "transient operation %s must be called directly" name))
228
+ (when binding
229
+ (cond
230
+ ((eliscript-transient--binding-operation binding)
231
+ (eliscript-transient--fail
232
+ "transient operation %s must be called directly" name))
233
+ ((eliscript-transient--binding-resource binding)
234
+ (let ((resource (eliscript-transient--binding-resource binding)))
235
+ (unless (= (eliscript-transient--resource-function-id resource)
236
+ eliscript-transient--function-id)
237
+ (eliscript-transient--fail
238
+ "transient binding %s cannot cross a function boundary" name))
239
+ (pcase (eliscript-transient--resource-state resource)
240
+ ('active
241
+ (eliscript-transient--fail
242
+ "transient binding %s may only be used by a direct transient operation"
243
+ name))
244
+ ('consumed
245
+ (eliscript-transient--fail
246
+ "transient binding %s is no longer editable after persistent!"
247
+ name))
248
+ (_
249
+ (eliscript-transient--fail
250
+ "transient binding %s may already be persistent after conditional control flow"
251
+ name)))))))))
252
+
253
+ (defun eliscript-transient--binding-pair (binding)
254
+ "Return the target and initializer represented by BINDING."
255
+ (let ((value (eliscript-form-value binding)))
256
+ (if (and (proper-list-p value) value)
257
+ (list (car value) (cadr value))
258
+ (list binding nil))))
259
+
260
+ (defun eliscript-transient--declare-pattern (scope pattern)
261
+ "Declare every name in PATTERN and return the new bindings."
262
+ (mapcar (lambda (name) (eliscript-transient--declare scope name))
263
+ (eliscript-binding-names pattern (lambda (_form message)
264
+ (error "%s" message)))))
265
+
266
+ (defun eliscript-transient--make-pattern-bindings (pattern)
267
+ "Create uninstalled bindings for every name in PATTERN."
268
+ (mapcar
269
+ (lambda (name)
270
+ (eliscript-transient--binding-create
271
+ :name (eliscript-form-value name)))
272
+ (eliscript-binding-names pattern (lambda (_form message)
273
+ (error "%s" message)))))
274
+
275
+ (defun eliscript-transient--install-bindings (scope bindings)
276
+ "Install BINDINGS into SCOPE after their initializer has been analyzed."
277
+ (dolist (binding bindings)
278
+ (puthash (eliscript-transient--binding-name binding)
279
+ binding
280
+ (eliscript-transient--scope-names scope)))
281
+ bindings)
282
+
283
+ (defun eliscript-transient--new-resource (binding span)
284
+ "Attach a fresh transient resource at SPAN to BINDING."
285
+ (let ((resource
286
+ (eliscript-transient--resource-create
287
+ :name (eliscript-transient--binding-name binding)
288
+ :function-id eliscript-transient--function-id
289
+ :span span)))
290
+ (setf (eliscript-transient--binding-resource binding) resource)
291
+ (puthash resource 'active eliscript-transient--state)
292
+ resource))
293
+
294
+ (defun eliscript-transient--constructor-call-p (form scope)
295
+ "Return non-nil when FORM is a canonical transient constructor call."
296
+ (let ((value (eliscript-form-value form)))
297
+ (and (proper-list-p value)
298
+ value
299
+ (let ((binding
300
+ (eliscript-transient--operator-binding (car value) scope)))
301
+ (and binding
302
+ (eq (eliscript-transient--binding-operation binding)
303
+ 'construct))))))
304
+
305
+ (defun eliscript-transient--analyze-initializer
306
+ (initializer scope bindings &optional predeclared)
307
+ "Analyze INITIALIZER for BINDINGS in SCOPE.
308
+
309
+ When PREDECLARED is non-nil, reuse an already attached top-level resource."
310
+ (if (and initializer
311
+ (eliscript-transient--constructor-call-p initializer scope))
312
+ (let* ((value (eliscript-form-value initializer))
313
+ (arguments (cdr value)))
314
+ (unless (and (= (length bindings) 1)
315
+ (symbolp (eliscript-transient--binding-name
316
+ (car bindings))))
317
+ (let ((eliscript-transient--current-span
318
+ (or (eliscript-form-span initializer)
319
+ eliscript-transient--current-span)))
320
+ (eliscript-transient--fail
321
+ "transient result must be bound directly to one symbol")))
322
+ (unless (= (length arguments) 1)
323
+ (let ((eliscript-transient--current-span
324
+ (or (eliscript-form-span initializer)
325
+ eliscript-transient--current-span)))
326
+ (eliscript-transient--fail "transient expects 1 argument")))
327
+ (eliscript-transient--analyze-expression
328
+ (car arguments) scope t)
329
+ (unless (and predeclared
330
+ (eliscript-transient--binding-resource (car bindings)))
331
+ (eliscript-transient--new-resource
332
+ (car bindings) (eliscript-form-span initializer))))
333
+ (when initializer
334
+ (eliscript-transient--analyze-expression initializer scope t))))
335
+
336
+ (defun eliscript-transient--analyze-sequence (forms scope value-context)
337
+ "Analyze FORMS in SCOPE, using VALUE-CONTEXT for the final form."
338
+ (while forms
339
+ (eliscript-transient--analyze-expression
340
+ (car forms) scope (and value-context (null (cdr forms))))
341
+ (setq forms (cdr forms))))
342
+
343
+ (defun eliscript-transient--analyze-let
344
+ (arguments scope sequential value-context)
345
+ "Analyze LET ARGUMENTS with SEQUENTIAL binding semantics."
346
+ (let* ((baseline (eliscript-transient--copy-state))
347
+ (bindings (eliscript-form-value (car arguments)))
348
+ (body (cdr arguments))
349
+ (child (eliscript-transient--make-scope scope)))
350
+ (if sequential
351
+ (dolist (entry bindings)
352
+ (pcase-let* ((`(,target ,initializer)
353
+ (eliscript-transient--binding-pair entry))
354
+ (declared
355
+ (eliscript-transient--make-pattern-bindings target)))
356
+ (eliscript-transient--analyze-initializer
357
+ initializer child declared)
358
+ (eliscript-transient--install-bindings child declared)))
359
+ (let ((parsed (mapcar #'eliscript-transient--binding-pair bindings))
360
+ declared)
361
+ (dolist (entry parsed)
362
+ (push (eliscript-transient--make-pattern-bindings (car entry))
363
+ declared))
364
+ (setq declared (nreverse declared))
365
+ (cl-mapc
366
+ (lambda (entry targets)
367
+ (eliscript-transient--analyze-initializer
368
+ (cadr entry) scope targets))
369
+ parsed declared)
370
+ (dolist (targets declared)
371
+ (eliscript-transient--install-bindings child targets))))
372
+ (eliscript-transient--analyze-sequence body child value-context)
373
+ (eliscript-transient--retain-state-keys baseline)))
374
+
375
+ (defun eliscript-transient--analyze-function (parameters body scope)
376
+ "Analyze a function with PARAMETERS and BODY below SCOPE."
377
+ (let* ((eliscript-transient--next-function-id
378
+ (1+ eliscript-transient--next-function-id))
379
+ (eliscript-transient--function-id eliscript-transient--next-function-id)
380
+ (eliscript-transient--state (copy-hash-table eliscript-transient--state))
381
+ (eliscript-transient--loop-protected nil)
382
+ (child (eliscript-transient--make-scope scope)))
383
+ (dolist (parameter
384
+ (eliscript-parameters-parse parameters
385
+ (lambda (_form message) (error "%s" message))))
386
+ (eliscript-transient--declare-pattern
387
+ child (eliscript-parameter-form parameter)))
388
+ (eliscript-transient--analyze-sequence body child t)))
389
+
390
+ (defun eliscript-transient--analyze-if
391
+ (arguments scope value-context)
392
+ "Analyze conditional ARGUMENTS with ownership-state merging."
393
+ (eliscript-transient--analyze-expression (car arguments) scope t)
394
+ (let* ((baseline (eliscript-transient--copy-state))
395
+ (then-state
396
+ (eliscript-transient--run-with-state
397
+ baseline
398
+ (lambda ()
399
+ (eliscript-transient--analyze-expression
400
+ (cadr arguments) scope value-context))))
401
+ (else-state
402
+ (if (cddr arguments)
403
+ (eliscript-transient--run-with-state
404
+ baseline
405
+ (lambda ()
406
+ (eliscript-transient--analyze-expression
407
+ (caddr arguments) scope value-context)))
408
+ baseline)))
409
+ (setq eliscript-transient--state
410
+ (eliscript-transient--merge-state
411
+ baseline (list then-state else-state)))))
412
+
413
+ (defun eliscript-transient--analyze-when
414
+ (arguments scope value-context)
415
+ "Analyze WHEN or UNLESS ARGUMENTS with an implicit unchanged branch."
416
+ (eliscript-transient--analyze-expression (car arguments) scope t)
417
+ (let* ((baseline (eliscript-transient--copy-state))
418
+ (body-state
419
+ (eliscript-transient--run-with-state
420
+ baseline
421
+ (lambda ()
422
+ (eliscript-transient--analyze-sequence
423
+ (cdr arguments) scope value-context)))))
424
+ (setq eliscript-transient--state
425
+ (eliscript-transient--merge-state
426
+ baseline (list baseline body-state)))))
427
+
428
+ (defun eliscript-transient--analyze-short-circuit
429
+ (arguments scope value-context)
430
+ "Analyze short-circuit ARGUMENTS and merge every possible stop point."
431
+ (let (exits)
432
+ (dolist (argument arguments)
433
+ (eliscript-transient--analyze-expression argument scope value-context)
434
+ (push (eliscript-transient--copy-state) exits))
435
+ (when exits
436
+ (let ((baseline (car (last exits))))
437
+ (setq eliscript-transient--state
438
+ (eliscript-transient--merge-state baseline exits))))))
439
+
440
+ (defun eliscript-transient--analyze-cond
441
+ (clauses scope value-context)
442
+ "Analyze COND CLAUSES with sequential test effects and merged exits."
443
+ (let ((baseline (eliscript-transient--copy-state))
444
+ exits default-seen)
445
+ (while (and clauses (not default-seen))
446
+ (let ((clause (pop clauses)))
447
+ (let* ((items (eliscript-form-value clause))
448
+ (test (car items)))
449
+ (eliscript-transient--analyze-expression test scope t)
450
+ (let* ((after-test (eliscript-transient--copy-state))
451
+ (body-state
452
+ (eliscript-transient--run-with-state
453
+ after-test
454
+ (lambda ()
455
+ (eliscript-transient--analyze-sequence
456
+ (cdr items) scope value-context)))))
457
+ (push body-state exits)
458
+ (when (eq (eliscript-form-value test) t)
459
+ (setq default-seen t))))))
460
+ (unless default-seen
461
+ (push (eliscript-transient--copy-state) exits))
462
+ (when exits
463
+ (setq eliscript-transient--state
464
+ (eliscript-transient--merge-state baseline exits)))))
465
+
466
+ (defun eliscript-transient--try-clause-p (form operator)
467
+ "Return non-nil when FORM is a TRY clause headed by OPERATOR."
468
+ (let ((value (eliscript-form-value form)))
469
+ (and (consp value)
470
+ (eq (eliscript-form-value (car value)) operator))))
471
+
472
+ (defun eliscript-transient--analyze-try (arguments scope value-context)
473
+ "Analyze TRY ARGUMENTS with conservative exceptional state merging."
474
+ (let ((baseline (eliscript-transient--copy-state))
475
+ body catch-clause finally-clause)
476
+ (dolist (argument arguments)
477
+ (cond
478
+ ((eliscript-transient--try-clause-p argument 'catch)
479
+ (setq catch-clause argument))
480
+ ((eliscript-transient--try-clause-p argument 'finally)
481
+ (setq finally-clause argument))
482
+ (t (push argument body))))
483
+ (setq body (nreverse body))
484
+ (let* ((eliscript-transient--try-prefix-states
485
+ (list (copy-hash-table baseline)))
486
+ (normal-state
487
+ (eliscript-transient--run-with-state
488
+ baseline
489
+ (lambda ()
490
+ (eliscript-transient--analyze-sequence
491
+ body scope value-context)))))
492
+ (let* ((exception-state
493
+ (eliscript-transient--merge-state
494
+ baseline eliscript-transient--try-prefix-states))
495
+ (catch-state
496
+ (if catch-clause
497
+ (let* ((items (eliscript-form-value catch-clause))
498
+ (child (eliscript-transient--make-scope scope)))
499
+ (eliscript-transient--declare-pattern child (cadr items))
500
+ (eliscript-transient--run-with-state
501
+ exception-state
502
+ (lambda ()
503
+ (eliscript-transient--analyze-sequence
504
+ (cddr items) child value-context))))
505
+ exception-state)))
506
+ (setq eliscript-transient--state
507
+ (eliscript-transient--merge-state
508
+ baseline (list normal-state catch-state)))))
509
+ (when finally-clause
510
+ (eliscript-transient--analyze-sequence
511
+ (cdr (eliscript-form-value finally-clause)) scope nil))))
512
+
513
+ (defun eliscript-transient--visible-active-resources ()
514
+ "Return active or maybe-active resources owned by the current function."
515
+ (let (resources)
516
+ (maphash
517
+ (lambda (resource state)
518
+ (when (and (= (eliscript-transient--resource-function-id resource)
519
+ eliscript-transient--function-id)
520
+ (memq state '(active maybe)))
521
+ (push resource resources)))
522
+ eliscript-transient--state)
523
+ (sort resources
524
+ (lambda (left right)
525
+ (string-lessp
526
+ (symbol-name (eliscript-transient--resource-name left))
527
+ (symbol-name (eliscript-transient--resource-name right)))))))
528
+
529
+ (defun eliscript-transient--analyze-await (arguments scope)
530
+ "Analyze AWAIT ARGUMENTS and reject live ownership at suspension."
531
+ (eliscript-transient--analyze-expression (car arguments) scope t)
532
+ (let ((active (eliscript-transient--visible-active-resources)))
533
+ (when active
534
+ (eliscript-transient--fail
535
+ "transient binding %s cannot remain active across await"
536
+ (eliscript-transient--resource-name (car active))))))
537
+
538
+ (defun eliscript-transient--analyze-operation
539
+ (operation form arguments scope value-context)
540
+ "Analyze transient OPERATION call FORM with ARGUMENTS in SCOPE."
541
+ (pcase operation
542
+ ('construct
543
+ (eliscript-transient--fail
544
+ "transient result must be bound directly to one symbol"))
545
+ ('update
546
+ (unless arguments
547
+ (eliscript-transient--fail
548
+ "transient update requires a directly bound transient value"))
549
+ (let ((eliscript-transient--current-span
550
+ (or (eliscript-form-span (car arguments))
551
+ (eliscript-form-span form))))
552
+ (eliscript-transient--direct-resource (car arguments) scope))
553
+ (when value-context
554
+ (eliscript-transient--fail
555
+ "transient update result cannot be used as an ordinary value"))
556
+ (dolist (argument (cdr arguments))
557
+ (eliscript-transient--analyze-expression argument scope t)))
558
+ ('complete
559
+ (unless (= (length arguments) 1)
560
+ (eliscript-transient--fail "persistent! expects 1 argument"))
561
+ (let* ((argument (car arguments))
562
+ (eliscript-transient--current-span
563
+ (or (eliscript-form-span argument) (eliscript-form-span form)))
564
+ (resource (eliscript-transient--direct-resource argument scope)))
565
+ (when (memq resource eliscript-transient--loop-protected)
566
+ (eliscript-transient--fail
567
+ "transient binding %s cannot be completed inside a repeated loop"
568
+ (eliscript-transient--resource-name resource)))
569
+ (puthash resource 'consumed eliscript-transient--state)))))
570
+
571
+ (defun eliscript-transient--analyze-loop
572
+ (arguments scope value-context)
573
+ "Analyze LOOP ARGUMENTS and protect resources visible before repetition."
574
+ (let* ((baseline (eliscript-transient--copy-state))
575
+ (bindings (eliscript-form-value (car arguments)))
576
+ (child (eliscript-transient--make-scope scope)))
577
+ (dolist (entry bindings)
578
+ (pcase-let* ((`(,target ,initializer)
579
+ (eliscript-transient--binding-pair entry))
580
+ (declared
581
+ (eliscript-transient--declare-pattern child target)))
582
+ (eliscript-transient--analyze-initializer initializer scope declared)))
583
+ (let ((eliscript-transient--loop-protected
584
+ (append (eliscript-transient--visible-active-resources)
585
+ eliscript-transient--loop-protected)))
586
+ (eliscript-transient--analyze-sequence
587
+ (cdr arguments) child value-context))
588
+ (eliscript-transient--retain-state-keys baseline)))
589
+
590
+ (defun eliscript-transient--analyze-while
591
+ (arguments scope value-context)
592
+ "Analyze WHILE ARGUMENTS with a conservative repeated-loop boundary."
593
+ (eliscript-transient--analyze-expression (car arguments) scope t)
594
+ (let* ((baseline (eliscript-transient--copy-state))
595
+ (eliscript-transient--loop-protected
596
+ (append (eliscript-transient--visible-active-resources)
597
+ eliscript-transient--loop-protected))
598
+ (body-state
599
+ (eliscript-transient--run-with-state
600
+ baseline
601
+ (lambda ()
602
+ (eliscript-transient--analyze-sequence
603
+ (cdr arguments) scope nil)))))
604
+ (setq eliscript-transient--state
605
+ (eliscript-transient--merge-state
606
+ baseline (list baseline body-state)))
607
+ (ignore value-context)))
608
+
609
+ (defun eliscript-transient--analyze-assignment (arguments scope)
610
+ "Analyze assignment ARGUMENTS and reject transient rebinding."
611
+ (while arguments
612
+ (let* ((name (pop arguments))
613
+ (value (pop arguments))
614
+ (binding
615
+ (eliscript-transient--lookup scope (eliscript-form-value name))))
616
+ (when (and binding (eliscript-transient--binding-resource binding))
617
+ (let ((eliscript-transient--current-span
618
+ (or (eliscript-form-span name)
619
+ eliscript-transient--current-span)))
620
+ (eliscript-transient--fail
621
+ "transient binding %s cannot be reassigned"
622
+ (eliscript-form-value name))))
623
+ (eliscript-transient--analyze-expression value scope t))))
624
+
625
+ (defun eliscript-transient--analyze-call (form scope value-context)
626
+ "Analyze call FORM in SCOPE under VALUE-CONTEXT."
627
+ (let* ((items (eliscript-form-value form))
628
+ (operator-form (car items))
629
+ (operator (eliscript-form-value operator-form))
630
+ (arguments (cdr items))
631
+ (operation-binding
632
+ (eliscript-transient--operator-binding operator-form scope)))
633
+ (if operation-binding
634
+ (eliscript-transient--analyze-operation
635
+ (eliscript-transient--binding-operation operation-binding)
636
+ form arguments scope value-context)
637
+ (pcase operator
638
+ ((or 'lambda 'fn 'async)
639
+ (eliscript-transient--analyze-function
640
+ (car arguments) (cdr arguments) scope))
641
+ ('await (eliscript-transient--analyze-await arguments scope))
642
+ ((or 'quote 'js*) nil)
643
+ ('let (eliscript-transient--analyze-let
644
+ arguments scope nil value-context))
645
+ ('let* (eliscript-transient--analyze-let
646
+ arguments scope t value-context))
647
+ ('loop (eliscript-transient--analyze-loop
648
+ arguments scope value-context))
649
+ ('while (eliscript-transient--analyze-while
650
+ arguments scope value-context))
651
+ ((or 'setq 'set!)
652
+ (eliscript-transient--analyze-assignment arguments scope))
653
+ ('if (eliscript-transient--analyze-if
654
+ arguments scope value-context))
655
+ ((or 'when 'unless)
656
+ (eliscript-transient--analyze-when
657
+ arguments scope value-context))
658
+ ((or 'and 'or)
659
+ (eliscript-transient--analyze-short-circuit
660
+ arguments scope value-context))
661
+ ('cond (eliscript-transient--analyze-cond
662
+ arguments scope value-context))
663
+ ('try (eliscript-transient--analyze-try
664
+ arguments scope value-context))
665
+ ((or 'progn 'do)
666
+ (eliscript-transient--analyze-sequence
667
+ arguments scope value-context))
668
+ (_
669
+ (let ((operator-binding
670
+ (and (symbolp operator)
671
+ (eliscript-transient--lookup scope operator))))
672
+ (when (and operator-binding
673
+ (or (eliscript-transient--binding-operation
674
+ operator-binding)
675
+ (eliscript-transient--binding-resource
676
+ operator-binding)))
677
+ (eliscript-transient--analyze-reference operator-form scope)))
678
+ (dolist (argument arguments)
679
+ (eliscript-transient--analyze-expression argument scope t)))))))
680
+
681
+ (defun eliscript-transient--analyze-expression (form scope value-context)
682
+ "Analyze expression FORM in SCOPE under VALUE-CONTEXT."
683
+ (when form
684
+ (let* ((eliscript-transient--current-span
685
+ (or (eliscript-form-span form)
686
+ eliscript-transient--current-span))
687
+ (value (eliscript-form-value form)))
688
+ (eliscript-transient--record-try-prefix)
689
+ (cond
690
+ ((symbolp value)
691
+ (eliscript-transient--analyze-reference form scope))
692
+ ((vectorp value)
693
+ (dolist (item (append value nil))
694
+ (eliscript-transient--analyze-expression item scope t)))
695
+ ((consp value)
696
+ (eliscript-transient--analyze-call form scope value-context)))
697
+ (eliscript-transient--record-try-prefix))))
698
+
699
+ (defun eliscript-transient--import-declarations (arguments scope)
700
+ "Declare import ARGUMENTS in SCOPE with canonical transient provenance."
701
+ (let* ((source (eliscript-form-value (car arguments)))
702
+ (family (eliscript-transient--canonical-module-family source))
703
+ (specifiers (cdr arguments)))
704
+ (while specifiers
705
+ (let* ((specifier-form (pop specifiers))
706
+ (specifier (eliscript-form-value specifier-form)))
707
+ (pcase specifier
708
+ ((or :default :as)
709
+ (let ((name (pop specifiers)))
710
+ (eliscript-transient--declare
711
+ scope name nil (and (eq specifier :as) family))))
712
+ ((pred symbolp)
713
+ (eliscript-transient--declare
714
+ scope specifier-form
715
+ (and family
716
+ (eliscript-transient--operation family specifier)))))))))
717
+
718
+ (defun eliscript-transient--flatten-modules (forms)
719
+ "Return FORMS with module wrappers removed."
720
+ (apply
721
+ #'append
722
+ (mapcar
723
+ (lambda (form)
724
+ (let ((value (eliscript-form-value form)))
725
+ (if (and (consp value)
726
+ (eq (eliscript-form-value (car value)) 'module))
727
+ (eliscript-transient--flatten-modules (cddr value))
728
+ (list form))))
729
+ forms)))
730
+
731
+ (defun eliscript-transient--predeclare-top-level (forms scope)
732
+ "Predeclare top-level names and canonical transient resources."
733
+ (dolist (form forms)
734
+ (let ((value (eliscript-form-value form)))
735
+ (when (consp value)
736
+ (pcase (eliscript-form-value (car value))
737
+ ((or 'import 'import-portable)
738
+ (eliscript-transient--import-declarations (cdr value) scope))
739
+ ((or 'defvar 'defconst 'defun 'defn 'defportable 'defasync)
740
+ (eliscript-transient--declare scope (cadr value)))))))
741
+ (dolist (form forms)
742
+ (let* ((value (eliscript-form-value form))
743
+ (operator (and (consp value)
744
+ (eliscript-form-value (car value)))))
745
+ (when (and (memq operator '(defvar defconst))
746
+ (caddr value)
747
+ (eliscript-transient--constructor-call-p
748
+ (caddr value) scope))
749
+ (let ((binding
750
+ (eliscript-transient--lookup
751
+ scope (eliscript-form-value (cadr value)))))
752
+ (eliscript-transient--new-resource
753
+ binding (eliscript-form-span (caddr value))))))))
754
+
755
+ (defun eliscript-transient--check-export (name scope)
756
+ "Reject exporting transient binding NAME from SCOPE."
757
+ (let* ((value (eliscript-form-value name))
758
+ (binding (and (symbolp value)
759
+ (eliscript-transient--lookup scope value))))
760
+ (when (and binding (eliscript-transient--binding-resource binding))
761
+ (let ((eliscript-transient--current-span
762
+ (or (eliscript-form-span name)
763
+ eliscript-transient--current-span)))
764
+ (eliscript-transient--fail
765
+ "transient binding %s cannot be exported" value)))))
766
+
767
+ (defun eliscript-transient--analyze-top-level (form scope)
768
+ "Analyze top-level FORM in transient ownership SCOPE."
769
+ (let* ((eliscript-transient--current-span
770
+ (or (eliscript-form-span form)
771
+ eliscript-transient--current-span))
772
+ (value (eliscript-form-value form))
773
+ (operator (and (consp value)
774
+ (eliscript-form-value (car value))))
775
+ (arguments (and (consp value) (cdr value))))
776
+ (pcase operator
777
+ ((or 'import 'import-portable) nil)
778
+ ((or 'defvar 'defconst)
779
+ (let* ((binding
780
+ (eliscript-transient--lookup
781
+ scope (eliscript-form-value (car arguments))))
782
+ (initializer (cadr arguments)))
783
+ (eliscript-transient--analyze-initializer
784
+ initializer scope (list binding) t)))
785
+ ((or 'defun 'defn 'defportable 'defasync)
786
+ (eliscript-transient--analyze-function
787
+ (cadr arguments) (cddr arguments) scope))
788
+ ('export
789
+ (dolist (name arguments)
790
+ (eliscript-transient--check-export name scope)))
791
+ ('export-default
792
+ (let ((argument (car arguments)))
793
+ (eliscript-transient--check-export argument scope)
794
+ (eliscript-transient--analyze-expression argument scope t)))
795
+ (_ (eliscript-transient--analyze-expression form scope nil)))))
796
+
797
+ (defun eliscript-transient--analyze-module (forms &optional filename)
798
+ "Validate transient ownership and escape rules in expanded FORMS."
799
+ (if (eliscript-transient--trusted-core-source-p filename)
800
+ forms
801
+ (let* ((eliscript-transient--filename filename)
802
+ (eliscript-transient--current-span nil)
803
+ (eliscript-transient--state (make-hash-table :test #'eq))
804
+ (eliscript-transient--function-id 0)
805
+ (eliscript-transient--next-function-id 0)
806
+ (eliscript-transient--loop-protected nil)
807
+ (eliscript-transient--try-prefix-states nil)
808
+ (flattened (eliscript-transient--flatten-modules forms))
809
+ (scope (eliscript-transient--make-scope)))
810
+ (eliscript-transient--predeclare-top-level flattened scope)
811
+ (dolist (form flattened)
812
+ (eliscript-transient--analyze-top-level form scope))
813
+ forms)))
814
+
815
+ (provide 'eliscript-transient-analysis)
816
+
817
+ ;;; eliscript-transient-analysis.el ends here