forked from alex-eg/sex
keep an aggregate written in place behind a pointer
A type was flattened word by word, a struct's body included, so
(* (struct ((c int)))) came out struct c{ int; } *.
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
(chicken module)
|
||||
(chicken string)
|
||||
(chicken syntax)
|
||||
brev-separate ; fn, flatten
|
||||
brev-separate ; fn
|
||||
fmt
|
||||
sex-fmt-c
|
||||
matchable
|
||||
@@ -447,6 +447,7 @@ forms, and what remains."
|
||||
(define (has-pointer-star? form)
|
||||
(and (pair? form)
|
||||
(not (structured-type? form))
|
||||
(not (inline-aggregate? form))
|
||||
(or (memq '* form)
|
||||
(any has-pointer-star? (filter pair? form)))))
|
||||
|
||||
@@ -476,11 +477,22 @@ forms, and what remains."
|
||||
(sex-error type "a fn type is a function pointer already: write (fn ...), not (* (fn ...))" type)
|
||||
(sex-error type "a pointer to an array is not supported" type))))
|
||||
(if (atom? type) (atom-to-fmt-c type)
|
||||
(flatten
|
||||
(tree-map atom-to-fmt-c
|
||||
(flatten
|
||||
(list-join (reverse (list-split type '*))
|
||||
'(*)))))))
|
||||
(type-words (list-join (reverse (list-split type '*)) '(*)))))
|
||||
|
||||
;;; A type's words, flat, except that an aggregate written out in full is
|
||||
;;; one of them, walked: (* (struct ((c int)))) is `struct {...} *'
|
||||
(define (type-words items)
|
||||
(append-map (lambda (item)
|
||||
(cond ((inline-aggregate? item) (list (walk-type item)))
|
||||
((pair? item) (type-words item))
|
||||
((null? item) (list))
|
||||
(else (list (atom-to-fmt-c item)))))
|
||||
items))
|
||||
|
||||
;;; (struct ((c int))), (union u (...)), (enum (red green)): a body, not
|
||||
;;; just a tag
|
||||
(define (inline-aggregate? form)
|
||||
(and (pair? form) (memq (car form) '(struct union enum)) (any list? (cdr form))))
|
||||
|
||||
(define (walk-fn-def form)
|
||||
(match form
|
||||
|
||||
@@ -790,6 +790,18 @@
|
||||
(test-assert "and negation beside a binary operator"
|
||||
(emits? (in-fn "(var n int (+ (- a) b))") "-a + b")))
|
||||
|
||||
;; A type was flattened word by word, a struct written out in it
|
||||
;; included: (* (struct ((c int)))) came out `struct c{ int; } *'
|
||||
(test-group "a pointer to an aggregate written in place"
|
||||
(test-assert "keeps its fields"
|
||||
(followed-by? "(struct h ((b (* (struct ((c int)))))))" "int c;" "} * b;"))
|
||||
(test-assert "a pointer among them as well"
|
||||
(emits? "(struct h ((b (* (struct ((p (* int)) (n int)))))))" "int * p;"))
|
||||
(test-assert "and a qualifier before it"
|
||||
(emits? "(struct h ((e (* const (union ((u int) (v float)))))))" "const union{"))
|
||||
(test-assert "behind as many stars as are written"
|
||||
(emits? "(struct h ((f (* * (struct ((c int)))))))" "} * * f;")))
|
||||
|
||||
;; Operands past what an operator takes were written as they came:
|
||||
;; (< a b c) as C's own chain, which compares a boolean with c
|
||||
(test-group "operator arity"
|
||||
|
||||
Reference in New Issue
Block a user