forked from alex-eg/sex
add record, a struct that names itself
(record point ...) emits the struct and a typedef of the same name, so point stands alone wherever (struct point) had to -- in a var, a pointer, an arglist, a sizeof. struct, union and enum keep C's meaning, for a type that came from a header or is laid out to match one.
This commit is contained in:
45
tests/sex-programs/records.sex
Normal file
45
tests/sex-programs/records.sex
Normal file
@@ -0,0 +1,45 @@
|
||||
(input)
|
||||
(output "p 3 4 / n 7 zoe / area 12 / sizes 8 8")
|
||||
(return 0)
|
||||
|
||||
;;; A record is a struct that also names itself, so the bare name
|
||||
;;; stands wherever (struct name) had to: a var, a pointer, an array
|
||||
;;; element, an arglist, a return type, a sizeof. struct keeps C's
|
||||
;;; meaning, for a type that came from a header or is laid out to
|
||||
;;; match one.
|
||||
|
||||
(include stdio.h)
|
||||
|
||||
(record point ((x int) (y int)))
|
||||
|
||||
(record named "Fields of more than one kind, and a docstring."
|
||||
((id int) (first-name (* const char))))
|
||||
|
||||
(struct raw ((a int) (b int)))
|
||||
|
||||
(fn shift ((p (* point)) (dx int) (dy int)) void
|
||||
(+= (-> p x) dx)
|
||||
(+= (-> p y) dy))
|
||||
|
||||
(fn area ((p point)) int
|
||||
(return (* (. p x) (. p y))))
|
||||
|
||||
(fn origin () point
|
||||
(return #(point : 0 0)))
|
||||
|
||||
(pub fn main () int
|
||||
(var p point #(point : 1 2))
|
||||
(shift (& p) 2 2)
|
||||
|
||||
(var n named #(named : .id 7 .first-name "zoe"))
|
||||
(var r (struct raw) #(struct raw : 1 2))
|
||||
|
||||
(var ps [point 2])
|
||||
(= [ps 0] p)
|
||||
(= [ps 1] (origin))
|
||||
|
||||
(printf "p %d %d / n %d %s / area %d / sizes %zu %zu\n"
|
||||
(. p x) (. p y) (. n id) (. n first-name)
|
||||
(area [ps 0])
|
||||
(sizeof point) (sizeof (struct raw)))
|
||||
(return (+ (. [ps 1] x) (- (. r a) 1))))
|
||||
Reference in New Issue
Block a user