forked from alex-eg/sex
make a declaring case body a block
A case body shared the switch's scope, so two cases could not each declare `bits', and a label could not be followed by a declaration before C23; inference shared the scope too, and typed a later case's read of an outer name as the earlier case's local. Fixes #51.
This commit is contained in:
@@ -84,6 +84,13 @@ forms, and what remains."
|
||||
form
|
||||
`(%begin ,@(map walk-expr comments) ,form)))
|
||||
|
||||
;;; A case body that declares is a block, so two cases can each declare
|
||||
;;; a `bits', and a declaration can follow the label before C23
|
||||
(define (walk-case-body body)
|
||||
(if (any (lambda (form) (and (pair? form) (eq? 'var (car form)))) body)
|
||||
(list (cons '%block-begin (walk-body body)))
|
||||
(walk-body body)))
|
||||
|
||||
(define (walk-if-clauses clauses)
|
||||
"(test stmt test stmt ... [else-stmt]) -- tests stay expressions."
|
||||
(let loop ((cs clauses) (acc (list)))
|
||||
@@ -248,12 +255,12 @@ forms, and what remains."
|
||||
(('case . rest)
|
||||
(let-values (((comments slots body) (take-slots rest 1)))
|
||||
(with-comments comments
|
||||
(cons* 'case (walk-expr (car slots)) (walk-body body)))))
|
||||
(cons* 'case (walk-expr (car slots)) (walk-case-body body)))))
|
||||
(('case/fallthrough . rest)
|
||||
(let-values (((comments slots body) (take-slots rest 1)))
|
||||
(with-comments comments
|
||||
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-body body)))))
|
||||
(('default . body) (cons 'default (walk-body body)))
|
||||
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-case-body body)))))
|
||||
(('default . body) (cons 'default (walk-case-body body)))
|
||||
|
||||
;; Drop comments so they will not generate additional comma
|
||||
(else (map walk-expr (remove comment-form? form)))))
|
||||
|
||||
16
semen.scm
16
semen.scm
@@ -501,9 +501,8 @@
|
||||
;;; (while (< v 0) (var v char 1) ...)
|
||||
;;; (var m _ (+ v 1)) ; double, not char
|
||||
;;;
|
||||
;;; One frame per form, not one per arm: a `case' label opens no scope
|
||||
;;; in C, and an `if' arm can only declare inside a `do', which brings
|
||||
;;; its own.
|
||||
;;; A `case' body gets one too, being written as a block. An `if' arm
|
||||
;;; gets none: it can only declare inside a `do', which brings its own.
|
||||
|
||||
(define (declare-name! env name type)
|
||||
(hash-table-set! (car (hash-table-ref env :scopes)) name type))
|
||||
@@ -584,20 +583,13 @@
|
||||
(cons walk-embed-result (walk-body expansion env)))))
|
||||
(else
|
||||
(case (car form)
|
||||
((do)
|
||||
((do default)
|
||||
(parameterize ((operand-position? #f))
|
||||
(with-scope env (lambda () (walk-parts form env)))))
|
||||
|
||||
((for while if switch)
|
||||
((for while if switch case case/fallthrough)
|
||||
(with-scope env (lambda () (walk-control form env))))
|
||||
|
||||
((case case/fallthrough)
|
||||
(walk-control form env))
|
||||
|
||||
((default)
|
||||
(parameterize ((operand-position? #f))
|
||||
(walk-parts form env)))
|
||||
|
||||
((lambda)
|
||||
(let ((name (aux-name! env make-lambda-name)))
|
||||
(add-aux-code! env (lift-lambda name form))
|
||||
|
||||
@@ -53,6 +53,15 @@ compiles."
|
||||
(let ((m (error-message source)))
|
||||
(and m (string-contains m fragment) #t)))
|
||||
|
||||
(define (followed-by? source first then)
|
||||
"Is FIRST in the C compiled from SOURCE, and the next thing past it,
|
||||
whitespace aside, THEN?"
|
||||
(let* ((c (silently (lambda () (sex->c source))))
|
||||
(at (string-contains c first)))
|
||||
(and at
|
||||
(let ((next (string-skip c char-whitespace? (+ at (string-length first)))))
|
||||
(and next (string-prefix? then (substring c next)))))))
|
||||
|
||||
(define (in-fn body)
|
||||
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
||||
|
||||
@@ -78,6 +87,22 @@ compiles."
|
||||
;; c-switch handed its scrutinee straight to `cat', which only works
|
||||
;; when it is an atom. Anything else was displayed as a raw
|
||||
;; s-expression: `switch ((%. e type))'.
|
||||
;; A case body shared the switch's scope, so two cases could not
|
||||
;; declare one name, and a label could not be followed by a
|
||||
;; declaration before C23. Only a body that declares needs a block
|
||||
(test-group "case bodies"
|
||||
(test-assert "are blocks"
|
||||
(followed-by? (in-fn "(switch a (case 1 (var x int 1) (g x)))")
|
||||
"case 1:" "{"))
|
||||
(test-assert "so is a default"
|
||||
(followed-by? (in-fn "(switch a (default (var x int 1) (g x)))")
|
||||
"default:" "{"))
|
||||
(test-assert "where they declare"
|
||||
(followed-by? (in-fn "(switch a (case 1 (g a)))") "case 1:" "g(a)"))
|
||||
(test-assert "and one already a block is not wrapped again"
|
||||
(not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))")
|
||||
"{" "{"))))
|
||||
|
||||
(test-group "switch scrutinee"
|
||||
(test-assert "member access"
|
||||
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
"loop: 0 1 2"
|
||||
"shadowed: 9 then 42"
|
||||
"still an int: 200"
|
||||
"case scope: 200"
|
||||
"partial: 1 2.5"
|
||||
"joined: 43.5 84 49 1"
|
||||
"promoted: 200 60000 3705032704"
|
||||
@@ -67,14 +68,18 @@
|
||||
(var wide int 200)
|
||||
(while false (var wide char 1) (printf "%d" wide))
|
||||
(if false (do (var wide char 1) (printf "%d" wide)))
|
||||
;; a statement before the declaration: a label may not be followed by
|
||||
;; one until C23
|
||||
(switch a (case 1 (printf "") (var wide char 1) (printf "%d" wide) (break)))
|
||||
(switch a (case 1 (var wide char 1) (printf "%d" wide)))
|
||||
;; a copy, not a sum: an arithmetic result would be promoted to `int'
|
||||
;; whatever leaked, and say nothing
|
||||
(var copy _ wide)
|
||||
(printf "still an int: %d\n" copy)
|
||||
|
||||
;; each case is a block of its own: the second sees the outer `wide',
|
||||
;; not the first one's
|
||||
(switch 2
|
||||
(case 1 (var wide double 1.5) (printf "%g" wide))
|
||||
(case 2 (var seen _ wide) (printf "case scope: %d\n" seen)))
|
||||
|
||||
;; a wildcard inside a written type: only it is solved
|
||||
(var pp2 (* _) (& p))
|
||||
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))
|
||||
|
||||
Reference in New Issue
Block a user