write an empty block as {}

(do), and a for with no body, gave c-begin nothing to begin with, and
the writer died on its arity.
This commit is contained in:
2026-10-17 17:35:48 +03:00
parent ffa5e9b02c
commit c0ed7ad916
2 changed files with 28 additions and 13 deletions

View File

@@ -619,11 +619,15 @@
(define (c-braced-block . args) (define (c-braced-block . args)
(apply c-braced-block/aux 0 args)) (apply c-braced-block/aux 0 args))
;; MODIFIED FROM UPSTREAM fmt-c: a block of nothing is `{}', where
;; c-begin had nothing to begin with and died on its arity.
(define (c-braced-block/aux offset header . body) (define (c-braced-block/aux offset header . body)
(lambda (st) (lambda (st)
((cat (if header header "") (c-open-brace st) (c-indent st offset) ((if (null? body)
(apply c-begin body) fl (cat (if header (cat header " ") "") "{}")
(c-current-indent-string st offset) (c-close-brace st)) (cat (if header header "") (c-open-brace st) (c-indent st offset)
(apply c-begin body) fl
(c-current-indent-string st offset) (c-close-brace st)))
st))) st)))
(define (c-begin . args) (define (c-begin . args)
@@ -734,17 +738,18 @@
fl)))) fl))))
;; MODIFIED FROM UPSTREAM fmt-c: a header's first and last slots are ;; MODIFIED FROM UPSTREAM fmt-c: a header's first and last slots are
;; the one place a comma expression goes bare, so they say so. ;; the one place a comma expression goes bare, so they say so. And a
;; loop with no body has the empty block.
(define (c-for init check update . body) (define (c-for init check update . body)
(c-reset-newline (let ((header (c-in-expr
(cat (cat "for (" (c-with-op 'comma (c-expr init)) "; "
(c-block (c-in-test (c-expr check)) "; "
(c-in-expr (c-with-op 'comma (c-expr update)) ")"))))
(cat "for (" (c-with-op 'comma (c-expr init)) "; " (c-reset-newline
(c-in-test (c-expr check)) "; " (cat (if (null? body)
(c-with-op 'comma (c-expr update)) ")")) (c-braced-block header)
(c-in-stmt (apply c-begin body))) (c-block header (c-in-stmt (apply c-begin body))))
fl))) fl))))
(define (c-param x) (define (c-param x)
(cond (cond

View File

@@ -336,6 +336,16 @@
(reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))" (reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))"
"codegen.sex:2:"))) "codegen.sex:2:")))
;; a block of nothing had nothing for c-begin to begin with, and the
;; writer died on its arity
(test-group "empty bodies"
(test-assert "an empty block is a pair of braces"
(followed-by? (in-fn "(do) (g a)") "{}" "g(a);"))
(test-assert "inside another too"
(followed-by? (in-fn "(do (do) (g a))") "{}" "g(a);"))
(test-assert "and a for with no body has them"
(emits? (in-fn "(for (var i int 0) (< i a) (++ i))") "for (int i = 0; i < a; ++i) {}")))
;; A nested pointer chain used to silently lose a level: ;; A nested pointer chain used to silently lose a level:
;; (var p (* (* char))) emitted `char *p' ;; (var p (* (* char))) emitted `char *p'
(test-group "malformed types are rejected" (test-group "malformed types are rejected"