diff --git a/Makefile b/Makefile index a0924ab3..c1b6a370 100644 --- a/Makefile +++ b/Makefile @@ -109,6 +109,7 @@ SEX_TEST_PROGRAMS = c99 \ lambdas \ lists \ operators \ + records \ serialize \ type-shapes \ unicode \ diff --git a/semen.scm b/semen.scm index e1952eab..96bb6e4c 100644 --- a/semen.scm +++ b/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)))) diff --git a/sex-mode.el b/sex-mode.el index b2197d02..2b45b6f9 100644 --- a/sex-mode.el +++ b/sex-mode.el @@ -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) diff --git a/sex-modules.scm b/sex-modules.scm index 744e68ad..595a6dcc 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -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)) diff --git a/tests/sex-programs/records.sex b/tests/sex-programs/records.sex new file mode 100644 index 00000000..9c0eefd0 --- /dev/null +++ b/tests/sex-programs/records.sex @@ -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))))