diff --git a/semen.scm b/semen.scm index 45fad890..811f270f 100644 --- a/semen.scm +++ b/semen.scm @@ -2042,7 +2042,13 @@ (walk-operands core (drop core 3) env)) core)) env))) - (copy-form-source! solved (map bare-braces solved))))) + (if (> (length solved) 3) + (copy-form-source! + solved + (append (take solved 3) + (map (cut bare-braces <> (third solved)) + (drop solved 3)))) + solved)))) (form (if qualifier (copy-form-source! resolved (cons qualifier core)) core))) @@ -2052,14 +2058,53 @@ ;;; 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: +;;; alone say the same. Not where it is an array a pointer is set to: +;;; that is the address of a static object, which is a constant, and its +;;; braces alone would be the pointer's own. TARGET is what FORM +;;; initializes, #f where nothing says. ;;; -;;; (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)) +;;; (var g t (t/a 1)) -> static t g = {.tag = tǂa, ...} +;;; (var p (* int) #([int 2] : 1 2)) -> static int * p = (int [2]){1, 2} +(define (bare-braces form target) + (if (not (vector? form)) + form + (let* ((parts (list-split (vector->list form) ':)) + (written (and (pair? (cdr parts)) (unwrap-type (car parts))))) + (if (and written (array-form? written) target (pointer-target target)) + form + (let ((elements (last parts))) + (list->vector + (map bare-braces elements + (element-targets (or written target) elements)))))))) + +;;; What each of ELEMENTS, in braces for TYPE, initializes: an element +;;; of an array, the fields of an aggregate in turn, or the one a `.name' +;;; before it names -- and #f where TYPE does not say +(define (element-targets type elements) + (let ((element (and type (array-form? type) (array-element-type type))) + (fields (and type (not (array-form? type)) (type-fields type)))) + (let loop ((es elements) (next (or fields (list))) (acc (list))) + (cond + ((null? es) (reverse acc)) + (element (loop (cdr es) next (cons element acc))) + ((designator-name (car es)) + => (lambda (name) + (let ((at (and fields + (find-tail (lambda (f) (eq? (car f) name)) fields)))) + (if (and at (pair? (cdr es))) + (loop (cddr es) (cdr at) (cons (second (car at)) (cons #f acc))) + (loop (cdr es) (list) (cons #f acc)))))) + (else (loop (cdr es) + (if (pair? next) (cdr next) next) + (cons (and (pair? next) (second (car next))) acc))))))) + +;;; .x -> x, as a designator names a field; #f for anything else +(define (designator-name x) + (and (symbol? x) + (let ((s (symbol->string x))) + (and (> (string-length s) 1) + (char=? #\. (string-ref s 0)) + (string->symbol (substring s 1)))))) ;;; Utils diff --git a/tests/codegen.scm b/tests/codegen.scm index 3c40ffeb..89243fac 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -1061,7 +1061,24 @@ (test-assert "ahead of it" (let ((c (sex->c "(var f (fn ((x int)) int) (lambda ((x int)) int (return x)))"))) (< (string-contains c "static int λ0_f (int x)") - (string-contains c "(*f)(int) = λ0_f"))))) + (string-contains c "(*f)(int) = λ0_f")))) + ;; every compound literal lost its type at file scope, which is right + ;; for a value and not for an array a pointer is set to: that is the + ;; address of a static object, and its braces alone the pointer's own + (test-assert "an array a pointer is set to keeps its type" + (emits? "(var gp (* int) #([int 3] : 1 2 3))" "int * gp = (int [3]){1, 2, 3}")) + (test-assert "in a field named for it" + (emits? (string-append "(struct h ((p (* int)) (n int)))" + "(var g (struct h) #(.p #([int 2] : 1 2) .n 3))") + "{.p = (int [2]){1, 2}, .n = 3}")) + (test-assert "or one in its place" + (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}}")) + (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)))") + "h2 g = {{1}}"))) ;; fmt-c kept the first star after a tag and dropped what followed it, ;; and took an enum's star for its list of values diff --git a/tests/sex-programs/c11.sex b/tests/sex-programs/c11.sex index 2081ca90..ad1582f0 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" +(output "c11: 42 43 6 4 6 12 5" "trigraphs: what??! ??= ???( ??/") (return 0) @@ -29,6 +29,7 @@ (sum shape ((square ((side int))) (dot ()))) (var corner _ #(point : 2 3)) +(var primes (* int) #([int 3] : 2 3 5)) (var unit _ (shape/square 1)) (var pair [_ 2] #(#(point : 1 2) #(point : 3 4))) @@ -44,8 +45,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\n" + (printf "c11: %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])) + (sides unit) (.area corner) (.area [pair 1]) [primes 2]) (printf "trigraphs: %s\n" "what??! ??= ???( ??/") (return 0))