designate an initializer's element by its index
[3] in braces reached the writer as a subscript of one operand, and sexc died taking the second.
This commit is contained in:
@@ -378,7 +378,7 @@ forms, and what remains."
|
|||||||
(let loop ((es elements) (acc (list)))
|
(let loop ((es elements) (acc (list)))
|
||||||
(match es
|
(match es
|
||||||
(() (reverse acc))
|
(() (reverse acc))
|
||||||
(((? designator? d))
|
(((or (? designator? d) (and ('¤ _) d)))
|
||||||
(sex-error elements "designated initializer without a value" d))
|
(sex-error elements "designated initializer without a value" d))
|
||||||
(((? designator? d) value . rest)
|
(((? designator? d) value . rest)
|
||||||
(loop rest
|
(loop rest
|
||||||
@@ -386,6 +386,12 @@ forms, and what remains."
|
|||||||
(atom-to-fmt-c (designator-field d))
|
(atom-to-fmt-c (designator-field d))
|
||||||
(walk-expr value))
|
(walk-expr value))
|
||||||
acc)))
|
acc)))
|
||||||
|
;; [3] 1: an element named by its index. A subscript has two
|
||||||
|
;; operands, so one alone can be nothing else
|
||||||
|
((('¤ index) value . rest)
|
||||||
|
(loop rest
|
||||||
|
(cons (list '%designate-index (walk-expr index) (walk-expr value))
|
||||||
|
acc)))
|
||||||
((e . rest) (loop rest (cons (walk-expr e) acc))))))
|
((e . rest) (loop rest (cons (walk-expr e) acc))))))
|
||||||
|
|
||||||
(define (designator? x)
|
(define (designator? x)
|
||||||
|
|||||||
@@ -322,6 +322,7 @@
|
|||||||
((%sizeof-type) ((cat "sizeof(" (c-type (cadr x)) ")") st))
|
((%sizeof-type) ((cat "sizeof(" (c-type (cadr x)) ")") st))
|
||||||
((%compound) ((apply c-compound (cdr x)) st))
|
((%compound) ((apply c-compound (cdr x)) st))
|
||||||
((%designate) ((apply c-designate (cdr x)) st))
|
((%designate) ((apply c-designate (cdr x)) st))
|
||||||
|
((%designate-index) ((apply c-designate-index (cdr x)) st))
|
||||||
((+ - & * / % ! ~ ^ && < > <= >= == != << >>
|
((+ - & * / % ! ~ ^ && < > <= >= == != << >>
|
||||||
= *= /= %= &= ^= >>= <<=) ; |\|| |\|\|| |\|=|
|
= *= /= %= &= ^= >>= <<=) ; |\|| |\|\|| |\|=|
|
||||||
((apply c-op x) st))
|
((apply c-op x) st))
|
||||||
@@ -927,6 +928,10 @@
|
|||||||
(define (c-designate field value)
|
(define (c-designate field value)
|
||||||
(cat "." (c-expr field) " = " (c-expr value)))
|
(cat "." (c-expr field) " = " (c-expr value)))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c, which designates fields only
|
||||||
|
(define (c-designate-index index value)
|
||||||
|
(cat "[" (c-expr index) "] = " (c-expr value)))
|
||||||
|
|
||||||
(define (c-typedef type alias . o)
|
(define (c-typedef type alias . o)
|
||||||
(c-wrap-stmt
|
(c-wrap-stmt
|
||||||
(cat "typedef " (c-type type alias) (fmt-join/prefix c-expr o " "))))
|
(cat "typedef " (c-type type alias) (fmt-join/prefix c-expr o " "))))
|
||||||
|
|||||||
@@ -1034,7 +1034,14 @@
|
|||||||
"{pp->x, pp->y}"))
|
"{pp->x, pp->y}"))
|
||||||
(test-assert "designated"
|
(test-assert "designated"
|
||||||
(emits? (in-line "(var q (struct point) #(.y (. pp x)))")
|
(emits? (in-line "(var q (struct point) #(.y (. pp x)))")
|
||||||
"{.y = pp->x}")))
|
"{.y = pp->x}"))
|
||||||
|
;; [3] reached the writer as a subscript of one operand, and died
|
||||||
|
(test-assert "an element designated by its index"
|
||||||
|
(emits? (in-fn "(var xs [int 5] #([3] 1 [1] 2))") "{[3] = 1, [1] = 2}"))
|
||||||
|
(test-assert "among positional ones"
|
||||||
|
(emits? (in-fn "(var xs [int 5] #(7 [3] 1 9))") "{7, [3] = 1, 9}"))
|
||||||
|
(test-assert "and one with no value is an error"
|
||||||
|
(reports? (in-fn "(var xs [int 5] #(1 [3]))") "designated initializer without a value")))
|
||||||
|
|
||||||
(test-group "member access"
|
(test-group "member access"
|
||||||
(test-assert "a value reads with a dot"
|
(test-assert "a value reads with a dot"
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
"array 10 20 30"
|
"array 10 20 30"
|
||||||
"argument 6"
|
"argument 6"
|
||||||
"mixed 1 5"
|
"mixed 1 5"
|
||||||
"read through a pointer 1 5")
|
"read through a pointer 1 5"
|
||||||
|
"by index 0 2 0 1")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; `:' inside #(...) ends a type and makes the rest a C99 compound
|
;;; `:' inside #(...) ends a type and makes the rest a C99 compound
|
||||||
@@ -53,4 +54,8 @@
|
|||||||
(var c (struct point) #(struct point : (. pm x) (. pm y)))
|
(var c (struct point) #(struct point : (. pm x) (. pm y)))
|
||||||
(printf "read through a pointer %d %d\n" (. c x) (. c y))
|
(printf "read through a pointer %d %d\n" (. c x) (. c y))
|
||||||
|
|
||||||
|
;; an element may be named by its index, as a field is by its name
|
||||||
|
(var sparse [int 4] #([3] 1 [1] 2))
|
||||||
|
(printf "by index %d %d %d %d\n" [sparse 0] [sparse 1] [sparse 2] [sparse 3])
|
||||||
|
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
Reference in New Issue
Block a user