leave an empty for slot empty

() went to C as NULL, so (for () () ()) tested false and never ran; it
is for (;;).
This commit is contained in:
2026-10-17 17:35:48 +03:00
parent c0ed7ad916
commit 8cb141de5d
2 changed files with 18 additions and 5 deletions

View File

@@ -738,13 +738,18 @@
fl))))
;; 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. And a
;; loop with no body has the empty block.
;; the one place a comma expression goes bare, so they say so. A slot
;; written () is empty, where c-expr would say NULL -- for (;;), not a
;; loop whose test is false. And a loop with no body has the empty
;; block.
(define (c-for init check update . body)
(define (slot x lead show)
(if (null? x) "" (cat lead (show (c-expr x)))))
(define (bare e) (c-with-op 'comma e))
(let ((header (c-in-expr
(cat "for (" (c-with-op 'comma (c-expr init)) "; "
(c-in-test (c-expr check)) "; "
(c-with-op 'comma (c-expr update)) ")"))))
(cat "for (" (slot init "" bare) ";"
(slot check " " c-in-test) ";"
(slot update " " bare) ")"))))
(c-reset-newline
(cat (if (null? body)
(c-braced-block header)

View File

@@ -346,6 +346,14 @@
(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) {}")))
;; an empty slot was the empty list, which C was told as NULL: the
;; test of (for () () ()) was false, and the loop never ran
(test-group "empty for slots"
(test-assert "are empty"
(emits? (in-fn "(for () () () (g a))") "for (;;) {"))
(test-assert "any of them"
(emits? (in-fn "(for () (< a 1) () (g a))") "for (; a < 1;) {")))
;; A nested pointer chain used to silently lose a level:
;; (var p (* (* char))) emitted `char *p'
(test-group "malformed types are rejected"