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:
2026-10-03 22:28:48 +03:00
parent 2512d52dd1
commit 622db8eadc
5 changed files with 71 additions and 1 deletions

View File

@@ -82,6 +82,8 @@
((or ('fn . _)
('pub 'fn . _)
('extern 'fn . _)) (process-fn sex-form acc))
((or ('record . _)
('pub 'record . _)) (process-record sex-form acc))
((or ('struct . _)
('pub 'struct . _)) (process-struct sex-form acc))
((or ('union . _)
@@ -1120,6 +1122,26 @@
(register-aggregate! form)
(with-docstring doc form acc))))
;;; (record point ((x int) (y int))) is a struct that also names
;;; itself, so `point' alone stands where `(struct point)' had to:
;;; (var p point ...), (* const point), (sizeof point).
;;;
;;; struct, union and enum keep C's meaning, for a type that comes from
;;; a header or is laid out to match one.
(define (process-record form acc)
(let* ((core (if (eq? (car form) 'pub) (cdr form) form))
(name (and (pair? (cdr core)) (cadr core))))
(unless (symbol? name)
(sex-error form "record needs a name" form))
(add-typedef name `(typedef ,name (struct ,name)))
(cons (copy-form-source! form `(typedef (struct ,name) ,name))
(process-struct
(copy-form-source! form
(if (eq? (car form) 'pub)
`(pub struct ,@(cdr core))
`(struct ,@(cdr core))))
acc))))
(define (register-aggregate! form)
(let* ((f (if (eq? (car form) 'pub) (cdr form) form))
(name (and (pair? (cdr f)) (symbol? (cadr f)) (cadr f))))