js-on-sx: switch/case/default/break

Parser: jp-parse-switch-stmt + jp-parse-switch-cases + jp-parse-switch-body.
AST: (js-switch discr (("case" val body) ("default" nil body) ...)).

Transpile: wraps body in (call/cc (fn (__break__) ...)). Each case
clause becomes (when (or __matched__ (js-strict-eq discr val))
(set! __matched__ true) body). Fall-through works naturally via
__matched__. Default appended as (when (not __matched__) body).

363/365 unit (+6), 148/148 slice unchanged.
This commit is contained in:
2026-04-23 21:04:22 +00:00
parent 275d2ecbae
commit 9d3e54029a
4 changed files with 167 additions and 0 deletions

View File

@@ -1015,6 +1015,63 @@
((e (jp-parse-assignment st)))
(do (jp-eat-semi st) (list (quote js-throw) e))))))
(define
jp-parse-switch-stmt
(fn
(st)
(jp-advance! st)
(jp-expect! st "punct" "(")
(let
((disc (jp-parse-assignment st)))
(jp-expect! st "punct" ")")
(jp-expect! st "punct" "{")
(let
((cases (list)))
(jp-parse-switch-cases st cases)
(jp-expect! st "punct" "}")
(list (quote js-switch) disc cases)))))
(define
jp-parse-switch-cases
(fn
(st cases)
(cond
((jp-at? st "punct" "}") nil)
((jp-at? st "keyword" "case")
(do
(jp-advance! st)
(let
((val (jp-parse-assignment st)))
(jp-expect! st "punct" ":")
(let
((body (list)))
(jp-parse-switch-body st body)
(append! cases (list "case" val body))
(jp-parse-switch-cases st cases)))))
((jp-at? st "keyword" "default")
(do
(jp-advance! st)
(jp-expect! st "punct" ":")
(let
((body (list)))
(jp-parse-switch-body st body)
(append! cases (list "default" nil body))
(jp-parse-switch-cases st cases))))
(else (error "switch: expected case or default")))))
(define
jp-parse-switch-body
(fn
(st body)
(cond
((jp-at? st "punct" "}") nil)
((jp-at? st "keyword" "case") nil)
((jp-at? st "keyword" "default") nil)
(else
(begin
(append! body (jp-parse-stmt st))
(jp-parse-switch-body st body))))))
(define
jp-parse-try-stmt
(fn
@@ -1055,6 +1112,7 @@
((and (jp-at? st "keyword" "async") (= (get (jp-peek-at st 1) :type) "keyword") (= (get (jp-peek-at st 1) :value) "function"))
(do (jp-advance! st) (jp-parse-async-function-decl st)))
((jp-at? st "keyword" "function") (jp-parse-function-decl st))
((jp-at? st "keyword" "switch") (jp-parse-switch-stmt st))
(else
(let
((e (jp-parse-assignment st)))