js-on-sx: postfix + prefix ++/--

Parser: jp-parse-postfix emits (js-postfix op target) on trailing
++/--; jp-parse-primary emits (js-prefix op target) before the
unary -/+/!/~ branch.

Transpile: js-transpile-prefix → (set! name (+ (js-to-number name)
±1)) for idents, (js-set-prop obj key ...) for members/indices.
js-transpile-postfix caches old value in a let binding, updates,
returns the saved value.

340/342 unit (+11), 148/148 slice unchanged.
This commit is contained in:
2026-04-23 20:50:10 +00:00
parent e0531d730c
commit 6c4001a299
4 changed files with 167 additions and 1 deletions

View File

@@ -101,6 +101,10 @@
(js-transpile-funcexpr (nth ast 1) (nth ast 2) (nth ast 3)))
((js-tag? ast "js-assign")
(js-transpile-assign (nth ast 1) (nth ast 2) (nth ast 3)))
((js-tag? ast "js-postfix")
(js-transpile-postfix (nth ast 1) (nth ast 2)))
((js-tag? ast "js-prefix")
(js-transpile-prefix (nth ast 1) (nth ast 2)))
((js-tag? ast "js-new")
(js-transpile-new (nth ast 1) (nth ast 2)))
((js-tag? ast "js-class")
@@ -440,6 +444,120 @@
((= op "=") rhs-expr)
(else (js-compound-update op lhs-expr rhs-expr)))))
(define
js-transpile-prefix
(fn
(op target)
(let
((delta (if (= op "++") 1 -1)))
(cond
((js-tag? target "js-ident")
(let
((name (nth target 1)))
(let
((sxname (js-sym name)))
(list
(js-sym "set!")
sxname
(list
(js-sym "+")
(list (js-sym "js-to-number") sxname)
delta)))))
((js-tag? target "js-member")
(let
((obj-sx (js-transpile (nth target 1))) (key (nth target 2)))
(list
(js-sym "js-set-prop")
obj-sx
key
(list
(js-sym "+")
(list
(js-sym "js-to-number")
(list (js-sym "js-get-prop") obj-sx key))
delta))))
((js-tag? target "js-index")
(let
((obj-sx (js-transpile (nth target 1)))
(key-sx (js-transpile (nth target 2))))
(list
(js-sym "js-set-prop")
obj-sx
key-sx
(list
(js-sym "+")
(list
(js-sym "js-to-number")
(list (js-sym "js-get-prop") obj-sx key-sx))
delta))))
(else (error "js-transpile-prefix: unsupported target"))))))
(define
js-transpile-postfix
(fn
(op target)
(let
((delta (if (= op "++") 1 -1)))
(cond
((js-tag? target "js-ident")
(let
((name (nth target 1)))
(let
((sxname (js-sym name)))
(list
(js-sym "let")
(list
(list
(js-sym "__js_old__")
(list (js-sym "js-to-number") sxname)))
(list
(js-sym "set!")
sxname
(list (js-sym "+") (js-sym "__js_old__") delta))
(js-sym "__js_old__")))))
((js-tag? target "js-member")
(let
((obj-sx (js-transpile (nth target 1))) (key (nth target 2)))
(list
(js-sym "let")
(list
(list (js-sym "__js_obj__") obj-sx)
(list
(js-sym "__js_old__")
(list
(js-sym "js-to-number")
(list (js-sym "js-get-prop") (js-sym "__js_obj__") key))))
(list
(js-sym "js-set-prop")
(js-sym "__js_obj__")
key
(list (js-sym "+") (js-sym "__js_old__") delta))
(js-sym "__js_old__"))))
((js-tag? target "js-index")
(let
((obj-sx (js-transpile (nth target 1)))
(key-sx (js-transpile (nth target 2))))
(list
(js-sym "let")
(list
(list (js-sym "__js_obj__") obj-sx)
(list (js-sym "__js_key__") key-sx)
(list
(js-sym "__js_old__")
(list
(js-sym "js-to-number")
(list
(js-sym "js-get-prop")
(js-sym "__js_obj__")
(js-sym "__js_key__")))))
(list
(js-sym "js-set-prop")
(js-sym "__js_obj__")
(js-sym "__js_key__")
(list (js-sym "+") (js-sym "__js_old__") delta))
(js-sym "__js_old__"))))
(else (error "js-transpile-postfix: unsupported target"))))))
(define
js-param-sym
(fn