From ef4f4bb25afa7ec0f656013f96ce7a260af9625f Mon Sep 17 00:00:00 2001 From: Janneke Nieuwenhuizen Date: Sat, 16 Sep 2023 08:19:00 +0200 Subject: [PATCH] squash! mescc: Fix switch statements' fallthrough --support mes --- module/mescc/compile.scm | 50 ++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/module/mescc/compile.scm b/module/mescc/compile.scm index 071dfb67..20cc8e28 100644 --- a/module/mescc/compile.scm +++ b/module/mescc/compile.scm @@ -1791,29 +1791,33 @@ (set! i (1+ i))) n)))) - (define (flatten-cases c) - (define (flatten-case case) - (pmatch case - ((case ,test (case . ,body)) - (append `((case ,test (expr-stmt))) (flatten-case `(case ,@body)))) - ((case ,test ,casebody (case . ,body)) - (append `((case ,test ,casebody)) (flatten-case `(case ,@body)))) - ((default (case . ,body)) - (append `((default (expr-stmt))) (flatten-case `(case ,@body)))) - ((default ,defbody (case . ,body)) - (append `((default ,defbody)) (flatten-case `(case ,@body)))) - ((case ,test (default . ,body)) - (append `((case ,test (expr-stmt))) (flatten-case `(default ,@body)))) - ((default ,rest) - (list case)) - ((case ,test) - (list case)) - ((case ,test ,expr) - (list case)) - (,s (list s)))) - (fold (lambda (x acc) (append acc (flatten-case x))) '() c)) - - + (define (flatten-cases cases) + (define (flatten-case o cases) + (let ((c (pmatch o + ((case ,test (case . ,body)) + (append `((case ,test (expr-stmt))) + (flatten-case `(case ,@body)))) + ((case ,test ,case-body (case . ,body)) + (append `((case ,test ,case-body)) + (flatten-case `(case ,@body)))) + ((default (case . ,body)) + (append `((default (expr-stmt))) + (flatten-case `(case ,@body)))) + ((default ,default-body (case . ,body)) + (append `((default ,default-body)) + (flatten-case `(case ,@body)))) + ((case ,test (default . ,body)) + (append `((case ,test (expr-stmt))) + (flatten-case `(default ,@body)))) + ((default ,rest) + (list o)) + ((case ,test) + (list o)) + ((case ,test ,expr) + (list o)) + (,s (list s))))) + (append c cases))) + (fold flatten-case '() cases)) (let* ((info (append-text info (ast->comment `(switch ,expr (compd-stmt (block-item-list (ellipsis))))))) (statements (flatten-cases statements))