| 1 |
#!r6rs |
| 2 |
;; Copyright 2010 Derick Eddington. My MIT-style license is in the file named |
| 3 |
;; LICENSE from the original collection this file is distributed with. |
| 4 |
|
| 5 |
(library (srfi :9 records) |
| 6 |
(export |
| 7 |
(rename (my:define-record-type define-record-type))) |
| 8 |
(import |
| 9 |
(rnrs)) |
| 10 |
|
| 11 |
(define-syntax my:define-record-type |
| 12 |
(lambda (stx) |
| 13 |
(syntax-case stx () |
| 14 |
((_ type (constructor constructor-tag ...) |
| 15 |
predicate |
| 16 |
(field-tag accessor setter ...) ...) |
| 17 |
(and (for-all identifier? |
| 18 |
#'(type constructor constructor-tag ... predicate |
| 19 |
field-tag ... accessor ... setter ... ...)) |
| 20 |
(for-all (lambda (s) (<= 0 (length s) 1)) |
| 21 |
#'((setter ...) ...)) |
| 22 |
(for-all (lambda (ct) |
| 23 |
(memp (lambda (ft) (bound-identifier=? ct ft)) |
| 24 |
#'(field-tag ...))) |
| 25 |
#'(constructor-tag ...))) |
| 26 |
(with-syntax (((field-clause ...) |
| 27 |
(map (lambda (clause) |
| 28 |
(if (= 2 (length clause)) |
| 29 |
#`(immutable . #,clause) |
| 30 |
#`(mutable . #,clause))) |
| 31 |
#'((field-tag accessor setter ...) ...))) |
| 32 |
((unspec-tag ...) |
| 33 |
(remp (lambda (ft) |
| 34 |
(memp (lambda (ct) (bound-identifier=? ft ct)) |
| 35 |
#'(constructor-tag ...))) |
| 36 |
#'(field-tag ...)))) |
| 37 |
#'(define-record-type (type constructor predicate) |
| 38 |
(protocol (lambda (ctor) |
| 39 |
(lambda (constructor-tag ...) |
| 40 |
(define unspec-tag) ... |
| 41 |
(ctor field-tag ...)))) |
| 42 |
(fields field-clause ...))))))) |
| 43 |
) |