eliscript 0.0.4 → 0.1.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 (192) hide show
  1. package/README.md +9 -8
  2. package/bootstrap/compiler/emitter.eli +161 -63
  3. package/bootstrap/host/check-cli.mjs +0 -1
  4. package/bootstrap/host/check-cli.mjs.map +1 -1
  5. package/bootstrap/host/compiler-session.eli +2 -1
  6. package/bootstrap/host/eliscript-cli.mjs +2 -9
  7. package/bootstrap/host/eliscript-cli.mjs.map +1 -1
  8. package/bootstrap/host/project-cli.mjs +1 -11
  9. package/bootstrap/host/project-cli.mjs.map +1 -1
  10. package/bootstrap/host/project.mjs +2 -8
  11. package/bootstrap/host/project.mjs.map +1 -1
  12. package/browser/README.md +1 -1
  13. package/compiler/README.md +2 -2
  14. package/compiler/eliscript-emitter.el +229 -85
  15. package/compiler/eliscript-ir-emitter.el +64 -34
  16. package/compiler/eliscript-project.el +20 -5
  17. package/dist/backends/async.mjs +1 -90
  18. package/dist/backends/async.mjs.map +1 -1
  19. package/dist/backends/binary.mjs +1 -91
  20. package/dist/backends/binary.mjs.map +1 -1
  21. package/dist/backends/capabilities.mjs +1 -90
  22. package/dist/backends/capabilities.mjs.map +1 -1
  23. package/dist/backends/elf.mjs +1 -90
  24. package/dist/backends/elf.mjs.map +1 -1
  25. package/dist/backends/managed.mjs +1 -94
  26. package/dist/backends/managed.mjs.map +1 -1
  27. package/dist/backends/memory.mjs +1 -90
  28. package/dist/backends/memory.mjs.map +1 -1
  29. package/dist/backends/native.mjs +1 -91
  30. package/dist/backends/native.mjs.map +1 -1
  31. package/dist/backends/numeric.mjs +1 -90
  32. package/dist/backends/numeric.mjs.map +1 -1
  33. package/dist/backends/optimize.mjs +1 -90
  34. package/dist/backends/optimize.mjs.map +1 -1
  35. package/dist/backends/regexp.mjs +1 -91
  36. package/dist/backends/regexp.mjs.map +1 -1
  37. package/dist/backends/sha256.mjs +2 -96
  38. package/dist/backends/sha256.mjs.map +1 -1
  39. package/dist/backends/wasm.mjs +1 -90
  40. package/dist/backends/wasm.mjs.map +1 -1
  41. package/dist/bootstrap/analyzer.mjs +1 -95
  42. package/dist/bootstrap/analyzer.mjs.map +1 -1
  43. package/dist/bootstrap/compiler.mjs +1 -105
  44. package/dist/bootstrap/compiler.mjs.map +1 -1
  45. package/dist/bootstrap/emitter.mjs +848 -807
  46. package/dist/bootstrap/emitter.mjs.map +1 -1
  47. package/dist/bootstrap/expander.mjs +1 -96
  48. package/dist/bootstrap/expander.mjs.map +1 -1
  49. package/dist/bootstrap/formatter.mjs +1 -104
  50. package/dist/bootstrap/formatter.mjs.map +1 -1
  51. package/dist/bootstrap/ir.mjs +18 -172
  52. package/dist/bootstrap/ir.mjs.map +1 -1
  53. package/dist/bootstrap/lower.mjs +1 -92
  54. package/dist/bootstrap/lower.mjs.map +1 -1
  55. package/dist/bootstrap/project.mjs +23 -209
  56. package/dist/bootstrap/project.mjs.map +1 -1
  57. package/dist/bootstrap/reader.mjs +1 -96
  58. package/dist/bootstrap/reader.mjs.map +1 -1
  59. package/dist/bootstrap/source-map.mjs +7 -112
  60. package/dist/bootstrap/source-map.mjs.map +1 -1
  61. package/dist/bootstrap/symbol.mjs +7 -114
  62. package/dist/bootstrap/symbol.mjs.map +1 -1
  63. package/dist/bootstrap/syntax.mjs +10 -176
  64. package/dist/bootstrap/syntax.mjs.map +1 -1
  65. package/dist/bootstrap/transient-analysis.mjs +1 -93
  66. package/dist/bootstrap/transient-analysis.mjs.map +1 -1
  67. package/dist/browser/compiler.js +105 -104
  68. package/dist/browser/loader.js +1 -1
  69. package/dist/browser/loader.mjs +5 -124
  70. package/dist/compiler-artifact.json +1 -1
  71. package/dist/host/compiler-session.mjs +205 -297
  72. package/dist/host/compiler-session.mjs.map +1 -1
  73. package/dist/host/compiler-tools.mjs +1 -91
  74. package/dist/host/compiler-tools.mjs.map +1 -1
  75. package/dist/literal-sources/runtime/core/order.eli +32 -10
  76. package/dist/literal-sources/runtime/core/sequence.eli +69 -28
  77. package/dist/literal-sources/runtime/core/vector.eli +2 -4
  78. package/dist/literal-sources/stdlib/managed-literal-protocols.eli +5 -5
  79. package/dist/literal-sources/stdlib/managed-literals.eli +1 -1
  80. package/dist/package.json +1 -0
  81. package/dist/runtime/math.mjs +62 -252
  82. package/dist/runtime/math.mjs.map +1 -1
  83. package/dist/runtime/prelude.mjs +95 -0
  84. package/dist/runtime/prelude.mjs.macro-origins.json +1 -0
  85. package/dist/runtime/prelude.mjs.map +1 -0
  86. package/dist/runtime/unicode-case-data.mjs +1 -95
  87. package/dist/runtime/unicode-case-data.mjs.map +1 -1
  88. package/dist/runtime/unicode-case.mjs +3 -97
  89. package/dist/runtime/unicode-case.mjs.map +1 -1
  90. package/dist/stage3-release.json +1 -1
  91. package/editor/README.md +9 -4
  92. package/editor/eliscript-abi.el +170 -0
  93. package/editor/eliscript-async.el +375 -0
  94. package/editor/eliscript-bind.el +822 -0
  95. package/editor/eliscript-compat.el +346 -0
  96. package/editor/eliscript-contract.el +334 -0
  97. package/editor/eliscript-data.el +1544 -0
  98. package/editor/eliscript-js.el +318 -0
  99. package/editor/eliscript-mode.el +1 -1
  100. package/editor/eliscript-module.el +340 -0
  101. package/editor/eliscript-repl.el +1 -1
  102. package/package.json +4 -1
  103. package/runtime/README.md +21 -1
  104. package/runtime/core/protocol-impl.mjs +1 -115
  105. package/runtime/core/protocol-impl.mjs.map +1 -1
  106. package/runtime/core/text-impl.mjs +1 -90
  107. package/runtime/core/text-impl.mjs.map +1 -1
  108. package/runtime/core/vector.eli +1 -1
  109. package/runtime/core/vector.mjs +0 -9
  110. package/runtime/emacs-compat.eli +5957 -0
  111. package/runtime/emacs-compat.mjs +13734 -0
  112. package/runtime/emacs-compat.mjs.map +1 -0
  113. package/runtime/math.mjs +62 -252
  114. package/runtime/math.mjs.map +1 -1
  115. package/runtime/prelude.eli +4 -0
  116. package/runtime/prelude.mjs +95 -0
  117. package/runtime/prelude.mjs.map +1 -0
  118. package/runtime/unicode-case-data.mjs +1 -95
  119. package/runtime/unicode-case-data.mjs.map +1 -1
  120. package/runtime/unicode-case.mjs +3 -97
  121. package/runtime/unicode-case.mjs.map +1 -1
  122. package/stdlib/managed-literals.eli +1 -1
  123. package/stdlib/managed-runtime-vector.eli +2 -4
  124. package/tools/backends/compiler-build.mjs +3 -93
  125. package/tools/backends/compiler-cli.mjs +2 -15
  126. package/tools/backends/compiler-corpus-bit.mjs +2 -95
  127. package/tools/backends/compiler-corpus-cli-adapters.mjs +1 -97
  128. package/tools/backends/compiler-corpus-debugging.mjs +2 -95
  129. package/tools/backends/compiler-corpus-emitter.mjs +1 -97
  130. package/tools/backends/compiler-corpus-execution.mjs +2 -108
  131. package/tools/backends/compiler-corpus-fuzz.mjs +2 -95
  132. package/tools/backends/compiler-corpus-host-context.mjs +2 -99
  133. package/tools/backends/compiler-corpus-identity-host.mjs +18 -123
  134. package/tools/backends/compiler-corpus-identity-value-adapters.mjs +0 -9
  135. package/tools/backends/compiler-corpus-literal-provider.mjs +1 -91
  136. package/tools/backends/compiler-corpus-managed-adapters.mjs +1 -99
  137. package/tools/backends/compiler-corpus-managed-fixtures.mjs +2 -113
  138. package/tools/backends/compiler-corpus-matchers.mjs +3 -106
  139. package/tools/backends/compiler-corpus-original-program.mjs +1 -91
  140. package/tools/backends/compiler-corpus-process.mjs +1 -92
  141. package/tools/backends/compiler-corpus-program-driver.mjs +1 -3
  142. package/tools/backends/compiler-corpus-project-adapters.mjs +1 -95
  143. package/tools/backends/compiler-corpus-project-entry-host.mjs +2 -92
  144. package/tools/backends/compiler-corpus-project-transport.mjs +1 -90
  145. package/tools/backends/compiler-corpus-raw-fixture-observer.mjs +1 -91
  146. package/tools/backends/compiler-corpus-raw-language-fixtures.mjs +1 -92
  147. package/tools/backends/compiler-corpus-raw-math-fixture.mjs +1 -91
  148. package/tools/backends/compiler-corpus-raw-special-fixtures.mjs +1 -91
  149. package/tools/backends/compiler-corpus-reference.mjs +1 -90
  150. package/tools/backends/compiler-corpus-runtime-adapters.mjs +2 -110
  151. package/tools/backends/compiler-corpus-tools-cli.mjs +1 -95
  152. package/tools/backends/compiler-corpus-tools-reference.mjs +22 -163
  153. package/tools/backends/compiler-corpus.mjs +3 -125
  154. package/tools/backends/compiler-matrix.mjs +1 -92
  155. package/tools/backends/compiler-package-browser.mjs +1 -90
  156. package/tools/backends/compiler-package.mjs +3 -95
  157. package/tools/backends/compiler-unicode-profile-host.mjs +1 -91
  158. package/tools/backends/dist/bootstrap-js-stages.mjs +1 -91
  159. package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -1
  160. package/tools/backends/dist/managed-ir-inventory.mjs +1 -91
  161. package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -1
  162. package/tools/backends/dist/record-grid.mjs +1 -91
  163. package/tools/backends/dist/record-grid.mjs.map +1 -1
  164. package/tools/backends/dist/release-cli.mjs +4 -94
  165. package/tools/backends/dist/release-cli.mjs.map +1 -1
  166. package/tools/backends/dist/run-artifact.mjs +1 -91
  167. package/tools/backends/dist/run-artifact.mjs.map +1 -1
  168. package/tools/backends/eliscript/compiler-corpus-bit.eli +1 -0
  169. package/tools/backends/managed-foreign-binding.mjs +1 -95
  170. package/tools/backends/managed-host-lifecycle.mjs +1 -91
  171. package/tools/backends/managed-host-object-provider.mjs +18 -59
  172. package/tools/backends/managed-host-object-realm.mjs +1 -91
  173. package/tools/backends/managed-host-proxy.mjs +9 -18
  174. package/tools/backends/managed-host-runtime.mjs +3 -105
  175. package/tools/backends/managed-host.mjs +1 -104
  176. package/tools/backends/managed-module-graph.mjs +1 -97
  177. package/tools/backends/managed-runtime.mjs +1 -96
  178. package/tools/backends/managed-source-view.mjs +13 -108
  179. package/tools/backends/native-managed-runtime.mjs +1 -91
  180. package/tools/backends/native-memory.mjs +1 -91
  181. package/tools/backends/stage3-targets.mjs +1 -91
  182. package/tools/bootstrap-runtime.el +3 -2
  183. package/tools/project/project.eli +1 -1
  184. package/tools/testing/stdlib-bit-corpus-host.mjs +1 -90
  185. package/tools/testing/stdlib-bit.mjs +1 -90
  186. package/dist/bootstrap/package.json +0 -1
  187. package/dist/bootstrap/runtime/math.mjs +0 -1117
  188. package/dist/bootstrap/runtime/math.mjs.map +0 -1
  189. package/dist/bootstrap/runtime/unicode-case-data.mjs +0 -10125
  190. package/dist/bootstrap/runtime/unicode-case-data.mjs.map +0 -1
  191. package/dist/bootstrap/runtime/unicode-case.mjs +0 -281
  192. package/dist/bootstrap/runtime/unicode-case.mjs.map +0 -1
