From 17d1467d1e37cb52239e920f9d10dd48692fd4d2 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Sat, 17 Oct 2026 17:35:48 +0300 Subject: [PATCH] leave an empty for slot empty () went to C as NULL, so (for () () ()) tested false and never ran; it is for (;;). --- sex-fmt-c.scm | 15 ++++++++++----- tests/codegen.scm | 8 ++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/sex-fmt-c.scm b/sex-fmt-c.scm index 1de620b..31f847d 100644 --- a/sex-fmt-c.scm +++ b/sex-fmt-c.scm @@ -733,13 +733,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) diff --git a/tests/codegen.scm b/tests/codegen.scm index f9c080c..41f5f49 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -416,6 +416,14 @@ whitespace aside, THEN?" (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"