| 1 |
#!r6rs |
| 2 |
;; Copyright (C) William D Clinger 2008. All Rights Reserved. |
| 3 |
;; |
| 4 |
;; Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
;; of this software and associated documentation files (the "Software"), to deal |
| 6 |
;; in the Software without restriction, including without limitation the rights |
| 7 |
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
;; copies of the Software, and to permit persons to whom the Software is |
| 9 |
;; furnished to do so, subject to the following conditions: |
| 10 |
;; |
| 11 |
;; The above copyright notice and this permission notice shall be included in |
| 12 |
;; all copies or substantial portions of the Software. |
| 13 |
;; |
| 14 |
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. REMEMBER, THERE IS NO |
| 17 |
;; SCHEME UNDERGROUND. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 18 |
;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 |
;; CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 20 |
;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
|
| 22 |
(library (srfi :99 records syntactic) |
| 23 |
|
| 24 |
(export define-record-type) |
| 25 |
|
| 26 |
(import (for (rnrs base) run expand) |
| 27 |
(for (rnrs lists) run expand) |
| 28 |
(for (rnrs syntax-case) run expand) |
| 29 |
(srfi :99 records procedural)) |
| 30 |
|
| 31 |
(define-syntax define-record-type |
| 32 |
(syntax-rules () |
| 33 |
((_ (type-name parent) constructor-spec predicate-spec . field-specs) |
| 34 |
(define-record-type-helper0 |
| 35 |
type-name parent constructor-spec predicate-spec . field-specs)) |
| 36 |
((_ type-name constructor-spec predicate-spec . field-specs) |
| 37 |
(define-record-type-helper0 |
| 38 |
type-name #f constructor-spec predicate-spec . field-specs)))) |
| 39 |
|
| 40 |
(define-syntax define-record-type-helper0 |
| 41 |
(lambda (x) |
| 42 |
|
| 43 |
; Given syntax objects, passes them to helper macro. |
| 44 |
|
| 45 |
(define (construct-record-type-definitions |
| 46 |
tname fields parent cspec pred afields mfields) |
| 47 |
(let () |
| 48 |
|
| 49 |
(define (frob x) |
| 50 |
(cond ((identifier? x) |
| 51 |
x) |
| 52 |
((pair? x) |
| 53 |
(cons (frob (car x)) (frob (cdr x)))) |
| 54 |
((vector? x) |
| 55 |
(vector-map frob x)) |
| 56 |
((symbol? x) |
| 57 |
(datum->syntax tname x)) |
| 58 |
(else |
| 59 |
x))) |
| 60 |
|
| 61 |
#`(#,(frob #'define-record-type-helper) |
| 62 |
#,(frob tname) |
| 63 |
#,(frob fields) |
| 64 |
#,(frob parent) |
| 65 |
#,(frob cspec) |
| 66 |
#,(frob pred) |
| 67 |
#,(frob afields) |
| 68 |
#,(frob mfields)))) |
| 69 |
|
| 70 |
; Given a syntax object that represents a non-empty list, |
| 71 |
; returns the syntax object for its first element. |
| 72 |
|
| 73 |
(define (syntax-car x) |
| 74 |
(syntax-case x () |
| 75 |
((x0 x1 ...) |
| 76 |
#'x0))) |
| 77 |
|
| 78 |
; Given a syntax object that represents a non-empty list, |
| 79 |
; returns the syntax object obtained by omitting the first |
| 80 |
; element of that list. |
| 81 |
|
| 82 |
(define (syntax-cdr x) |
| 83 |
(syntax-case x () |
| 84 |
((x0 x1 ...) |
| 85 |
#'(x1 ...)))) |
| 86 |
|
| 87 |
; Given a syntax object that represents a non-empty list, |
| 88 |
; returns the corresponding list of syntax objects. |
| 89 |
|
| 90 |
(define (syntax->list x) |
| 91 |
(syntax-case x () |
| 92 |
(() |
| 93 |
'()) |
| 94 |
((x0 . x1) |
| 95 |
(cons #'x0 (syntax->list #'x1))))) |
| 96 |
|
| 97 |
(define (complain) |
| 98 |
(syntax-violation 'define-record-type "illegal syntax" x)) |
| 99 |
|
| 100 |
; tname and pname are always identifiers here. |
| 101 |
|
| 102 |
(syntax-case x () |
| 103 |
((_ tname pname constructor-spec predicate-spec . field-specs) |
| 104 |
(let* ((type-name (syntax->datum #'tname)) |
| 105 |
(cspec (syntax->datum #'constructor-spec)) |
| 106 |
(pspec (syntax->datum #'predicate-spec)) |
| 107 |
(fspecs (syntax->datum #'field-specs)) |
| 108 |
(type-name-string |
| 109 |
(begin (if (not (symbol? type-name)) |
| 110 |
(complain)) |
| 111 |
(symbol->string type-name))) |
| 112 |
(constructor-name |
| 113 |
(cond ((eq? cspec #f) |
| 114 |
#'constructor-spec) |
| 115 |
((eq? cspec #t) |
| 116 |
(datum->syntax |
| 117 |
#'tname |
| 118 |
(string->symbol |
| 119 |
(string-append "make-" type-name-string)))) |
| 120 |
((symbol? cspec) |
| 121 |
#'constructor-spec) |
| 122 |
((and (pair? cspec) (symbol? (car cspec))) |
| 123 |
(syntax-car #'constructor-spec)) |
| 124 |
(else (complain)))) |
| 125 |
(constructor-args |
| 126 |
(cond ((pair? cspec) |
| 127 |
(if (not (for-all symbol? cspec)) |
| 128 |
(complain) |
| 129 |
(list->vector |
| 130 |
(syntax->list (syntax-cdr #'constructor-spec))))) |
| 131 |
(else #f))) |
| 132 |
(new-constructor-spec |
| 133 |
(if constructor-args |
| 134 |
(list constructor-name constructor-args) |
| 135 |
constructor-name)) |
| 136 |
(predicate-name |
| 137 |
(cond ((eq? pspec #f) |
| 138 |
#'predicate-spec) |
| 139 |
((eq? pspec #t) |
| 140 |
(datum->syntax |
| 141 |
#'tname |
| 142 |
(string->symbol |
| 143 |
(string-append type-name-string "?")))) |
| 144 |
((symbol? pspec) |
| 145 |
#'predicate-spec) |
| 146 |
(else (complain)))) |
| 147 |
(field-specs |
| 148 |
(map (lambda (fspec field-spec) |
| 149 |
(cond ((symbol? fspec) |
| 150 |
(list 'immutable |
| 151 |
fspec |
| 152 |
(string->symbol |
| 153 |
(string-append |
| 154 |
type-name-string |
| 155 |
"-" |
| 156 |
(symbol->string fspec))))) |
| 157 |
((not (pair? fspec)) |
| 158 |
(complain)) |
| 159 |
((not (list? fspec)) |
| 160 |
(complain)) |
| 161 |
((not (for-all symbol? fspec)) |
| 162 |
(complain)) |
| 163 |
((null? (cdr fspec)) |
| 164 |
(list 'mutable |
| 165 |
(car fspec) |
| 166 |
(string->symbol |
| 167 |
(string-append |
| 168 |
type-name-string |
| 169 |
"-" |
| 170 |
(symbol->string (car fspec)))) |
| 171 |
(string->symbol |
| 172 |
(string-append |
| 173 |
type-name-string |
| 174 |
"-" |
| 175 |
(symbol->string (car fspec)) |
| 176 |
"-set!")))) |
| 177 |
((null? (cddr fspec)) |
| 178 |
(list 'immutable |
| 179 |
(car fspec) |
| 180 |
(syntax-car (syntax-cdr field-spec)))) |
| 181 |
((null? (cdddr fspec)) |
| 182 |
(list 'mutable |
| 183 |
(car fspec) |
| 184 |
(syntax-car (syntax-cdr field-spec)) |
| 185 |
(syntax-car (syntax-cdr |
| 186 |
(syntax-cdr field-spec))))) |
| 187 |
(else (complain)))) |
| 188 |
fspecs |
| 189 |
(syntax->list #'field-specs))) |
| 190 |
|
| 191 |
(fields (list->vector (map cadr field-specs))) |
| 192 |
|
| 193 |
(accessor-fields |
| 194 |
(map (lambda (x) (list (caddr x) (cadr x))) |
| 195 |
(filter (lambda (x) (>= (length x) 3)) |
| 196 |
field-specs))) |
| 197 |
|
| 198 |
(mutator-fields |
| 199 |
(map (lambda (x) (list (cadddr x) (cadr x))) |
| 200 |
(filter (lambda (x) (= (length x) 4)) |
| 201 |
field-specs)))) |
| 202 |
|
| 203 |
(construct-record-type-definitions |
| 204 |
#'tname |
| 205 |
fields |
| 206 |
#'pname |
| 207 |
new-constructor-spec |
| 208 |
predicate-name |
| 209 |
accessor-fields |
| 210 |
mutator-fields)))))) |
| 211 |
|
| 212 |
(define-syntax define-record-type-helper |
| 213 |
(syntax-rules () |
| 214 |
|
| 215 |
((_ type-name fields parent #f predicate |
| 216 |
((accessor field) ...) ((mutator mutable-field) ...)) |
| 217 |
(define-record-type-helper |
| 218 |
type-name fields parent ignored predicate |
| 219 |
((accessor field) ...) ((mutator mutable-field) ...))) |
| 220 |
|
| 221 |
((_ type-name fields parent constructor #f |
| 222 |
((accessor field) ...) ((mutator mutable-field) ...)) |
| 223 |
(define-record-type-helper |
| 224 |
type-name fields parent constructor ignored |
| 225 |
((accessor field) ...) ((mutator mutable-field) ...))) |
| 226 |
|
| 227 |
((_ type-name fields parent (constructor args) predicate |
| 228 |
((accessor field) ...) ((mutator mutable-field) ...)) |
| 229 |
(begin (define type-name (make-rtd 'type-name 'fields parent)) |
| 230 |
(define constructor (rtd-constructor type-name 'args)) |
| 231 |
(define predicate (rtd-predicate type-name)) |
| 232 |
(define accessor (rtd-accessor type-name 'field)) |
| 233 |
... |
| 234 |
(define mutator (rtd-mutator type-name 'mutable-field)) |
| 235 |
...)) |
| 236 |
|
| 237 |
((_ type-name fields parent constructor predicate |
| 238 |
((accessor field) ...) ((mutator mutable-field) ...)) |
| 239 |
(begin (define type-name (make-rtd 'type-name 'fields parent)) |
| 240 |
(define constructor (rtd-constructor type-name)) |
| 241 |
(define predicate (rtd-predicate type-name)) |
| 242 |
(define accessor (rtd-accessor type-name 'field)) |
| 243 |
... |
| 244 |
(define mutator (rtd-mutator type-name 'mutable-field)) |
| 245 |
...)))) |
| 246 |
|
| 247 |
) ; srfi :99 records syntactic |