nest a function type's declarator round the name
A function type's return was written ahead of its declarator, so one returning a function pointer came out int (*)(int) get (void), and a variable of such a type as void (*)() (*g)(int).
This commit is contained in:
@@ -788,9 +788,12 @@
|
|||||||
(c-in-stmt (apply c-begin body)))
|
(c-in-stmt (apply c-begin body)))
|
||||||
fl))
|
fl))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: the name and parameters are the
|
||||||
|
;; return type's declarator, so a function returning a function pointer
|
||||||
|
;; is int (*get (void))(int), not int (*)(int) get (void)
|
||||||
(define (c-prototype type name params . o)
|
(define (c-prototype type name params . o)
|
||||||
(c-wrap-stmt
|
(c-wrap-stmt
|
||||||
(cat (c-type type) " " (c-expr name) " (" (c-param-list params) ")"
|
(cat (c-type type (cat (c-expr name) " (" (c-param-list params) ")"))
|
||||||
(fmt-join/prefix c-expr o " "))))
|
(fmt-join/prefix c-expr o " "))))
|
||||||
|
|
||||||
(define (c-static x) (cat "static " (c-expr x)))
|
(define (c-static x) (cat "static " (c-expr x)))
|
||||||
@@ -808,10 +811,14 @@
|
|||||||
(cond
|
(cond
|
||||||
((pair? type)
|
((pair? type)
|
||||||
(case (car type)
|
(case (car type)
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: the declarator goes round the
|
||||||
|
;; name, inside the return type's own, so a function type
|
||||||
|
;; returning one nests: void (*(*g)(int))()
|
||||||
((%fun)
|
((%fun)
|
||||||
(cat (c-type (cadr type) #f)
|
(c-type (cadr type)
|
||||||
" (*" (or name "") ")("
|
(cat "(*" (or name "") ")("
|
||||||
(fmt-join (lambda (x) (c-type x #f)) (caddr type) ", ") ")"))
|
(fmt-join (lambda (x) (c-type x #f)) (caddr type) ", ")
|
||||||
|
")")))
|
||||||
;; MODIFIED FROM UPSTREAM fmt-c: a nameless declarator -- an
|
;; MODIFIED FROM UPSTREAM fmt-c: a nameless declarator -- an
|
||||||
;; array parameter of a function type, where C has no room
|
;; array parameter of a function type, where C has no room
|
||||||
;; for a name -- arrives as #f, and upstream printed it
|
;; for a name -- arrives as #f, and upstream printed it
|
||||||
@@ -847,10 +854,14 @@
|
|||||||
(cond
|
(cond
|
||||||
((pair? type)
|
((pair? type)
|
||||||
(case (car type)
|
(case (car type)
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: the declarator goes round the
|
||||||
|
;; name, inside the return type's own, so a function type
|
||||||
|
;; returning one nests: void (*(*g)(int))()
|
||||||
((%fun)
|
((%fun)
|
||||||
(cat (c-type (cadr type) #f)
|
(c-type (cadr type)
|
||||||
" (*" (or name "") ")("
|
(cat "(*" (or name "") ")("
|
||||||
(fmt-join (lambda (x) (c-type x #f)) (caddr type) ", ") ")"))
|
(fmt-join (lambda (x) (c-type x #f)) (caddr type) ", ")
|
||||||
|
")")))
|
||||||
;; MODIFIED FROM UPSTREAM fmt-c: a nameless declarator -- an
|
;; MODIFIED FROM UPSTREAM fmt-c: a nameless declarator -- an
|
||||||
;; array parameter of a function type, where C has no room
|
;; array parameter of a function type, where C has no room
|
||||||
;; for a name -- arrives as #f, and upstream printed it
|
;; for a name -- arrives as #f, and upstream printed it
|
||||||
|
|||||||
@@ -828,6 +828,20 @@
|
|||||||
(test-assert "behind as many stars as are written"
|
(test-assert "behind as many stars as are written"
|
||||||
(emits? "(struct h ((f (* * (struct ((c int)))))))" "} * * f;")))
|
(emits? "(struct h ((f (* * (struct ((c int)))))))" "} * * f;")))
|
||||||
|
|
||||||
|
;; A function type's return was written ahead of its declarator, so a
|
||||||
|
;; function returning one came out `int (*)(int) get (void)'
|
||||||
|
(test-group "a function type returning one"
|
||||||
|
(test-assert "a function returning a function pointer"
|
||||||
|
(emits? "(fn inc ((x int)) int (return x)) (fn get () (fn ((int)) int) (return inc))"
|
||||||
|
"int (*get (void))(int)"))
|
||||||
|
(test-assert "and its prototype"
|
||||||
|
(emits? "(pub fn get () (fn ((int)) int))" "int (*get (void))(int);"))
|
||||||
|
(test-assert "a variable of one"
|
||||||
|
(emits? "(var g (fn ((int)) (fn () void)))" "void (*(*g)(int))()"))
|
||||||
|
(test-assert "and a parameter"
|
||||||
|
(emits? "(fn take ((f (fn ((int)) (fn () void)))) void (return))"
|
||||||
|
"take (void (*(*f)(int))())")))
|
||||||
|
|
||||||
;; Operands past what an operator takes were written as they came:
|
;; 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
|
;; (< a b c) as C's own chain, which compares a boolean with c
|
||||||
(test-group "operator arity"
|
(test-group "operator arity"
|
||||||
|
|||||||
@@ -9,7 +9,8 @@
|
|||||||
"unnamed parameters: 7 -1 2"
|
"unnamed parameters: 7 -1 2"
|
||||||
"typedefs: hi 4 2"
|
"typedefs: hi 4 2"
|
||||||
"sizeof a pointer type: 1 1"
|
"sizeof a pointer type: 1 1"
|
||||||
"dimensions: 6 12 5")
|
"dimensions: 6 12 5"
|
||||||
|
"returned function: 42")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; Three questions about a written type that used to be answered in
|
;;; Three questions about a written type that used to be answered in
|
||||||
@@ -29,6 +30,9 @@
|
|||||||
(typedef vec4 [float 4])
|
(typedef vec4 [float 4])
|
||||||
(typedef unary (fn ((small)) small))
|
(typedef unary (fn ((small)) small))
|
||||||
|
|
||||||
|
(fn successor ((n int)) int (return (+ n 1)))
|
||||||
|
(fn stepper () (fn ((int)) int) (return successor))
|
||||||
|
|
||||||
(define CAP 2)
|
(define CAP 2)
|
||||||
|
|
||||||
;;; a parameter that names nothing is a type, however many words it
|
;;; a parameter that names nothing is a type, however many words it
|
||||||
@@ -101,4 +105,7 @@
|
|||||||
(var g [int 2 3] #(#(0 1 2) #(3 4 5)))
|
(var g [int 2 3] #(#(0 1 2) #(3 4 5)))
|
||||||
(printf "dimensions: %zu %zu %d\n"
|
(printf "dimensions: %zu %zu %d\n"
|
||||||
(/ (sizeof g) (sizeof int)) (sizeof [g 0]) [g 1 2])
|
(/ (sizeof g) (sizeof int)) (sizeof [g 0]) [g 1 2])
|
||||||
|
|
||||||
|
;; a function type is a declarator wrapped round the name
|
||||||
|
(printf "returned function: %d\n" ((stepper) 41))
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
Reference in New Issue
Block a user