From ffa5e9b02c4068eb7ffac0703cb79a120103b03f Mon Sep 17 00:00:00 2001 From: alex-eg Date: Sat, 17 Oct 2026 17:35:48 +0300 Subject: [PATCH] write #() as {0} {} is C23's. #() is all zeros, which C11 spells {0}, whatever it initializes. --- fmt-c-writer.scm | 9 ++++++--- tests/codegen.scm | 13 +++++++++++++ tests/sex-programs/c11.sex | 7 ++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/fmt-c-writer.scm b/fmt-c-writer.scm index a40977c3..776b726b 100644 --- a/fmt-c-writer.scm +++ b/fmt-c-writer.scm @@ -359,13 +359,16 @@ forms, and what remains." ;;; language. `:' is the separator because it is the one thing that can ;;; be neither a type word nor an expression -- a named form would be a ;;; C identifier, and so shadowable (see issue #36). +;;; +;;; #() is all zeros, and C11 spells that {0}: {} came in only with C23 (define (walk-initializer elements) - (let ((parts (list-split (remove comment-form? elements) ':))) + (let ((parts (list-split (remove comment-form? elements) ':)) + (filled (lambda (es) (if (null? es) (list 0) (walk-designators es))))) (if (null? (cdr parts)) - (list->vector (walk-designators (car parts))) + (list->vector (filled (car parts))) (cons* '%compound (walk-type (maybe-unwrap-type (car parts))) - (walk-designators (cadr parts)))))) + (filled (cadr parts)))))) ;;; `.field value' is a designated initializer; anything else is ;;; positional. C lets the two be mixed, and nothing here stops it. A diff --git a/tests/codegen.scm b/tests/codegen.scm index 5c216c68..586420fb 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -1095,6 +1095,19 @@ (emits? (string-append "(struct h ((n int) (p (* int))))" "(var g (struct h) #((struct h) : 3 #([int 2] : 1 2)))") "{3, (int [2]){1, 2}}")) + ;; #() is all zeros; {} says so only from C23, and {0} in C11 + (test-assert "empty braces are zeros" + (emits? "(struct pt ((x int))) (var g (struct pt) #())" "struct pt g = {0};")) + (test-assert "in a function and in a compound literal as well" + (emits? (string-append "(struct pt ((x int)))" + "(fn f () void (var p (struct pt) #((struct pt) :)))") + "(struct pt){0}")) + (test-assert "inside other braces" + (emits? (string-append "(struct pt ((x int))) (struct h ((a (struct pt)) (n int)))" + "(var g (struct h) #(#() 1))") + "{{0}, 1}")) + (test-assert "and for a scalar" + (emits? (in-fn "(var n int #())") "int n = {0};")) (test-assert "while a value is still its braces" (emits? (string-append "(struct pt ((x int))) (struct h2 ((a (struct pt))))" "(var g (struct h2) #(#((struct pt) : 1)))") diff --git a/tests/sex-programs/c11.sex b/tests/sex-programs/c11.sex index ad1582f0..6065ed90 100644 --- a/tests/sex-programs/c11.sex +++ b/tests/sex-programs/c11.sex @@ -1,6 +1,6 @@ (compilation "-- -std=c11 -pedantic-errors") (input) -(output "c11: 42 43 6 4 6 12 5" +(output "c11: 42 43 6 4 6 12 5 0" "trigraphs: what??! ??= ???( ??/") (return 0) @@ -30,6 +30,7 @@ (var corner _ #(point : 2 3)) (var primes (* int) #([int 3] : 2 3 5)) +(var origin point #()) (var unit _ (shape/square 1)) (var pair [_ 2] #(#(point : 1 2) #(point : 3 4))) @@ -45,8 +46,8 @@ (var k int 1) (var add-k (closure ((int)) int) (closure ((v int)) int (k) (return (+ v k)))) - (printf "c11: %d %d %d %d %d %d %d\n" + (printf "c11: %d %d %d %d %d %d %d %d\n" (.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2) - (sides unit) (.area corner) (.area [pair 1]) [primes 2]) + (sides unit) (.area corner) (.area [pair 1]) [primes 2] (. origin y)) (printf "trigraphs: %s\n" "what??! ??= ???( ??/") (return 0))