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:
1
Makefile
1
Makefile
@@ -109,6 +109,7 @@ SEX_TEST_PROGRAMS = c99 \
|
||||
lambdas \
|
||||
lists \
|
||||
operators \
|
||||
records \
|
||||
serialize \
|
||||
type-shapes \
|
||||
unicode \
|
||||
|
||||
22
semen.scm
22
semen.scm
@@ -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))))
|
||||
|
||||
@@ -39,6 +39,7 @@ All commands in `lisp-mode-shared-map' are inherited by this map.")
|
||||
"include"
|
||||
"fn"
|
||||
"pub"
|
||||
"record"
|
||||
"struct"
|
||||
"var"
|
||||
"union")
|
||||
@@ -81,6 +82,7 @@ All commands in `lisp-mode-shared-map' are inherited by this map.")
|
||||
(put 'fn 'lisp-indent-function 'defun)
|
||||
(put 'pub 'lisp-indent-function 'defun)
|
||||
(put 'defmacro 'lisp-indent-function 'defun)
|
||||
(put 'record 'lisp-indent-function 'defun)
|
||||
(put 'struct 'lisp-indent-function 'defun)
|
||||
(put 'union 'lisp-indent-function 'defun)
|
||||
(put 'var 'lisp-indent-function 0)
|
||||
|
||||
@@ -100,7 +100,7 @@
|
||||
(('pub 'var . _)
|
||||
(match-let ((('pub 'var name type . _) (strip-header-comments form 4)))
|
||||
(cons (copy-form-source! form `(extern var ,name ,type)) acc)))
|
||||
(('pub (or 'define 'defmacro 'enum 'import 'include 'struct 'typedef 'union) . _)
|
||||
(('pub (or 'define 'defmacro 'enum 'import 'include 'record 'struct 'typedef 'union) . _)
|
||||
(cons (copy-form-source! form (cdr form)) acc))
|
||||
(('pub . _)
|
||||
(sex-error form "pub must be followed by a definition" form))
|
||||
|
||||
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