| 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 procedural) |
| 23 |
|
| 24 |
(export make-rtd rtd? rtd-constructor |
| 25 |
rtd-predicate rtd-accessor rtd-mutator) |
| 26 |
|
| 27 |
(import (rnrs base) |
| 28 |
(rnrs lists) |
| 29 |
(rnrs records procedural) |
| 30 |
(srfi :99 records inspection)) |
| 31 |
|
| 32 |
; Note: the options are permitted by ERR5RS, |
| 33 |
; but are not part of ERR5RS. |
| 34 |
|
| 35 |
(define (make-rtd name fieldspecs . rest) |
| 36 |
(let* ((parent (if (null? rest) #f (car rest))) |
| 37 |
(options (if (null? rest) '() (cdr rest))) |
| 38 |
(sealed? (and (memq 'sealed options) #t)) |
| 39 |
(opaque? (and (memq 'opaque options) #t)) |
| 40 |
(uid (let ((probe (memq 'uid options))) |
| 41 |
(if (and probe (not (null? (cdr probe)))) |
| 42 |
(cadr probe) |
| 43 |
#f)))) |
| 44 |
(make-record-type-descriptor |
| 45 |
name |
| 46 |
parent |
| 47 |
uid |
| 48 |
sealed? |
| 49 |
opaque? |
| 50 |
(vector-map (lambda (fieldspec) |
| 51 |
(if (symbol? fieldspec) |
| 52 |
(list 'mutable fieldspec) |
| 53 |
fieldspec)) |
| 54 |
;; NB: hack to get srfi :131 working |
| 55 |
(if (list? fieldspecs) |
| 56 |
(list->vector fieldspecs) |
| 57 |
fieldspecs))))) |
| 58 |
|
| 59 |
(define rtd? record-type-descriptor?) |
| 60 |
|
| 61 |
(define (rtd-constructor rtd . rest) |
| 62 |
|
| 63 |
; Computes permutation and allocates permutation buffer |
| 64 |
; when the constructor is created, not when the constructor |
| 65 |
; is called. More error checking is recommended. |
| 66 |
|
| 67 |
(define (make-constructor fieldspecs allnames maker) |
| 68 |
(let* ((k (length fieldspecs)) |
| 69 |
(n (length allnames)) |
| 70 |
(buffer (make-vector n)) |
| 71 |
(reverse-all-names (reverse allnames))) |
| 72 |
|
| 73 |
(define (position fieldname) |
| 74 |
(let ((names (memq fieldname reverse-all-names))) |
| 75 |
(assert names) |
| 76 |
(- (length names) 1))) |
| 77 |
|
| 78 |
(let ((indexes (map position fieldspecs))) |
| 79 |
|
| 80 |
; The following can be made quite efficient by |
| 81 |
; hand-coding it in some lower-level language, |
| 82 |
; e.g. Larceny's mal. Even case-lambda would |
| 83 |
; be good enough in most systems. |
| 84 |
|
| 85 |
(lambda args |
| 86 |
(assert (= (length args) k)) |
| 87 |
(for-each (lambda (arg posn) |
| 88 |
(vector-set! buffer posn arg)) |
| 89 |
args indexes) |
| 90 |
(apply maker (vector->list buffer)))))) |
| 91 |
|
| 92 |
(if (null? rest) |
| 93 |
(record-constructor |
| 94 |
(make-record-constructor-descriptor rtd #f #f)) |
| 95 |
(begin (assert (null? (cdr rest))) |
| 96 |
(make-constructor |
| 97 |
(vector->list (car rest)) |
| 98 |
(vector->list (rtd-all-field-names rtd)) |
| 99 |
(record-constructor |
| 100 |
(make-record-constructor-descriptor rtd #f #f)))))) |
| 101 |
|
| 102 |
(define rtd-predicate record-predicate) |
| 103 |
|
| 104 |
(define (rtd-accessor rtd0 fieldname) |
| 105 |
(define (loop rtd) |
| 106 |
(if (rtd? rtd) |
| 107 |
(let* ((names (vector->list (rtd-field-names rtd))) |
| 108 |
(probe (memq fieldname names))) |
| 109 |
(if probe |
| 110 |
(record-accessor rtd (- (length names) (length probe))) |
| 111 |
(loop (rtd-parent rtd)))) |
| 112 |
(assertion-violation 'rtd-accessor |
| 113 |
"illegal argument" rtd0 fieldname))) |
| 114 |
(loop rtd0)) |
| 115 |
|
| 116 |
(define (rtd-mutator rtd0 fieldname) |
| 117 |
(define (loop rtd) |
| 118 |
(if (rtd? rtd) |
| 119 |
(let* ((names (vector->list (rtd-field-names rtd))) |
| 120 |
(probe (memq fieldname names))) |
| 121 |
(if probe |
| 122 |
(record-mutator rtd (- (length names) (length probe))) |
| 123 |
(loop (rtd-parent rtd)))) |
| 124 |
(assertion-violation 'rtd-mutator |
| 125 |
"illegal argument" rtd0 fieldname))) |
| 126 |
(loop rtd0)) |
| 127 |
|
| 128 |
) |