write #() as {0}
{} is C23's. #() is all zeros, which C11 spells {0}, whatever it
initializes.
This commit is contained in:
@@ -359,13 +359,16 @@ forms, and what remains."
|
|||||||
;;; language. `:' is the separator because it is the one thing that can
|
;;; 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
|
;;; be neither a type word nor an expression -- a named form would be a
|
||||||
;;; C identifier, and so shadowable (see issue #36).
|
;;; 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)
|
(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))
|
(if (null? (cdr parts))
|
||||||
(list->vector (walk-designators (car parts)))
|
(list->vector (filled (car parts)))
|
||||||
(cons* '%compound
|
(cons* '%compound
|
||||||
(walk-type (maybe-unwrap-type (car parts)))
|
(walk-type (maybe-unwrap-type (car parts)))
|
||||||
(walk-designators (cadr parts))))))
|
(filled (cadr parts))))))
|
||||||
|
|
||||||
;;; `.field value' is a designated initializer; anything else is
|
;;; `.field value' is a designated initializer; anything else is
|
||||||
;;; positional. C lets the two be mixed, and nothing here stops it. A
|
;;; positional. C lets the two be mixed, and nothing here stops it. A
|
||||||
|
|||||||
@@ -1095,6 +1095,19 @@
|
|||||||
(emits? (string-append "(struct h ((n int) (p (* int))))"
|
(emits? (string-append "(struct h ((n int) (p (* int))))"
|
||||||
"(var g (struct h) #((struct h) : 3 #([int 2] : 1 2)))")
|
"(var g (struct h) #((struct h) : 3 #([int 2] : 1 2)))")
|
||||||
"{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"
|
(test-assert "while a value is still its braces"
|
||||||
(emits? (string-append "(struct pt ((x int))) (struct h2 ((a (struct pt))))"
|
(emits? (string-append "(struct pt ((x int))) (struct h2 ((a (struct pt))))"
|
||||||
"(var g (struct h2) #(#((struct pt) : 1)))")
|
"(var g (struct h2) #(#((struct pt) : 1)))")
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
(compilation "-- -std=c11 -pedantic-errors")
|
(compilation "-- -std=c11 -pedantic-errors")
|
||||||
(input)
|
(input)
|
||||||
(output "c11: 42 43 6 4 6 12 5"
|
(output "c11: 42 43 6 4 6 12 5 0"
|
||||||
"trigraphs: what??! ??= ???( ??/")
|
"trigraphs: what??! ??= ???( ??/")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
(var corner _ #(point : 2 3))
|
(var corner _ #(point : 2 3))
|
||||||
(var primes (* int) #([int 3] : 2 3 5))
|
(var primes (* int) #([int 3] : 2 3 5))
|
||||||
|
(var origin point #())
|
||||||
(var unit _ (shape/square 1))
|
(var unit _ (shape/square 1))
|
||||||
(var pair [_ 2] #(#(point : 1 2) #(point : 3 4)))
|
(var pair [_ 2] #(#(point : 1 2) #(point : 3 4)))
|
||||||
|
|
||||||
@@ -45,8 +46,8 @@
|
|||||||
(var k int 1)
|
(var k int 1)
|
||||||
(var add-k (closure ((int)) int)
|
(var add-k (closure ((int)) int)
|
||||||
(closure ((v int)) int (k) (return (+ v k))))
|
(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)
|
(.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??! ??= ???( ??/")
|
(printf "trigraphs: %s\n" "what??! ??= ???( ??/")
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
Reference in New Issue
Block a user