give fmt-c a name for every parameter
fmt-c takes a parameter's name with `cadr', so an unnamed one handed over bare lost its second word to it: `(* const char)' dropped its star, and `(int)' had no second word at all.
This commit is contained in:
51
tests/sex-programs/unnamed-params.sex
Normal file
51
tests/sex-programs/unnamed-params.sex
Normal file
@@ -0,0 +1,51 @@
|
||||
(input)
|
||||
(output "one word: 7"
|
||||
"pointer: 2"
|
||||
"aggregate: 3"
|
||||
"array: 2.5"
|
||||
"variadic: 1 two")
|
||||
(return 0)
|
||||
|
||||
;;; A parameter that names nothing still has to reach the C writer as a
|
||||
;;; type and a name, the name being absent. Handed the bare type
|
||||
;;; instead, fmt-c read the type's own second word as the name -- so
|
||||
;;; `(* const char)' came out `const char', which is a different
|
||||
;;; function -- and a one-word type had no second word to read at all.
|
||||
|
||||
(include stdio.h)
|
||||
(include stdarg.h)
|
||||
|
||||
(struct point ((x int) (y int)))
|
||||
|
||||
;;; declared here rather than included, so the prototype we emit is the
|
||||
;;; one the C compiler checks the call against
|
||||
(extern fn abs ((int)) int)
|
||||
(extern fn strlen ((* const char)) size-t)
|
||||
|
||||
(fn origin-x ((p (* (struct point)))) int
|
||||
(return (. (* p) x)))
|
||||
|
||||
(fn second-of ((xs (¤ float 4))) float
|
||||
(return (¤ xs 1)))
|
||||
|
||||
(fn say ((fmt (* const char)) ...) void
|
||||
(var ap va-list)
|
||||
(va-start ap fmt)
|
||||
(vprintf fmt ap)
|
||||
(va-end ap))
|
||||
|
||||
(pub fn main () int
|
||||
(printf "one word: %d\n" (abs -7))
|
||||
(printf "pointer: %d\n" (cast (strlen "hi") int))
|
||||
|
||||
;; the same parameter lists written as types
|
||||
(var p (struct point) #((struct point) : 3 4))
|
||||
(var f (fn ((* (struct point))) int) origin-x)
|
||||
(printf "aggregate: %d\n" (f (& p)))
|
||||
|
||||
(var xs (¤ float 4) #(1.5 2.5 3.5 4.5))
|
||||
(var g (fn ((¤ float 4)) float) second-of)
|
||||
(printf "array: %g\n" (g xs))
|
||||
|
||||
(say "variadic: %d %s\n" 1 "two")
|
||||
(return 0))
|
||||
Reference in New Issue
Block a user