From d6975d3c793cfcccc08e12084e12fc1537c27ad6 Mon Sep 17 00:00:00 2001 From: giles Date: Thu, 23 Apr 2026 22:43:38 +0000 Subject: [PATCH] js-on-sx: logical assignment &&= ||= ??= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit js-compound-update gains logical-assign operators: - &&= → (if (js-to-boolean lhs) rhs lhs) - ||= → (if (js-to-boolean lhs) lhs rhs) - ??= → (if nullish? rhs lhs) 427/429 unit (+4), 148/148 slice unchanged. --- lib/js/test.sh | 16 ++++++++++++++++ lib/js/transpile.sx | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/js/test.sh b/lib/js/test.sh index 1a80ae69..25c223aa 100755 --- a/lib/js/test.sh +++ b/lib/js/test.sh @@ -1093,6 +1093,16 @@ cat > "$TMPFILE" << 'EPOCHS' (epoch 2704) (eval "(js-eval \"var o = {a:null}; var r = o?.a?.b; r === undefined\")") +;; ── Phase 11.logassign: &&= ||= ??= ───────────────────────────── +(epoch 2800) +(eval "(js-eval \"var q=0; q||=5; q\")") +(epoch 2801) +(eval "(js-eval \"var q=1; q&&=7; q\")") +(epoch 2802) +(eval "(js-eval \"var q=null; q??=99; q\")") +(epoch 2803) +(eval "(js-eval \"var q=42; q??=99; q\")") + EPOCHS @@ -1683,6 +1693,12 @@ check 2702 "?. obj undef" 'undefined' check 2703 "?. chained" '7' check 2704 "?. null-chain" 'true' +# ── Phase 11.logassign ──────────────────────────────────────── +check 2800 "||= falsy" '5' +check 2801 "&&= truthy" '7' +check 2802 "??= null" '99' +check 2803 "??= has value" '42' + TOTAL=$((PASS + FAIL)) if [ $FAIL -eq 0 ]; then echo "✓ $PASS/$TOTAL JS-on-SX tests passed" diff --git a/lib/js/transpile.sx b/lib/js/transpile.sx index 5bcf22c7..92944bb8 100644 --- a/lib/js/transpile.sx +++ b/lib/js/transpile.sx @@ -506,6 +506,27 @@ ((= op "/=") (list (js-sym "js-div") lhs-expr rhs-expr)) ((= op "%=") (list (js-sym "js-mod") lhs-expr rhs-expr)) ((= op "**=") (list (js-sym "js-pow") lhs-expr rhs-expr)) + ((= op "&&=") + (list + (js-sym "if") + (list (js-sym "js-to-boolean") lhs-expr) + rhs-expr + lhs-expr)) + ((= op "||=") + (list + (js-sym "if") + (list (js-sym "js-to-boolean") lhs-expr) + lhs-expr + rhs-expr)) + ((= op "??=") + (list + (js-sym "if") + (list + (js-sym "or") + (list (js-sym "=") lhs-expr nil) + (list (js-sym "js-undefined?") lhs-expr)) + rhs-expr + lhs-expr)) (else (error (str "js-compound-update: unsupported op: " op)))))) (define