@@ -0,0 +1,822 @@
1
+ ;;; eliscript-bind.el --- Unified pcase destructuring, multi-arity, let-const, recur, and pipelines -*- lexical-binding: t; -*-
2
+
3
+ ;;; Commentary:
4
+
5
+ ;; Implements `eliscript-bind` for Eliscript 0.1.0:
6
+ ;; - Unified destructuring (`let-bind`, `let*-bind`, `defun-bind`, `lambda-bind`, `pcase-value`, `cond-predicate`)
7
+ ;; - Multi-arity functions (`defun-case`, `lambda-case`)
8
+ ;; - Immutable local bindings (`let-const`, `let*-const`)
9
+ ;; - Stack-safe explicit tail recursion (`let-loop`, `defun-tail`, `lambda-tail`, `recur`, `trampoline`)
10
+ ;; - Pipeline macros (`thread-first`, `thread-last`, `thread-as`, `thread-cond-first`, `thread-cond-last`, `thread-some-first`, `thread-some-last`)
11
+ ;; - Function combinators (`fn-compose`, `fn-partial`, `fn-juxt`, `fn-complement`)
12
+
13
+ ;;; Code:
14
+
15
+ (require 'cl-lib)
16
+ (require 'subr-x)
17
+ (require 'pcase)
18
+ (require 'seq)
19
+ (require 'eliscript-contract)
20
+ (require 'eliscript-data)
21
+
22
+ ;; ============================================================================
23
+ ;; 1. Unified Destructuring Pattern Compiler & Runtime Matcher
24
+ ;; ============================================================================
25
+
26
+ (defun eliscript-bind--collect-pattern-vars (pattern)
27
+ "Return all bound variable symbols in PATTERN, checking for duplicates."
28
+ (let ((vars nil))
29
+ (cl-labels
30
+ ((walk (pat)
31
+ (cond
32
+ ((null pat) nil)
33
+ ((eq pat '_) nil)
34
+ ((eq pat :none) nil)
35
+ ((keywordp pat) nil)
36
+ ((symbolp pat)
37
+ (when (memq pat vars)
38
+ (signal 'eliscript-compile-error
39
+ (list (format "ELI-B0010: duplicate variable `%S' in destructuring pattern" pat))))
40
+ (push pat vars))
41
+ ((vectorp pat)
42
+ (let ((elems (append pat nil))
43
+ (i 0)
44
+ (len (length pat)))
45
+ (while (< i len)
46
+ (let ((el (nth i elems)))
47
+ (if (eq el '&rest)
48
+ (progn
49
+ (walk (nth (1+ i) elems))
50
+ (setq i len))
51
+ (walk el)
52
+ (setq i (1+ i)))))))
53
+ ((consp pat)
54
+ (let ((head (car pat)))
55
+ (cond
56
+ ((eq head :list)
57
+ (let ((cursor (cdr pat)))
58
+ (while cursor
59
+ (if (eq (car cursor) '&rest)
60
+ (progn
61
+ (walk (cadr cursor))
62
+ (setq cursor nil))
63
+ (walk (car cursor))
64
+ (setq cursor (cdr cursor))))))
65
+ ((eq head :map)
66
+ (dolist (entry (cdr pat))
67
+ (let ((subpat (cadr entry)))
68
+ (if (and (consp subpat) (eq (car subpat) 'default))
69
+ (walk (cadr subpat))
70
+ (walk subpat)))))
71
+ ((eq head :record)
72
+ (dolist (entry (cddr pat))
73
+ (walk (cadr entry))))
74
+ ((memq head '(:ok :error :some))
75
+ (walk (cadr pat)))
76
+ (t nil)))))))
77
+ (walk pattern))
78
+ (nreverse vars)))
79
+
80
+ (defun eliscript-bind--match-runtime (pattern value env-ht eval-default-fn)
81
+ "Match PATTERN against VALUE, storing variable bindings in ENV-HT.
82
+ Signal an error if VALUE does not match PATTERN."
83
+ (cond
84
+ ((eq pattern '_) t)
85
+ ((eq pattern :none)
86
+ (unless (option-none-p value)
87
+ (error "Destructuring expected option-none, got %S" value))
88
+ t)
89
+ ((and (symbolp pattern) (not (keywordp pattern)) (not (null pattern)))
90
+ (puthash pattern value env-ht)
91
+ t)
92
+ ((vectorp pattern)
93
+ (unless (or (vectorp value) (persistent-vector-p value))
94
+ (error "Destructuring vector pattern %S requires vector or persistent-vector, got %S"
95
+ pattern value))
96
+ (let* ((pat-list (append pattern nil))
97
+ (rest-pos (cl-position '&rest pat-list))
98
+ (val-len (value-count value)))
99
+ (if rest-pos
100
+ (let ((fixed-count rest-pos)
101
+ (rest-pat (nth (1+ rest-pos) pat-list)))
102
+ (when (< val-len fixed-count)
103
+ (error "Destructuring vector pattern %S expected at least %d elements, got %d"
104
+ pattern fixed-count val-len))
105
+ (dotimes (i fixed-count)
106
+ (eliscript-bind--match-runtime
107
+ (nth i pat-list) (value-nth value i) env-ht eval-default-fn))
108
+ (let ((tail-val (if (persistent-vector-p value)
109
+ (seq-subseq value fixed-count)
110
+ (cl-subseq value fixed-count))))
111
+ (eliscript-bind--match-runtime rest-pat tail-val env-ht eval-default-fn)))
112
+ (unless (= val-len (length pat-list))
113
+ (error "Destructuring vector pattern %S expected %d elements, got %d"
114
+ pattern (length pat-list) val-len))
115
+ (dotimes (i val-len)
116
+ (eliscript-bind--match-runtime
117
+ (nth i pat-list) (value-nth value i) env-ht eval-default-fn))))
118
+ t)
119
+ ((and (consp pattern) (eq (car pattern) :list))
120
+ (unless (or (listp value) (persistent-list-p value))
121
+ (error "Destructuring :list pattern %S requires list or persistent-list, got %S"
122
+ pattern value))
123
+ (let* ((pat-list (cdr pattern))
124
+ (rest-pos (cl-position '&rest pat-list))
125
+ (elems (if (persistent-list-p value)
126
+ (eliscript-data--seq-elements value)
127
+ value))
128
+ (val-len (length elems)))
129
+ (if rest-pos
130
+ (let ((fixed-count rest-pos)
131
+ (rest-pat (nth (1+ rest-pos) pat-list)))
132
+ (when (< val-len fixed-count)
133
+ (error "Destructuring :list pattern expected at least %d elements, got %d"
134
+ fixed-count val-len))
135
+ (dotimes (i fixed-count)
136
+ (eliscript-bind--match-runtime
137
+ (nth i pat-list) (nth i elems) env-ht eval-default-fn))
138
+ (let* ((tail-elems (nthcdr fixed-count elems))
139
+ (tail-val (if (persistent-list-p value)
140
+ (apply #'persistent-list tail-elems)
141
+ tail-elems)))
142
+ (eliscript-bind--match-runtime rest-pat tail-val env-ht eval-default-fn)))
143
+ (unless (= val-len (length pat-list))
144
+ (error "Destructuring :list pattern expected %d elements, got %d"
145
+ (length pat-list) val-len))
146
+ (dotimes (i val-len)
147
+ (eliscript-bind--match-runtime
148
+ (nth i pat-list) (nth i elems) env-ht eval-default-fn))))
149
+ t)
150
+ ((and (consp pattern) (eq (car pattern) :map))
151
+ (unless (or (hash-table-p value)
152
+ (persistent-map-p value)
153
+ (persistent-sorted-map-p value)
154
+ (eliscript-persistent-record-p value))
155
+ (error "Destructuring :map pattern requires map or persistent struct, got %S" value))
156
+ (dolist (entry (cdr pattern))
157
+ (let ((key (car entry))
158
+ (subpat (cadr entry)))
159
+ (if (and (consp subpat) (eq (car subpat) 'default))
160
+ (let ((target-pat (cadr subpat))
161
+ (def-expr (nth 2 subpat)))
162
+ (if (value-contains-p value key)
163
+ (eliscript-bind--match-runtime
164
+ target-pat (value-get value key) env-ht eval-default-fn)
165
+ (eliscript-bind--match-runtime
166
+ target-pat (funcall eval-default-fn def-expr) env-ht eval-default-fn)))
167
+ (unless (value-contains-p value key)
168
+ (error "Destructuring :map missing required key %S in %S" key value))
169
+ (eliscript-bind--match-runtime
170
+ subpat (value-get value key) env-ht eval-default-fn))))
171
+ t)
172
+ ((and (consp pattern) (eq (car pattern) :record))
173
+ (let ((rec-name (cadr pattern))
174
+ (entries (cddr pattern)))
175
+ (unless (and (eliscript-persistent-record-p value)
176
+ (eq (eliscript-persistent-record-type-name value) rec-name))
177
+ (error "Destructuring :record expected %S instance, got %S" rec-name value))
178
+ (dolist (entry entries)
179
+ (let ((key (car entry))
180
+ (subpat (cadr entry)))
181
+ (unless (value-contains-p value key)
182
+ (error "Destructuring :record %S missing field %S" rec-name key))
183
+ (eliscript-bind--match-runtime
184
+ subpat (value-get value key) env-ht eval-default-fn))))
185
+ t)
186
+ ((and (consp pattern) (eq (car pattern) :ok))
187
+ (unless (result-ok-p value)
188
+ (error "Destructuring :ok expected result-ok, got %S" value))
189
+ (eliscript-bind--match-runtime
190
+ (cadr pattern) (result-value value) env-ht eval-default-fn))
191
+ ((and (consp pattern) (eq (car pattern) :error))
192
+ (unless (result-error-p value)
193
+ (error "Destructuring :error expected result-error, got %S" value))
194
+ (eliscript-bind--match-runtime
195
+ (cadr pattern) (result-error-value value) env-ht eval-default-fn))
196
+ ((and (consp pattern) (eq (car pattern) :some))
197
+ (unless (option-some-p value)
198
+ (error "Destructuring :some expected option-some, got %S" value))
199
+ (eliscript-bind--match-runtime
200
+ (cadr pattern) (option-value value) env-ht eval-default-fn))
201
+ (t
202
+ (unless (value-equal-p pattern value)
203
+ (error "Destructuring literal mismatch: expected %S, got %S" pattern value))
204
+ t)))
205
+
206
+ (defun eliscript-bind--expand-single-binding (pattern expr body)
207
+ "Expand binding of PATTERN to EXPR around BODY."
208
+ (let ((vars (eliscript-bind--collect-pattern-vars pattern)))
209
+ (if (and (symbolp pattern) (not (keywordp pattern)) (not (eq pattern :none)))
210
+ (if (eq pattern '_)
211
+ `(progn ,expr ,@body)
212
+ `(let ((,pattern ,expr)) ,@body))
213
+ (let ((ht-sym (make-symbol "bind-env"))
214
+ (val-sym (make-symbol "bind-val")))
215
+ `(let ((,ht-sym (make-hash-table :test 'eq))
216
+ (,val-sym ,expr))
217
+ (eliscript-bind--match-runtime
218
+ ',pattern ,val-sym ,ht-sym #'eval)
219
+ (let ,(mapcar (lambda (v) `(,v (gethash ',v ,ht-sym))) vars)
220
+ ,@body))))))
221
+
222
+ (defmacro let*-bind (bindings &rest body)
223
+ "Bind destructuring BINDINGS sequentially and evaluate BODY."
224
+ (if (null bindings)
225
+ `(progn ,@body)
226
+ (let* ((first-b (car bindings))
227
+ (pat (car first-b))
228
+ (expr (cadr first-b)))
229
+ (eliscript-bind--expand-single-binding
230
+ pat expr `((let*-bind ,(cdr bindings) ,@body))))))
231
+
232
+ (defmacro let-bind (bindings &rest body)
233
+ "Bind destructuring BINDINGS in parallel and evaluate BODY."
234
+ (let ((temp-lets nil)
235
+ (seq-binds nil)
236
+ (all-vars nil))
237
+ (dolist (b bindings)
238
+ (let ((pat (car b))
239
+ (expr (cadr b))
240
+ (tmp (make-symbol "let-bind-tmp")))
241
+ (dolist (v (eliscript-bind--collect-pattern-vars pat))
242
+ (when (memq v all-vars)
243
+ (signal 'eliscript-compile-error
244
+ (list (format "ELI-B0010: duplicate variable `%S' in let-bind" v))))
245
+ (push v all-vars))
246
+ (push `(,tmp ,expr) temp-lets)
247
+ (push `(,pat ,tmp) seq-binds)))
248
+ `(let ,(nreverse temp-lets)
249
+ (let*-bind ,(nreverse seq-binds)
250
+ ,@body))))
251
+
252
+ (defun eliscript-bind--build-destructuring-lambda (name arg-specs raw-body)
253
+ "Build a lambda form for NAME with destructuring ARG-SPECS and RAW-BODY."
254
+ (let* ((extracted (eliscript-contract-extract-declarations raw-body name))
255
+ (plist (plist-get extracted :eliscript))
256
+ (clean-body (plist-get extracted :body))
257
+ (req-pats nil)
258
+ (opt-specs nil)
259
+ (rest-pat nil)
260
+ (mode 'req)
261
+ (all-vars nil))
262
+ (dolist (item arg-specs)
263
+ (cond
264
+ ((eq item '&optional) (setq mode 'opt))
265
+ ((eq item '&rest) (setq mode 'rest))
266
+ ((eq mode 'req)
267
+ (dolist (v (eliscript-bind--collect-pattern-vars item))
268
+ (when (memq v all-vars)
269
+ (signal 'eliscript-compile-error
270
+ (list (format "ELI-B0010: duplicate variable `%S' in %s" v name))))
271
+ (push v all-vars))
272
+ (push item req-pats))
273
+ ((eq mode 'opt)
274
+ (let ((pat (if (consp item) (car item) item))
275
+ (def (if (consp item) (cadr item) nil)))
276
+ (dolist (v (eliscript-bind--collect-pattern-vars pat))
277
+ (when (memq v all-vars)
278
+ (signal 'eliscript-compile-error
279
+ (list (format "ELI-B0010: duplicate variable `%S' in %s" v name))))
280
+ (push v all-vars))
281
+ (push (list pat def) opt-specs)))
282
+ ((eq mode 'rest)
283
+ (dolist (v (eliscript-bind--collect-pattern-vars item))
284
+ (when (memq v all-vars)
285
+ (signal 'eliscript-compile-error
286
+ (list (format "ELI-B0010: duplicate variable `%S' in %s" v name))))
287
+ (push v all-vars))
288
+ (setq rest-pat item))))
289
+ (setq req-pats (nreverse req-pats))
290
+ (setq opt-specs (nreverse opt-specs))
291
+ (eliscript-contract-validate-context 'defun-bind name plist (nreverse all-vars) clean-body)
292
+ (let* ((wrapped-body (eliscript-contract-wrap-body name plist all-vars clean-body))
293
+ (args-sym (make-symbol "bind-args"))
294
+ (argc-sym (make-symbol "bind-argc"))
295
+ (min-arity (length req-pats))
296
+ (max-arity (if rest-pat nil (+ min-arity (length opt-specs))))
297
+ (inner-forms wrapped-body))
298
+ (when rest-pat
299
+ (setq inner-forms
300
+ (list (eliscript-bind--expand-single-binding
301
+ rest-pat `(nthcdr ,(+ min-arity (length opt-specs)) ,args-sym) inner-forms))))
302
+ (let ((opt-idx (1- (length opt-specs))))
303
+ (while (>= opt-idx 0)
304
+ (let* ((ospec (nth opt-idx opt-specs))
305
+ (opat (car ospec))
306
+ (odef (cadr ospec))
307
+ (pos (+ min-arity opt-idx)))
308
+ (setq inner-forms
309
+ (list (eliscript-bind--expand-single-binding
310
+ opat
311
+ `(if (> ,argc-sym ,pos)
312
+ (nth ,pos ,args-sym)
313
+ ,odef)
314
+ inner-forms))))
315
+ (setq opt-idx (1- opt-idx))))
316
+ (let ((req-idx (1- min-arity)))
317
+ (while (>= req-idx 0)
318
+ (let ((rpat (nth req-idx req-pats)))
319
+ (setq inner-forms
320
+ (list (eliscript-bind--expand-single-binding
321
+ rpat `(nth ,req-idx ,args-sym) inner-forms))))
322
+ (setq req-idx (1- req-idx))))
323
+ `(lambda (&rest ,args-sym)
324
+ (let ((,argc-sym (length ,args-sym)))
325
+ (when (< ,argc-sym ,min-arity)
326
+ (signal 'wrong-number-of-arguments (list ',name ,argc-sym)))
327
+ ,@(when max-arity
328
+ `((when (> ,argc-sym ,max-arity)
329
+ (signal 'wrong-number-of-arguments (list ',name ,argc-sym)))))
330
+ ,@inner-forms)))))
331
+
332
+ (defmacro defun-bind (name arg-specs &rest body)
333
+ "Define function NAME with unified destructuring ARG-SPECS and BODY."
334
+ (let ((fn-form (eliscript-bind--build-destructuring-lambda name arg-specs body)))
335
+ `(progn
336
+ (fset ',name ,fn-form)
337
+ ',name)))
338
+
339
+ (defmacro lambda-bind (arg-specs &rest body)
340
+ "Create an anonymous function with unified destructuring ARG-SPECS and BODY."
341
+ (eliscript-bind--build-destructuring-lambda 'lambda-bind arg-specs body))
342
+
343
+ (defun eliscript-bind--pattern-form-p (pat)
344
+ "Return non-nil if PAT is a destructuring pattern rather than a value expression."
345
+ (or (eq pat '_)
346
+ (eq pat :none)
347
+ (vectorp pat)
348
+ (and (consp pat)
349
+ (memq (car pat) '(:list :map :record :ok :error :some)))))
350
+
351
+ (defmacro pcase-value (expr &rest clauses)
352
+ "Match EXPR against CLAUSES using `value-equal-p' and unified patterns."
353
+ (let ((val-sym (make-symbol "pcase-val"))
354
+ (done-tag (make-symbol "pcase-done"))
355
+ (branch-forms nil))
356
+ (dolist (clause clauses)
357
+ (let ((pat (car clause))
358
+ (cbody (cdr clause)))
359
+ (cond
360
+ ((eq pat '_)
361
+ (push `(throw ',done-tag (progn ,@cbody)) branch-forms))
362
+ ((eliscript-bind--pattern-form-p pat)
363
+ (let ((ht-sym (make-symbol "pcase-ht"))
364
+ (vars (eliscript-bind--collect-pattern-vars pat)))
365
+ (push `(condition-case nil
366
+ (let ((,ht-sym (make-hash-table :test 'eq)))
367
+ (eliscript-bind--match-runtime ',pat ,val-sym ,ht-sym #'eval)
368
+ (throw ',done-tag
369
+ (let ,(mapcar (lambda (v) `(,v (gethash ',v ,ht-sym))) vars)
370
+ ,@cbody)))
371
+ (error nil))
372
+ branch-forms)))
373
+ (t
374
+ (push `(when (value-equal-p ,val-sym ,pat)
375
+ (throw ',done-tag (progn ,@cbody)))
376
+ branch-forms)))))
377
+ `(let ((,val-sym ,expr))
378
+ (catch ',done-tag
379
+ ,@(nreverse branch-forms)
380
+ nil))))
381
+
382
+ (defmacro cond-predicate (pred expr &rest clauses)
383
+ "Evaluate PRED and EXPR once, then test CLAUSES sequentially (supporting `:>>')."
384
+ (let ((pred-sym (make-symbol "cond-pred"))
385
+ (val-sym (make-symbol "cond-val"))
386
+ (res-sym (make-symbol "cond-res"))
387
+ (done-tag (make-symbol "cond-done"))
388
+ (cursor clauses)
389
+ (steps nil))
390
+ (while cursor
391
+ (cond
392
+ ((null (cdr cursor))
393
+ (push `(throw ',done-tag ,(car cursor)) steps)
394
+ (setq cursor nil))
395
+ ((eq (cadr cursor) :>>)
396
+ (let ((test-val (car cursor))
397
+ (recv-fn (nth 2 cursor)))
398
+ (push `(let ((,res-sym (funcall ,pred-sym ,test-val ,val-sym)))
399
+ (when ,res-sym
400
+ (throw ',done-tag (funcall ,recv-fn ,res-sym))))
401
+ steps)
402
+ (setq cursor (nthcdr 3 cursor))))
403
+ (t
404
+ (let ((test-val (car cursor))
405
+ (then-expr (cadr cursor)))
406
+ (push `(when (funcall ,pred-sym ,test-val ,val-sym)
407
+ (throw ',done-tag ,then-expr))
408
+ steps)
409
+ (setq cursor (cddr cursor))))))
410
+ `(let ((,pred-sym ,pred)
411
+ (,val-sym ,expr))
412
+ (catch ',done-tag
413
+ ,@(nreverse steps)
414
+ nil))))
415
+
416
+ ;; ============================================================================
417
+ ;; 2. Multi-Arity Dispatch (`defun-case`, `lambda-case`)
418
+ ;; ============================================================================
419
+
420
+ (defun eliscript-bind--compile-case-clauses (name clauses plist)
421
+ "Validate and compile multi-arity CLAUSES for NAME with declaration PLIST."
422
+ (let ((fixed-arities nil)
423
+ (rest-min-arity nil)
424
+ (compiled-branches nil)
425
+ (destructure-p (plist-get plist :destructure)))
426
+ (dolist (clause clauses)
427
+ (unless (and (consp clause) (listp (car clause)))
428
+ (signal 'eliscript-compile-error
429
+ (list (format "ELI-A0010: invalid clause %S in %s" clause name))))
430
+ (let* ((params (car clause))
431
+ (cbody (cdr clause)))
432
+ (when (memq '&optional params)
433
+ (signal 'eliscript-compile-error
434
+ (list (format "ELI-A0010: &optional is forbidden in defun-case/lambda-case (%s)" name))))
435
+ (let ((rest-pos (cl-position '&rest params)))
436
+ (if rest-pos
437
+ (progn
438
+ (when rest-min-arity
439
+ (signal 'eliscript-compile-error
440
+ (list (format "ELI-A0012: multiple &rest clauses in %s" name))))
441
+ (unless (= rest-pos (- (length params) 2))
442
+ (signal 'eliscript-compile-error
443
+ (list (format "ELI-A0012: &rest must be followed by a single parameter in %s" name))))
444
+ (setq rest-min-arity rest-pos)
445
+ (let ((fn-form (if destructure-p
446
+ (eliscript-bind--build-destructuring-lambda name params cbody)
447
+ `(lambda ,params ,@cbody))))
448
+ (push (list :kind 'rest :min rest-pos :fn fn-form) compiled-branches)))
449
+ (let ((arity (length params)))
450
+ (when (memq arity fixed-arities)
451
+ (signal 'eliscript-compile-error
452
+ (list (format "ELI-A0011: duplicate fixed arity %d in %s" arity name))))
453
+ (push arity fixed-arities)
454
+ (let ((fn-form (if destructure-p
455
+ (eliscript-bind--build-destructuring-lambda name params cbody)
456
+ `(lambda ,params ,@cbody))))
457
+ (push (list :kind 'fixed :arity arity :fn fn-form) compiled-branches)))))))
458
+ (when rest-min-arity
459
+ (dolist (fa fixed-arities)
460
+ (when (>= fa rest-min-arity)
461
+ (signal 'eliscript-compile-error
462
+ (list (format "ELI-A0012: fixed arity %d overlaps &rest minimum arity %d in %s"
463
+ fa rest-min-arity name))))))
464
+ (let ((args-sym (make-symbol "case-args"))
465
+ (argc-sym (make-symbol "case-argc"))
466
+ (cond-clauses nil))
467
+ (dolist (br (nreverse compiled-branches))
468
+ (if (eq (plist-get br :kind) 'fixed)
469
+ (push `((= ,argc-sym ,(plist-get br :arity))
470
+ (apply ,(plist-get br :fn) ,args-sym))
471
+ cond-clauses)
472
+ (push `((>= ,argc-sym ,(plist-get br :min))
473
+ (apply ,(plist-get br :fn) ,args-sym))
474
+ cond-clauses)))
475
+ `(lambda (&rest ,args-sym)
476
+ (let ((,argc-sym (length ,args-sym)))
477
+ (cond
478
+ ,@(nreverse cond-clauses)
479
+ (t (signal 'wrong-number-of-arguments (list ',name ,argc-sym)))))))))
480
+
481
+ (defmacro defun-case (name &rest body)
482
+ "Define multi-arity function NAME from clauses in BODY."
483
+ (let* ((extracted (eliscript-contract-extract-declarations body name))
484
+ (plist (plist-get extracted :eliscript))
485
+ (clauses (plist-get extracted :body))
486
+ (fn-form (eliscript-bind--compile-case-clauses name clauses plist)))
487
+ `(progn
488
+ (fset ',name ,fn-form)
489
+ ',name)))
490
+
491
+ (defmacro lambda-case (&rest body)
492
+ "Create an anonymous multi-arity function from clauses in BODY."
493
+ (let* ((extracted (eliscript-contract-extract-declarations body 'lambda-case))
494
+ (plist (plist-get extracted :eliscript))
495
+ (clauses (plist-get extracted :body)))
496
+ (eliscript-bind--compile-case-clauses 'lambda-case clauses plist)))
497
+
498
+ ;; ============================================================================
499
+ ;; 3. Immutable Local Bindings (`let-const`, `let*-const`)
500
+ ;; ============================================================================
501
+
502
+ (defun eliscript-bind--check-const-mutations (form const-vars)
503
+ "Scan FORM and signal an error if any symbol in CONST-VARS is mutated."
504
+ (when (and const-vars (consp form))
505
+ (let ((head (car form)))
506
+ (cond
507
+ ((eq head 'quote) nil)
508
+ ((eq head 'setq)
509
+ (let ((cursor (cdr form)))
510
+ (while cursor
511
+ (let ((target (car cursor))
512
+ (val-expr (cadr cursor)))
513
+ (when (memq target const-vars)
514
+ (signal 'eliscript-compile-error
515
+ (list (format "ELI-C0010: cannot reassign immutable binding `%S' via setq"
516
+ target))))
517
+ (eliscript-bind--check-const-mutations val-expr const-vars))
518
+ (setq cursor (cddr cursor)))))
519
+ ((eq head 'setf)
520
+ (let ((cursor (cdr form)))
521
+ (while cursor
522
+ (let ((place (car cursor))
523
+ (val-expr (cadr cursor)))
524
+ (when (and (symbolp place) (memq place const-vars))
525
+ (signal 'eliscript-compile-error
526
+ (list (format "ELI-C0010: cannot mutate immutable binding `%S' via setf"
527
+ place))))
528
+ (eliscript-bind--check-const-mutations place const-vars)
529
+ (eliscript-bind--check-const-mutations val-expr const-vars))
530
+ (setq cursor (cddr cursor)))))
531
+ ((memq head '(let let*))
532
+ (let ((bindings (cadr form))
533
+ (b-body (cddr form))
534
+ (shadowed nil))
535
+ (dolist (b bindings)
536
+ (if (consp b)
537
+ (progn
538
+ (eliscript-bind--check-const-mutations (cadr b) const-vars)
539
+ (push (car b) shadowed))
540
+ (push b shadowed)))
541
+ (let ((remaining (cl-set-difference const-vars shadowed)))
542
+ (dolist (sub b-body)
543
+ (eliscript-bind--check-const-mutations sub remaining)))))
544
+ (t
545
+ (dolist (sub (cdr form))
546
+ (eliscript-bind--check-const-mutations sub const-vars)))))))
547
+
548
+ (defun eliscript-bind--validate-const-bindings (bindings body macro-name)
549
+ "Validate BINDINGS and BODY for `let-const' or `let*-const'."
550
+ (let ((vars nil))
551
+ (dolist (b bindings)
552
+ (let ((sym (if (consp b) (car b) b)))
553
+ (unless (and (symbolp sym) (not (keywordp sym)))
554
+ (signal 'eliscript-compile-error
555
+ (list (format "ELI-C0010: invalid binding variable `%S' in %s" sym macro-name))))
556
+ (when (special-variable-p sym)
557
+ (signal 'eliscript-compile-error
558
+ (list (format "ELI-C0011: %s cannot bind dynamic special variable `%S'"
559
+ macro-name sym))))
560
+ (push sym vars)))
561
+ (dolist (form body)
562
+ (eliscript-bind--check-const-mutations form vars))))
563
+
564
+ (defmacro let-const (bindings &rest body)
565
+ "Bind immutable local BINDINGS in parallel and evaluate BODY."
566
+ (eliscript-bind--validate-const-bindings bindings body 'let-const)
567
+ `(let ,bindings ,@body))
568
+
569
+ (defmacro let*-const (bindings &rest body)
570
+ "Bind immutable local BINDINGS sequentially and evaluate BODY."
571
+ (eliscript-bind--validate-const-bindings bindings body 'let*-const)
572
+ `(let* ,bindings ,@body))
573
+
574
+ ;; ============================================================================
575
+ ;; 4. Stack-Safe Explicit Tail Recursion (`let-loop`, `defun-tail`, `lambda-tail`, `recur`, `trampoline`)
576
+ ;; ============================================================================
577
+
578
+ (defun eliscript-bind--verify-recur-form (form tail-p arity context-name)
579
+ "Verify `recur' in FORM only appears when TAIL-P is non-nil and matches ARITY."
580
+ (when (consp form)
581
+ (let ((head (car form)))
582
+ (cond
583
+ ((eq head 'quote) nil)
584
+ ((eq head 'recur)
585
+ (unless tail-p
586
+ (signal 'eliscript-compile-error
587
+ (list (format "ELI-R0001: `recur' must be in tail position in %s" context-name))))
588
+ (unless (= (length (cdr form)) arity)
589
+ (signal 'eliscript-compile-error
590
+ (list (format "ELI-R0002: `recur' in %s expected %d arguments, got %d"
591
+ context-name arity (length (cdr form))))))
592
+ (dolist (arg (cdr form))
593
+ (eliscript-bind--verify-recur-form arg nil arity context-name)))
594
+ ((memq head '(lambda lambda-async lambda-bind lambda-case defun defun-async defun-bind defun-case))
595
+ ;; Nested functions establish a new boundary; any unshadowed recur inside is forbidden
596
+ (dolist (sub (cddr form))
597
+ (eliscript-bind--verify-recur-form sub nil arity context-name)))
598
+ ((memq head '(let-loop defun-tail lambda-tail))
599
+ ;; Inner tail-recursion construct shadows recur for its own body
600
+ nil)
601
+ ((eq head 'progn)
602
+ (eliscript-bind--verify-recur-seq (cdr form) tail-p arity context-name))
603
+ ((eq head 'if)
604
+ (eliscript-bind--verify-recur-form (nth 1 form) nil arity context-name)
605
+ (eliscript-bind--verify-recur-form (nth 2 form) tail-p arity context-name)
606
+ (eliscript-bind--verify-recur-seq (nthcdr 3 form) tail-p arity context-name))
607
+ ((memq head '(when unless))
608
+ (eliscript-bind--verify-recur-form (nth 1 form) nil arity context-name)
609
+ (eliscript-bind--verify-recur-seq (cddr form) tail-p arity context-name))
610
+ ((eq head 'cond)
611
+ (dolist (clause (cdr form))
612
+ (when (consp clause)
613
+ (if (null (cdr clause))
614
+ (eliscript-bind--verify-recur-form (car clause) tail-p arity context-name)
615
+ (eliscript-bind--verify-recur-form (car clause) nil arity context-name)
616
+ (eliscript-bind--verify-recur-seq (cdr clause) tail-p arity context-name)))))
617
+ ((memq head '(let let* let-const let*-const let-bind let*-bind))
618
+ (dolist (b (cadr form))
619
+ (when (consp b)
620
+ (eliscript-bind--verify-recur-form (cadr b) nil arity context-name)))
621
+ (eliscript-bind--verify-recur-seq (cddr form) tail-p arity context-name))
622
+ ((eq head 'unwind-protect)
623
+ (eliscript-bind--verify-recur-form (nth 1 form) tail-p arity context-name)
624
+ (dolist (cleanup (nthcdr 2 form))
625
+ (eliscript-bind--verify-recur-form cleanup nil arity context-name)))
626
+ (t
627
+ (dolist (sub (cdr form))
628
+ (eliscript-bind--verify-recur-form sub nil arity context-name)))))))
629
+
630
+ (defun eliscript-bind--verify-recur-seq (forms tail-p arity context-name)
631
+ "Verify `recur' in sequence FORMS where only the last form has TAIL-P."
632
+ (let ((cursor forms))
633
+ (while cursor
634
+ (eliscript-bind--verify-recur-form
635
+ (car cursor) (and tail-p (null (cdr cursor))) arity context-name)
636
+ (setq cursor (cdr cursor)))))
637
+
638
+ (defvar eliscript-bind--active-recur-tag nil
639
+ "Dynamic tag used by active `let-loop' / `defun-tail' / `lambda-tail' frame.")
640
+
641
+ (defun recur (&rest new-args)
642
+ "Jump to enclosing tail-recursive target with NEW-ARGS."
643
+ (unless eliscript-bind--active-recur-tag
644
+ (signal 'eliscript-compile-error
645
+ (list "ELI-R0001: `recur' called outside `let-loop', `defun-tail', or `lambda-tail'")))
646
+ (throw eliscript-bind--active-recur-tag (cons :eliscript-recur new-args)))
647
+
648
+ (defun eliscript-bind--expand-tail-loop (context-name vars init-exprs body)
649
+ "Expand tail-recursive loop for CONTEXT-NAME with VARS, INIT-EXPRS, BODY."
650
+ (eliscript-bind--verify-recur-seq body t (length vars) context-name)
651
+ (let ((tag-sym (make-symbol "recur-tag"))
652
+ (running-sym (make-symbol "recur-running"))
653
+ (result-sym (make-symbol "recur-result"))
654
+ (step-sym (make-symbol "recur-step"))
655
+ (next-vals-sym (make-symbol "recur-vals")))
656
+ `(let ,(mapcar (lambda (pair) (list (car pair) (cdr pair)))
657
+ (cl-mapcar #'cons vars init-exprs))
658
+ (let ((,running-sym t)
659
+ (,result-sym nil))
660
+ (while ,running-sym
661
+ (let ((,step-sym
662
+ (catch ',tag-sym
663
+ (let ((eliscript-bind--active-recur-tag ',tag-sym))
664
+ (cons :eliscript-return (progn ,@body))))))
665
+ (if (and (consp ,step-sym) (eq (car ,step-sym) :eliscript-recur))
666
+ (let ((,next-vals-sym (cdr ,step-sym)))
667
+ (ignore ,next-vals-sym)
668
+ ,@(let ((idx 0)
669
+ (assigns nil))
670
+ (dolist (v vars (nreverse assigns))
671
+ (push `(setq ,v (nth ,idx ,next-vals-sym)) assigns)
672
+ (setq idx (1+ idx)))))
673
+ (setq ,running-sym nil)
674
+ (setq ,result-sym (cdr ,step-sym)))))
675
+ ,result-sym))))
676
+
677
+ (defmacro let-loop (&rest args)
678
+ "Stack-safe loop construct `(let-loop ((var init) ...) body...)' with `recur'."
679
+ (let* ((has-name (symbolp (car args)))
680
+ (bindings (if has-name (cadr args) (car args)))
681
+ (body (if has-name (cddr args) (cdr args)))
682
+ (vars (mapcar (lambda (b) (if (consp b) (car b) b)) bindings))
683
+ (inits (mapcar (lambda (b) (if (consp b) (cadr b) nil)) bindings)))
684
+ (eliscript-bind--expand-tail-loop (if has-name (car args) 'let-loop) vars inits body)))
685
+
686
+ (defmacro defun-tail (name args &rest body)
687
+ "Define a stack-safe tail-recursive function NAME with ARGS and BODY."
688
+ (let* ((extracted (eliscript-contract-extract-declarations body name))
689
+ (plist (plist-get extracted :eliscript))
690
+ (clean-body (plist-get extracted :body)))
691
+ (eliscript-contract-validate-context 'defun-tail name plist args clean-body)
692
+ (let ((loop-form (eliscript-bind--expand-tail-loop name args args clean-body)))
693
+ `(defun ,name ,args
694
+ ,@(eliscript-contract-wrap-body name plist args (list loop-form))))))
695
+
696
+ (defmacro lambda-tail (args &rest body)
697
+ "Create an anonymous stack-safe tail-recursive function with ARGS and BODY."
698
+ (let* ((extracted (eliscript-contract-extract-declarations body 'lambda-tail))
699
+ (plist (plist-get extracted :eliscript))
700
+ (clean-body (plist-get extracted :body)))
701
+ (eliscript-contract-validate-context 'lambda-tail 'lambda-tail plist args clean-body)
702
+ (let ((loop-form (eliscript-bind--expand-tail-loop 'lambda-tail args args clean-body)))
703
+ `(lambda ,args
704
+ ,@(eliscript-contract-wrap-body 'lambda-tail plist args (list loop-form))))))
705
+
706
+ (defun trampoline (fn &rest args)
707
+ "Repeatedly call zero-arg thunks from `(apply FN ARGS)' until non-function."
708
+ (let ((res (apply fn args)))
709
+ (while (functionp res)
710
+ (setq res (funcall res)))
711
+ res))
712
+
713
+ ;; ============================================================================
714
+ ;; 5. Pipeline Threading Macros (`thread-as`, `thread-cond-*`, `thread-some-*`)
715
+ ;; ============================================================================
716
+
717
+ (defmacro thread-as (initial-form var &rest forms)
718
+ "Bind VAR to INITIAL-FORM, then thread VAR sequentially through FORMS."
719
+ (let ((acc initial-form))
720
+ (dolist (step forms acc)
721
+ (setq acc `(let ((,var ,acc)) ,step)))))
722
+
723
+ (defun eliscript-bind--insert-first (val step)
724
+ "Insert VAL as the first argument of STEP."
725
+ (if (consp step)
726
+ `(,(car step) ,val ,@(cdr step))
727
+ `(,step ,val)))
728
+
729
+ (defun eliscript-bind--insert-last (val step)
730
+ "Insert VAL as the last argument of STEP."
731
+ (if (consp step)
732
+ `(,(car step) ,@(cdr step) ,val)
733
+ `(,step ,val)))
734
+
735
+ (defmacro thread-cond-first (initial-form &rest clauses)
736
+ "Thread INITIAL-FORM as first arg through `(TEST STEP)' pairs in CLAUSES."
737
+ (let ((val-sym (make-symbol "cond-thread-val"))
738
+ (cursor clauses)
739
+ (steps nil))
740
+ (while cursor
741
+ (let ((test (car cursor))
742
+ (step (cadr cursor)))
743
+ (push `(when ,test
744
+ (setq ,val-sym ,(eliscript-bind--insert-first val-sym step)))
745
+ steps))
746
+ (setq cursor (cddr cursor)))
747
+ `(let ((,val-sym ,initial-form))
748
+ ,@(nreverse steps)
749
+ ,val-sym)))
750
+
751
+ (defmacro thread-cond-last (initial-form &rest clauses)
752
+ "Thread INITIAL-FORM as last arg through `(TEST STEP)' pairs in CLAUSES."
753
+ (let ((val-sym (make-symbol "cond-thread-val"))
754
+ (cursor clauses)
755
+ (steps nil))
756
+ (while cursor
757
+ (let ((test (car cursor))
758
+ (step (cadr cursor)))
759
+ (push `(when ,test
760
+ (setq ,val-sym ,(eliscript-bind--insert-last val-sym step)))
761
+ steps))
762
+ (setq cursor (cddr cursor)))
763
+ `(let ((,val-sym ,initial-form))
764
+ ,@(nreverse steps)
765
+ ,val-sym)))
766
+
767
+ (defmacro thread-some-first (initial-form &rest forms)
768
+ "Thread INITIAL-FORM as first arg through FORMS, short-circuiting on nil."
769
+ (let ((val-sym (make-symbol "some-thread-val"))
770
+ (steps nil))
771
+ (dolist (step forms)
772
+ (push `(when (not (null ,val-sym))
773
+ (setq ,val-sym ,(eliscript-bind--insert-first val-sym step)))
774
+ steps))
775
+ `(let ((,val-sym ,initial-form))
776
+ ,@(nreverse steps)
777
+ ,val-sym)))
778
+
779
+ (defmacro thread-some-last (initial-form &rest forms)
780
+ "Thread INITIAL-FORM as last arg through FORMS, short-circuiting on nil."
781
+ (let ((val-sym (make-symbol "some-thread-val"))
782
+ (steps nil))
783
+ (dolist (step forms)
784
+ (push `(when (not (null ,val-sym))
785
+ (setq ,val-sym ,(eliscript-bind--insert-last val-sym step)))
786
+ steps))
787
+ `(let ((,val-sym ,initial-form))
788
+ ,@(nreverse steps)
789
+ ,val-sym)))
790
+
791
+ ;; ============================================================================
792
+ ;; 6. Higher-Order Function Combinators (`fn-compose`, `fn-partial`, `fn-juxt`, `fn-complement`)
793
+ ;; ============================================================================
794
+
795
+ (defun fn-compose (&rest fns)
796
+ "Return the right-to-left composition of FNS."
797
+ (if (null fns)
798
+ #'identity
799
+ (let ((rev (reverse fns)))
800
+ (lambda (&rest args)
801
+ (let ((res (apply (car rev) args)))
802
+ (dolist (f (cdr rev) res)
803
+ (setq res (funcall f res))))))))
804
+
805
+ (defun fn-partial (fn &rest bound-args)
806
+ "Return a function that calls FN with BOUND-ARGS prepended to new arguments."
807
+ (lambda (&rest more-args)
808
+ (apply fn (append bound-args more-args))))
809
+
810
+ (defun fn-juxt (&rest fns)
811
+ "Return a function applying each fn in FNS to args and returning a list."
812
+ (lambda (&rest args)
813
+ (mapcar (lambda (f) (apply f args)) fns)))
814
+
815
+ (defun fn-complement (pred)
816
+ "Return a predicate that returns the boolean negation of PRED."
817
+ (lambda (&rest args)
818
+ (not (apply pred args))))
819
+
820
+ (provide 'eliscript-bind)
821
+
822
+ ;;; eliscript-bind.el ends here