js-on-sx: object + array destructuring

Parser: jp-parse-vardecl handles {a, b} obj pattern and [a, , c]
arr pattern (with hole support) in addition to plain idents.
Emits (js-vardecl-obj names rhs) and (js-vardecl-arr names rhs).

Transpile: js-vardecl-forms dispatches on tag. Destructures emit
(define __destruct__ rhs) then (define name (js-get-prop __destruct__
key-or-index)) for each pattern element. Array holes (nil) are skipped.

418/420 unit (+4), 148/148 slice unchanged.
This commit is contained in:
2026-04-23 22:32:24 +00:00
parent dd6375af18
commit 15c310cdc1
4 changed files with 175 additions and 19 deletions

View File

@@ -845,12 +845,68 @@
(else
(let
((d (first decls)))
(cons
(list
(js-sym "define")
(js-sym (nth d 1))
(js-transpile (nth d 2)))
(js-vardecl-forms (rest decls))))))))
(cond
((js-tag? d "js-vardecl")
(cons
(list
(js-sym "define")
(js-sym (nth d 1))
(js-transpile (nth d 2)))
(js-vardecl-forms (rest decls))))
((js-tag? d "js-vardecl-obj")
(let
((names (nth d 1))
(rhs (js-transpile (nth d 2)))
(tmp-sym (js-sym "__destruct__")))
(cons
(list (js-sym "define") tmp-sym rhs)
(js-vardecl-obj-forms
names
tmp-sym
(js-vardecl-forms (rest decls))))))
((js-tag? d "js-vardecl-arr")
(let
((names (nth d 1))
(rhs (js-transpile (nth d 2)))
(tmp-sym (js-sym "__destruct__")))
(cons
(list (js-sym "define") tmp-sym rhs)
(js-vardecl-arr-forms
names
tmp-sym
0
(js-vardecl-forms (rest decls))))))
(else (error "js-vardecl-forms: unexpected decl"))))))))
(define
js-vardecl-obj-forms
(fn
(names tmp-sym tail)
(cond
((empty? names) tail)
(else
(cons
(list
(js-sym "define")
(js-sym (first names))
(list (js-sym "js-get-prop") tmp-sym (first names)))
(js-vardecl-obj-forms (rest names) tmp-sym tail))))))
(define
js-vardecl-arr-forms
(fn
(names tmp-sym i tail)
(cond
((empty? names) tail)
((= (first names) nil)
(js-vardecl-arr-forms (rest names) tmp-sym (+ i 1) tail))
(else
(cons
(list
(js-sym "define")
(js-sym (first names))
(list (js-sym "js-get-prop") tmp-sym i))
(js-vardecl-arr-forms (rest names) tmp-sym (+ i 1) tail))))))
(define
js-transpile-if-stmt