declare a record's typedef ahead of its struct

It came after, so a record pointing at itself, (next (* node)), named a
type C had not been told of yet.
This commit is contained in:
2026-10-16 13:55:22 +03:00
parent 8d09d8f68b
commit edfae88e4d
2 changed files with 23 additions and 8 deletions

View File

@@ -1903,13 +1903,19 @@
(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))))
;; the typedef first, so a field may point back at the record, and
;; the docstring ahead of both
(let-values (((doc struct)
(extract-aggregate-docstring
(copy-form-source! form
(if (eq? (car form) 'pub)
`(pub struct ,@(cdr core))
`(struct ,@(cdr core)))))))
(process-struct
struct
(with-docstring doc
(copy-form-source! form `(typedef (struct ,name) ,name))
acc)))))
(define (register-aggregate! form)
(let* ((f (if (eq? (car form) 'pub) (cdr form) form))

View File

@@ -1,5 +1,6 @@
(input)
(output "p 3 4 / n 7 zoe / area 12 / sizes 8 8")
(output "p 3 4 / n 7 zoe / area 12 / sizes 8 8"
"chain 1 2")
(return 0)
;;; A record is a struct that also names itself, so the bare name
@@ -18,6 +19,10 @@
(struct raw ((a int) (b int)))
;;; A record naming itself in its own fields: the name is a type before
;;; the fields are
(record node ((value int) (next (* node))))
(fn shift ((p (* point)) (dx int) (dy int)) void
(+= (. p x) dx)
(+= (. p y) dy))
@@ -43,4 +48,8 @@
(. p x) (. p y) (. n id) (. n first-name)
(area [ps 0])
(sizeof point) (sizeof (struct raw)))
(var tail node #(node : 2 NULL))
(var head node #(node : 1 (& tail)))
(printf "chain %d %d\n" (. head value) (. head next value))
(return (+ (. [ps 1] x) (- (. r a) 1))))