diff --git a/semen.scm b/semen.scm index 5d53e729..c9b33c7f 100644 --- a/semen.scm +++ b/semen.scm @@ -1878,7 +1878,8 @@ core (if (> (length core) 3) (append (take core 3) - (walk-operands core (drop core 3) env)) + (map bare-braces + (walk-operands core (drop core 3) env))) core)) env))) (form (if qualifier @@ -1888,6 +1889,17 @@ (add-name-type! (second core) (third core))) (cons form (append (hash-table-ref env :lambda-aux-code) acc)))) +;;; A compound literal is not a constant, though cc lets one pass at file +;;; scope unless asked to be pedantic; where it initializes, its braces +;;; alone say the same: +;;; +;;; (var g t (t/a 1)) -> static t g = {.tag = tǂa, .u = {.a = {1}}} +(define (bare-braces form) + (if (vector? form) + (let ((parts (list-split (vector->list form) ':))) + (list->vector (map bare-braces (last parts)))) + form)) + ;;; Utils (define (non-empty-list? form) (and (list? form) diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index 95d553eb..3d5b5772 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -203,7 +203,11 @@ (not (emits? "(pub sum t ((a ((x int)))))(fn f () t (return (t/a 1)))" "t tːa ("))) (test-assert "so it is a constant at file scope" - (emits? "(sum t ((a ((x int)))))(var g t (t/a 1))" "static t g = (t){")) + (emits? "(sum t ((a ((x int)))))(var g t (t/a 1))" + "static t g = {.tag = tǂa, .u = {.a = {1}}};")) + (test-assert "as is any literal a global starts from" + (emits? "(record point ((x int) (y int)))(var g point #(point : 1 2))" + "static point g = {1, 2};")) (test-assert "and an lvalue a method can take the address of" (emits? (string-append "(sum t ((a ((x int)))))" diff --git a/tests/sex-programs/c11.sex b/tests/sex-programs/c11.sex index 3d404621..174aca4c 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") +(output "c11: 42 43 6 4 6") (return 0) ;;; C11 is the floor, and this is the program that holds it there: the @@ -10,7 +10,8 @@ ;;; ;;; A capturing closure is the one that leans on C11 itself, through ;;; max_align_t in the environment and `_Static_assert' on its size; -;;; the record, the method and the match are here for what they emit. +;;; the record, the method and the match are here for what they emit, +;;; and the globals for initializers that have to be constants. (include stdio.h) @@ -22,6 +23,9 @@ (sum shape ((square ((side int))) (dot ()))) +(var corner point #(point : 2 3)) +(var unit shape (shape/square 1)) + (fn sides ((s shape)) int (var n int 0) (match s @@ -34,6 +38,7 @@ (var k int 1) (var add-k (closure ((int)) int) (closure ((v int)) int (k) (return (+ v k)))) - (printf "c11: %d %d %d\n" - (.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2)) + (printf "c11: %d %d %d %d %d\n" + (.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2) + (sides unit) (.area corner)) (return 0))