Files
sex/tests/sex-programs/type-shapes.sex
alex-eg 2345f7854e 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).
2026-10-19 18:26:12 +03:00

112 lines
3.7 KiB
Scheme

(input)
(output "aggregate element: 3 4"
"pointer element: there"
"multi-word element: 9"
"through a pointer: 55"
"unsized of a typedef: 1 2"
"unsized of a pointer: 5"
"named bound: 2 4 b"
"unnamed parameters: 7 -1 2"
"typedefs: hi 4 2"
"sizeof a pointer type: 1 1"
"dimensions: 6 12 5"
"returned function: 42")
(return 0)
;;; Three questions about a written type that used to be answered in
;;; three places and disagreed: is `(a b)' a named parameter or a bare
;;; type, is the last element of a `[...]' its bound or the last word of
;;; its element type, and what is one element of an array.
;;;
;;; They are one question -- where does the type end -- so the answer
;;; lives in `types' and everything else asks it.
(include stdio.h)
(struct point ((x int) (y int)))
(typedef small int)
(typedef cstr (* const char))
(typedef vec4 [float 4])
(typedef unary (fn ((small)) small))
(fn successor ((n int)) int (return (+ n 1)))
(fn stepper () (fn ((int)) int) (return successor))
(define CAP 2)
;;; a parameter that names nothing is a type, however many words it
;;; takes: `(unsigned int)' is one of them, not a `unsigned' called
;;; `int'
(fn width ((n unsigned int)) int
(return (cast n int)))
(fn sign ((c const char)) int
(if (== c #\a) (return -1))
(return 1))
(fn twice ((n small)) int
(return (* n 2)))
(pub fn main () int
;; an element keeps every word of its type, tag and all
(var pts [(struct point) 2] #(#((struct point) : 1 2)
#((struct point) : 3 4)))
(var p _ [pts 1])
(printf "aggregate element: %d %d\n" (. p x) (. p y))
(var names [(* const char) 2] #("hi" "there"))
(var s _ [names 1])
(printf "pointer element: %s\n" s)
(var nums [unsigned int 3] #(7 8 9))
(var u _ [nums 2])
(printf "multi-word element: %u\n" u)
;; subscripting a pointer answers the same as subscripting an array
(var q (* (struct point)) (& [pts 0]))
(var r _ [q 1])
(printf "through a pointer: %d\n" (+ (. r x) (* 13 (. r y))))
;; the last word of an unsized array's type is not its bound: neither
;; a typedef name nor the target of a `*' can be one
(var tail [const small] #(1 2))
(printf "unsized of a typedef: %d %d\n" [tail 0] [tail 1])
(var one size-t 5)
(var sizes [* size-t] #((& one)))
(var w _ [sizes 0])
(printf "unsized of a pointer: %d\n" (cast (* w) int))
;; ...but after a whole type, a name is: `size-t' is a typedef only C
;; knows of, and `(* const char)' a type grouped into one element
(var ws [size-t CAP] #(3 4))
(var gs [(* const char) CAP] #("a" "b"))
(printf "named bound: %zu %zu %s\n"
(/ (sizeof ws) (sizeof [ws 0])) [ws 1] [gs 1])
;; the same question in type position: `(fn ((unsigned int)) int)'
;; takes one parameter, not two
(var fp (fn ((unsigned int)) int) width)
(printf "unnamed parameters: %d %d %d\n" (fp 7) (sign #\a) (twice 1))
;; a typedef's target is every one of those shapes as well
(var greeting cstr "hi")
(var v vec4 #(1.0 2.0 3.0 4.0))
(var op unary twice)
(printf "typedefs: %s %zu %d\n" greeting (/ (sizeof v) (sizeof [v 0])) (op 1))
;; `(* T)' is a type or a dereference, and only what is in scope says
(printf "sizeof a pointer type: %d %d\n"
(== (sizeof (* const char)) (sizeof greeting))
(== (sizeof (* greeting)) 1))
;; [int 2 3] is two rows of three, indexed [g row column]
(var g [int 2 3] #(#(0 1 2) #(3 4 5)))
(printf "dimensions: %zu %zu %d\n"
(/ (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))