2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
|
|
|
;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum
|
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-06-27 06:15:51 -04:00
|
|
|
|
2007-10-12 04:01:11 -04:00
|
|
|
(library (ikarus records procedural)
|
2007-06-27 06:15:51 -04:00
|
|
|
(export
|
2007-10-12 04:01:11 -04:00
|
|
|
make-record-type-descriptor record-type-descriptor?
|
|
|
|
make-record-constructor-descriptor record-accessor
|
|
|
|
record-mutator record-constructor record-predicate record?
|
|
|
|
record-rtd record-type-name record-type-parent record-type-uid
|
|
|
|
record-type-generative? record-type-sealed? record-type-opaque?
|
2007-10-23 16:38:34 -04:00
|
|
|
record-type-field-names record-field-mutable? rtd-subtype? rtd?)
|
2007-06-27 06:15:51 -04:00
|
|
|
(import
|
2007-10-12 04:01:11 -04:00
|
|
|
(except (ikarus)
|
|
|
|
record-constructor record-predicate record? record-type-name
|
2007-10-25 02:19:53 -04:00
|
|
|
record-type-parent record-type-descriptor? record-rtd
|
|
|
|
record-type-uid record-type-sealed? record-type-opaque?
|
|
|
|
record-type-generative? make-record-type-descriptor
|
|
|
|
make-record-constructor-descriptor record-accessor
|
|
|
|
record-mutator
|
2007-10-23 16:38:34 -04:00
|
|
|
record-type-field-names record-field-mutable?
|
|
|
|
rtd? rtd-subtype?)
|
2007-10-12 04:01:11 -04:00
|
|
|
(ikarus system $structs))
|
2007-06-27 06:15:51 -04:00
|
|
|
|
2007-10-12 04:01:11 -04:00
|
|
|
(define-struct rtd
|
|
|
|
(name size old-fields printer-proc symbol parent sealed? opaque? uid fields))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
2007-06-27 06:15:51 -04:00
|
|
|
(define rtd-alist '())
|
|
|
|
(define (intern-rtd! uid rtd)
|
|
|
|
(set! rtd-alist (cons (cons uid rtd) rtd-alist)))
|
|
|
|
(define (lookup-rtd uid)
|
|
|
|
(cond
|
|
|
|
[(assq uid rtd-alist) => cdr]
|
|
|
|
[else #f]))
|
|
|
|
|
|
|
|
(define (record-type-descriptor? x) (rtd? x))
|
|
|
|
|
2007-07-13 06:23:54 -04:00
|
|
|
(define (record? x)
|
2007-10-12 04:01:11 -04:00
|
|
|
(and ($struct? x)
|
|
|
|
(let ([rtd ($struct-rtd x)])
|
2007-07-13 06:23:54 -04:00
|
|
|
(and (rtd? rtd)
|
|
|
|
(not (rtd-opaque? rtd))))))
|
|
|
|
|
|
|
|
(define (record-rtd x)
|
2007-10-12 04:01:11 -04:00
|
|
|
(if ($struct? x)
|
|
|
|
(let ([rtd ($struct-rtd x)])
|
2007-07-13 06:23:54 -04:00
|
|
|
(if (rtd? rtd)
|
|
|
|
(if (not (rtd-opaque? rtd))
|
|
|
|
rtd
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-rtd "record is opaque"))
|
|
|
|
(die 'record-rtd "not a record" x)))
|
|
|
|
(die 'record-rtd "not a record" x)))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
|
|
|
(define (record-type-name x)
|
|
|
|
(if (rtd? x)
|
|
|
|
(rtd-name x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-type-name "not an rtd" x)))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
|
|
|
(define (record-type-parent x)
|
|
|
|
(if (rtd? x)
|
|
|
|
(rtd-parent x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-type-parent "not an rtd" x)))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
|
|
|
(define (record-type-uid x)
|
|
|
|
(if (rtd? x)
|
|
|
|
(rtd-uid x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-type-uid "not an rtd" x)))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
|
|
|
(define (record-type-sealed? x)
|
|
|
|
(if (rtd? x)
|
|
|
|
(rtd-sealed? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-type-sealed? "not an rtd" x)))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
|
|
|
(define (record-type-opaque? x)
|
|
|
|
(if (rtd? x)
|
|
|
|
(rtd-opaque? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-type-opaque? "not an rtd" x)))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
|
|
|
(define (record-type-generative? x)
|
|
|
|
(if (rtd? x)
|
|
|
|
(not (rtd-sealed? x))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-type-generative? "not an rtd" x)))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
|
|
|
(define (record-type-field-names x)
|
|
|
|
(if (rtd? x)
|
|
|
|
(let ([v (rtd-fields x)])
|
|
|
|
(let ([n (vector-length v)])
|
|
|
|
(let f ([x (make-vector n)] [v v] [n n] [i 0])
|
|
|
|
(if (= i n)
|
|
|
|
x
|
|
|
|
(begin
|
|
|
|
(vector-set! x i (cdr (vector-ref v i)))
|
|
|
|
(f x v n (fxadd1 i)))))))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-type-field-names "not an rtd" x)))
|
2007-07-13 06:23:54 -04:00
|
|
|
|
|
|
|
|
2007-06-27 06:15:51 -04:00
|
|
|
(module (make-record-type-descriptor)
|
|
|
|
(define who 'make-record-type-descriptor)
|
2007-07-13 06:23:54 -04:00
|
|
|
(define (make-rtd-aux name parent uid sealed? opaque?
|
|
|
|
parent-size fields)
|
|
|
|
(make-rtd name (+ parent-size (vector-length fields))
|
|
|
|
#f #f #f parent sealed? opaque? uid fields))
|
|
|
|
(define (convert-fields sv)
|
2007-06-27 06:15:51 -04:00
|
|
|
(unless (vector? sv)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid fields argument" sv))
|
2007-07-13 06:23:54 -04:00
|
|
|
(let ([n2 (vector-length sv)])
|
|
|
|
(let ([v (make-vector n2)])
|
2007-06-27 06:15:51 -04:00
|
|
|
(let f ([i 0])
|
|
|
|
(unless (= i n2)
|
|
|
|
(let ([x (vector-ref sv i)])
|
|
|
|
(if (pair? x)
|
|
|
|
(let ([m/u (car x)] [x (cdr x)])
|
|
|
|
(if (pair? x)
|
|
|
|
(let ([name (car x)])
|
|
|
|
(unless (and (null? (cdr x)) (symbol? name))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid fields argument" sv))
|
2007-07-13 06:23:54 -04:00
|
|
|
(vector-set! v i
|
2007-06-27 06:15:51 -04:00
|
|
|
(cons (case m/u
|
|
|
|
[(mutable) #t]
|
|
|
|
[(immutable) #f]
|
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid fields argument" sv)])
|
2007-06-27 06:15:51 -04:00
|
|
|
name)))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid fields argument" sv)))
|
|
|
|
(die who "invalid fields argument" sv)))
|
2007-06-27 06:15:51 -04:00
|
|
|
(f (add1 i))))
|
|
|
|
v)))
|
|
|
|
(define generate-rtd
|
|
|
|
(lambda (name parent uid sealed? opaque? fields)
|
|
|
|
(cond
|
|
|
|
[(rtd? parent)
|
|
|
|
(when (rtd-sealed? parent)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "cannot extend sealed parent" parent))
|
2007-06-27 06:15:51 -04:00
|
|
|
(make-rtd-aux name parent uid sealed?
|
|
|
|
(or opaque? (rtd-opaque? parent))
|
2007-07-13 06:23:54 -04:00
|
|
|
(rtd-size parent)
|
|
|
|
(convert-fields fields))]
|
2007-06-27 06:15:51 -04:00
|
|
|
[(eqv? parent #f)
|
2007-07-13 06:23:54 -04:00
|
|
|
(make-rtd-aux name parent uid sealed? opaque? 0
|
|
|
|
(convert-fields fields))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "not a valid parent" parent)])))
|
2007-06-27 06:15:51 -04:00
|
|
|
(define (same-fields-as-rtd? fields rtd)
|
|
|
|
(let* ([fv (rtd-fields rtd)]
|
|
|
|
[n (vector-length fv)])
|
|
|
|
(and (vector? fields)
|
|
|
|
(= (vector-length fields) n)
|
|
|
|
(let f ([i 0])
|
|
|
|
(or (= i n)
|
|
|
|
(let ([a (vector-ref fields i)]
|
|
|
|
[b (vector-ref fv i)])
|
|
|
|
(and
|
|
|
|
(pair? a)
|
|
|
|
(case (car a)
|
|
|
|
[(mutable) (eqv? (car b) #t)]
|
|
|
|
[(immutable) (eqv? (car b) #f)]
|
|
|
|
[else #f])
|
|
|
|
(let ([a (cdr a)])
|
|
|
|
(and (pair? a)
|
|
|
|
(null? (cdr a))
|
|
|
|
(eq? (car a) (cdr b))))
|
|
|
|
(f (+ i 1)))))))))
|
|
|
|
(define make-nongenerative-rtd
|
|
|
|
(lambda (name parent uid sealed? opaque? fields)
|
|
|
|
(cond
|
|
|
|
[(lookup-rtd uid) =>
|
|
|
|
(lambda (rtd)
|
|
|
|
(unless
|
2007-12-01 22:32:19 -05:00
|
|
|
(and ; must not check name!
|
|
|
|
; (eqv? name (rtd-name rtd))
|
2007-06-27 06:15:51 -04:00
|
|
|
(eqv? parent (rtd-parent rtd))
|
|
|
|
(eqv? sealed? (rtd-sealed? rtd))
|
|
|
|
(eqv? opaque? (rtd-opaque? rtd))
|
|
|
|
(same-fields-as-rtd? fields rtd))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid arguments"))
|
2007-06-27 06:15:51 -04:00
|
|
|
rtd)]
|
|
|
|
[else
|
|
|
|
(let ([rtd (generate-rtd name parent uid sealed? opaque? fields)])
|
|
|
|
(intern-rtd! uid rtd)
|
|
|
|
rtd)])))
|
|
|
|
(define make-record-type-descriptor
|
|
|
|
(lambda (name parent uid sealed? opaque? fields)
|
|
|
|
(unless (symbol? name)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid record type name" name))
|
2007-06-27 06:15:51 -04:00
|
|
|
(unless (boolean? sealed?)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid sealed? argument" sealed?))
|
2007-06-27 06:15:51 -04:00
|
|
|
(unless (boolean? opaque?)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid opaque? argument" opaque?))
|
2007-06-27 06:15:51 -04:00
|
|
|
(cond
|
|
|
|
[(symbol? uid)
|
|
|
|
(make-nongenerative-rtd name parent uid sealed? opaque? fields)]
|
|
|
|
[(eqv? uid #f)
|
|
|
|
(generate-rtd name parent uid sealed? opaque? fields)]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "not a valid uid" uid)]))))
|
2007-06-27 06:15:51 -04:00
|
|
|
|
2007-10-12 04:01:11 -04:00
|
|
|
(define-struct rcd (rtd prcd proc))
|
2007-10-23 16:38:34 -04:00
|
|
|
|
2007-07-05 04:48:40 -04:00
|
|
|
(define (is-parent-of? prtd rtd)
|
|
|
|
(let ([p (rtd-parent rtd)])
|
|
|
|
(cond
|
|
|
|
[(eq? p prtd) #t]
|
|
|
|
[(not p) #f]
|
|
|
|
[else (is-parent-of? prtd p)])))
|
2007-10-23 16:38:34 -04:00
|
|
|
|
|
|
|
(define (rtd-subtype? rtd parent-rtd)
|
|
|
|
(unless (rtd? rtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'rtd-subtype? "not an rtd" rtd))
|
2007-10-23 16:38:34 -04:00
|
|
|
(unless (rtd? parent-rtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'rtd-substype? "not an rtd" parent-rtd))
|
2007-10-23 16:38:34 -04:00
|
|
|
(or (eq? rtd parent-rtd)
|
|
|
|
(is-parent-of? parent-rtd rtd)))
|
2007-07-05 04:48:40 -04:00
|
|
|
|
2007-06-27 06:15:51 -04:00
|
|
|
(define make-record-constructor-descriptor
|
2007-07-05 04:48:40 -04:00
|
|
|
(lambda (rtd prcd protocol)
|
2007-06-27 06:15:51 -04:00
|
|
|
(define who 'make-record-constructor-descriptor)
|
|
|
|
(unless (rtd? rtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a record type descriptor" rtd))
|
2007-07-05 04:48:40 -04:00
|
|
|
(unless (or (not protocol) (procedure? protocol))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid protocol" protocol))
|
2007-07-05 04:48:40 -04:00
|
|
|
(let ([prtd (rtd-parent rtd)])
|
|
|
|
(cond
|
|
|
|
[(not prcd)
|
|
|
|
(make-rcd rtd #f protocol)]
|
|
|
|
[(rcd? prcd)
|
|
|
|
(unless (is-parent-of? (rcd-rtd prcd) rtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "descriptor does not apply"
|
2007-07-05 04:48:40 -04:00
|
|
|
prcd rtd))
|
|
|
|
(make-rcd rtd prcd protocol)]
|
2007-07-13 06:23:54 -04:00
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid record constructor descriptor" prcd)]))))
|
2007-06-27 06:15:51 -04:00
|
|
|
|
|
|
|
(define (record-constructor rcd)
|
|
|
|
(define who 'record-constructor)
|
2007-10-29 16:18:11 -04:00
|
|
|
|
|
|
|
(define (split all-fields n)
|
|
|
|
(let f ([ls all-fields] [n n])
|
|
|
|
(if (zero? n)
|
|
|
|
(values '() ls)
|
|
|
|
(if (pair? ls)
|
|
|
|
(let-values ([(m p) (f (cdr ls) (- n 1))])
|
|
|
|
(values (cons (car ls) m) p))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'record-condtructor "insufficient arguments"
|
2007-10-29 16:18:11 -04:00
|
|
|
all-fields)))))
|
|
|
|
|
2007-10-23 00:28:21 -04:00
|
|
|
(define (constructor main-rtd size prcd proto)
|
|
|
|
(if (not prcd) ;;; base
|
|
|
|
(lambda (f*)
|
2007-11-21 01:24:13 -05:00
|
|
|
(let ([a-record-constructor
|
|
|
|
(lambda flds
|
|
|
|
(let ([n (rtd-size main-rtd)])
|
|
|
|
(unless (= (length flds) size)
|
2007-12-15 08:22:49 -05:00
|
|
|
(apply die
|
2007-11-21 01:39:31 -05:00
|
|
|
'a-record-constructor
|
|
|
|
(format
|
|
|
|
"expected ~a args, got ~a instead"
|
|
|
|
n (length flds))
|
|
|
|
flds))
|
2007-11-21 01:24:13 -05:00
|
|
|
(let ([r ($make-struct main-rtd n)])
|
|
|
|
(let f ([i 0] [r r] [flds flds] [f* f*])
|
|
|
|
(cond
|
|
|
|
[(null? flds)
|
|
|
|
(if (null? f*)
|
|
|
|
r
|
|
|
|
(f i r (car f*) (cdr f*)))]
|
|
|
|
[else
|
|
|
|
($struct-set! r i (car flds))
|
|
|
|
(f (add1 i) r (cdr flds) f*)])))))])
|
|
|
|
(if proto
|
|
|
|
(proto a-record-constructor)
|
|
|
|
a-record-constructor)))
|
2007-10-23 00:28:21 -04:00
|
|
|
(let ([pprcd (rcd-prcd prcd)]
|
|
|
|
[sz (rtd-size (rcd-rtd prcd))])
|
|
|
|
(let ([p (constructor main-rtd sz pprcd (rcd-proc prcd))]
|
2007-10-29 16:18:11 -04:00
|
|
|
[n (- size sz)]
|
2007-11-21 01:24:13 -05:00
|
|
|
[protocol
|
2007-10-29 16:18:11 -04:00
|
|
|
(if proto
|
|
|
|
proto
|
|
|
|
(lambda (new)
|
2007-11-21 01:24:13 -05:00
|
|
|
(let ([a-record-constructor
|
|
|
|
(lambda all-fields
|
|
|
|
(let-values ([(parent-fields myfields)
|
|
|
|
(split all-fields
|
|
|
|
(- (length all-fields)
|
|
|
|
(- size sz)))])
|
|
|
|
(apply (apply new parent-fields)
|
|
|
|
myfields)))])
|
|
|
|
a-record-constructor)))])
|
2007-10-23 00:28:21 -04:00
|
|
|
(lambda (f*)
|
2007-11-21 01:24:13 -05:00
|
|
|
(protocol
|
2007-10-29 16:18:11 -04:00
|
|
|
(lambda fmls
|
2007-10-23 16:38:34 -04:00
|
|
|
(lambda flds
|
|
|
|
(unless (= (length flds) n)
|
2007-12-15 08:22:49 -05:00
|
|
|
(apply die
|
2007-11-21 01:39:31 -05:00
|
|
|
'a-record-constructor
|
|
|
|
(format
|
|
|
|
"expected ~a args, got ~a instead"
|
|
|
|
n (length flds))
|
|
|
|
flds))
|
2007-10-29 16:18:11 -04:00
|
|
|
(apply (p (cons flds f*)) fmls)))))))))
|
2007-06-27 06:15:51 -04:00
|
|
|
(unless (rcd? rcd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a record constructor descriptor" rcd))
|
2007-06-27 06:15:51 -04:00
|
|
|
(let ([rtd (rcd-rtd rcd)]
|
2007-10-23 00:28:21 -04:00
|
|
|
[prcd (rcd-prcd rcd)]
|
|
|
|
[proto (rcd-proc rcd)])
|
|
|
|
((constructor rtd (rtd-size rtd) prcd proto) '())))
|
|
|
|
|
2007-07-05 09:59:06 -04:00
|
|
|
|
2007-06-27 06:15:51 -04:00
|
|
|
(define (record-accessor rtd k)
|
|
|
|
(define who 'record-accessor)
|
|
|
|
(unless (rtd? rtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not an rtd" rtd))
|
2007-06-27 06:15:51 -04:00
|
|
|
(unless (and (fixnum? k) (fx>= k 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid index" k))
|
2007-06-27 06:15:51 -04:00
|
|
|
(let ([sz (rtd-size rtd)]
|
|
|
|
[p (rtd-parent rtd)])
|
|
|
|
(let ([i (if p (+ k (rtd-size p)) k)])
|
|
|
|
(unless (fx< i sz)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid index" k))
|
2007-11-21 01:24:13 -05:00
|
|
|
(let ([a-record-accessor
|
|
|
|
(lambda (x)
|
2007-06-27 06:15:51 -04:00
|
|
|
(cond
|
2007-11-21 01:24:13 -05:00
|
|
|
[($struct/rtd? x rtd) ($struct-ref x i)]
|
|
|
|
[($struct? x)
|
|
|
|
(let ([xrtd ($struct-rtd x)])
|
|
|
|
(unless (rtd? xrtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid type" x rtd))
|
2007-11-21 01:24:13 -05:00
|
|
|
(let f ([prtd (rtd-parent xrtd)] [rtd rtd] [x x] [i i])
|
|
|
|
(cond
|
|
|
|
[(eq? prtd rtd) ($struct-ref x i)]
|
|
|
|
[(not prtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid type" x rtd)]
|
2007-11-21 01:24:13 -05:00
|
|
|
[else (f (rtd-parent prtd) rtd x i)])))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "invalid type" x rtd)]))])
|
2007-11-21 01:24:13 -05:00
|
|
|
a-record-accessor))))
|
2007-06-27 06:15:51 -04:00
|
|
|
|
|
|
|
(define (record-mutator rtd k)
|
|
|
|
(define who 'record-mutator)
|
|
|
|
(unless (rtd? rtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not an rtd" rtd))
|
2007-06-27 06:15:51 -04:00
|
|
|
(unless (and (fixnum? k) (fx>= k 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid index" k))
|
2007-06-27 06:15:51 -04:00
|
|
|
(let ([sz (rtd-size rtd)]
|
|
|
|
[p (rtd-parent rtd)])
|
|
|
|
(let ([i (if p (+ k (rtd-size p)) k)])
|
|
|
|
(unless (fx< i sz)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid index" k))
|
2007-06-27 06:15:51 -04:00
|
|
|
(unless (car (vector-ref (rtd-fields rtd) k))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "field is not mutable" k rtd))
|
2007-11-21 01:24:13 -05:00
|
|
|
(let ([a-record-mutator
|
|
|
|
(lambda (x v)
|
2007-06-27 06:15:51 -04:00
|
|
|
(cond
|
2007-11-21 01:24:13 -05:00
|
|
|
[($struct/rtd? x rtd) ($struct-set! x i v)]
|
|
|
|
[($struct? x)
|
|
|
|
(let ([xrtd ($struct-rtd x)])
|
|
|
|
(unless (rtd? xrtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid type" x rtd))
|
2007-11-21 01:24:13 -05:00
|
|
|
(let f ([prtd (rtd-parent xrtd)] [rtd rtd] [x x] [i i] [v v])
|
|
|
|
(cond
|
|
|
|
[(eq? prtd rtd) ($struct-set! x i v)]
|
|
|
|
[(not prtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "invalid type" x rtd)]
|
2007-11-21 01:24:13 -05:00
|
|
|
[else (f (rtd-parent prtd) rtd x i v)])))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die who "invalid type" x rtd)]))])
|
2007-11-21 01:24:13 -05:00
|
|
|
a-record-mutator))))
|
2007-06-27 06:15:51 -04:00
|
|
|
|
|
|
|
(define (record-predicate rtd)
|
|
|
|
(define who 'record-predicate)
|
|
|
|
(unless (rtd? rtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not an rtd" rtd))
|
2007-06-27 06:15:51 -04:00
|
|
|
(let ([sz (rtd-size rtd)]
|
|
|
|
[p (rtd-parent rtd)])
|
2007-11-21 01:24:13 -05:00
|
|
|
(let ([a-record-predicate
|
|
|
|
(lambda (x)
|
|
|
|
(cond
|
|
|
|
[($struct/rtd? x rtd) #t]
|
|
|
|
[($struct? x)
|
|
|
|
(let ([xrtd ($struct-rtd x)])
|
|
|
|
(and (rtd? xrtd)
|
|
|
|
(let f ([prtd (rtd-parent xrtd)] [rtd rtd])
|
|
|
|
(cond
|
|
|
|
[(eq? prtd rtd) #t]
|
|
|
|
[(not prtd) #f]
|
|
|
|
[else (f (rtd-parent prtd) rtd)]))))]
|
|
|
|
[else #f]))])
|
|
|
|
a-record-predicate)))
|
2007-06-27 06:15:51 -04:00
|
|
|
|
2007-10-12 04:01:11 -04:00
|
|
|
|
|
|
|
(define (record-field-mutable? rtd k)
|
|
|
|
(define who 'record-field-mutable?)
|
|
|
|
(unless (rtd? rtd)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not an rtd" rtd))
|
2007-10-12 04:01:11 -04:00
|
|
|
(unless (and (fixnum? k) (fx>= k 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid index" k))
|
2007-10-12 04:01:11 -04:00
|
|
|
(let ([sz (rtd-size rtd)]
|
|
|
|
[p (rtd-parent rtd)])
|
|
|
|
(let ([i (if p (+ k (rtd-size p)) k)])
|
|
|
|
(unless (fx< i sz)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a valid index" k))
|
2007-10-12 04:01:11 -04:00
|
|
|
(car (vector-ref (rtd-fields rtd) k)))))
|
|
|
|
|
|
|
|
(set-rtd-printer! (type-descriptor rtd)
|
2007-07-13 06:23:54 -04:00
|
|
|
(lambda (x p)
|
|
|
|
(display (format "#<record-type-descriptor ~s>" (rtd-name x)) p)))
|
2007-06-27 06:15:51 -04:00
|
|
|
|
2007-10-12 04:01:11 -04:00
|
|
|
(set-rtd-printer! (type-descriptor rcd)
|
2007-07-13 06:23:54 -04:00
|
|
|
(lambda (x p)
|
|
|
|
(display (format "#<record-constructor-descriptor ~s>"
|
|
|
|
(rtd-name (rcd-rtd x))) p)))
|
2007-06-27 06:15:51 -04:00
|
|
|
|
|
|
|
)
|
2007-10-23 00:28:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
#!eof
|
|
|
|
|
|
|
|
rtd0 fields=4
|
|
|
|
proto0 =
|
|
|
|
(lambda (n)
|
|
|
|
(lambda (p0-fmls ...)
|
|
|
|
(n f0 f1 f2 f3)))
|
|
|
|
|
|
|
|
rtd1 fields=2
|
|
|
|
proto1 =
|
|
|
|
(lambda (n)
|
|
|
|
(lambda (p1-fmls ...)
|
|
|
|
((n p0-acts ...) f4 f5)))
|
|
|
|
|
|
|
|
rtd2 fields=1
|
|
|
|
proto2 =
|
|
|
|
(lambda (n)
|
|
|
|
(lambda (p2-fmls ...)
|
|
|
|
((n p1-acts ...) f6)))
|
|
|
|
|
|
|
|
|
|
|
|
(record-constructor rcd2)
|
|
|
|
==
|
|
|
|
(proto2 (lambda p1-fml*
|
|
|
|
(lambda (f6)
|
|
|
|
(apply (proto1 (lambda p0-fml*
|
|
|
|
(lambda (f4 f5)
|
|
|
|
(apply (proto0 (lambda (f0 f1 f2 f3)
|
|
|
|
($record rtd2 f0 f1 f2 f3 f4 f5 f6)))
|
|
|
|
p0-fml*))))
|
|
|
|
p1-fml*))))
|
|
|
|
|
|
|
|
new0 = (lambda (f0 f1 f2 f3 f4 f5 f6)
|
|
|
|
($record rtd2 f0 f1 f2 f3 f4 f5 f6))
|
|
|
|
|
|
|
|
(record-constructor rcd2)
|
|
|
|
==
|
|
|
|
(proto2 (lambda p1-fml*
|
|
|
|
(lambda (f6)
|
|
|
|
(apply (proto1 (lambda p0-fml*
|
|
|
|
(lambda (f4 f5)
|
|
|
|
(apply (proto0 (lambda (f0 f1 f2 f3)
|
|
|
|
(new0 f0 f1 f2 f3 f4 f5 f6)))
|
|
|
|
p0-fml*))))
|
|
|
|
p1-fml*))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|