| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
(library (ikarus structs) |
| 19 |
(export |
| 20 |
make-struct-type struct-type-name struct-type-symbol |
| 21 |
struct-type-field-names struct-constructor struct-predicate |
| 22 |
struct-field-accessor struct-field-mutator struct? struct-rtd |
| 23 |
set-rtd-printer! |
| 24 |
(rename (struct-rtd struct-type-descriptor)) |
| 25 |
struct-name struct-printer struct-length struct-ref struct-set!) |
| 26 |
|
| 27 |
(import |
| 28 |
(ikarus system $structs) |
| 29 |
(ikarus system $pairs) |
| 30 |
(ikarus system $fx) |
| 31 |
(except (ikarus) |
| 32 |
make-struct-type struct-type-name struct-type-symbol |
| 33 |
struct-type-field-names struct-constructor struct-predicate |
| 34 |
struct-field-accessor struct-field-mutator struct? struct-rtd |
| 35 |
struct-type-descriptor struct-name struct-printer struct-length |
| 36 |
struct-ref struct-set! set-rtd-printer!)) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
(define rtd? |
| 41 |
(lambda (x) |
| 42 |
(and ($struct? x) |
| 43 |
(eq? ($struct-rtd x) (base-rtd))))) |
| 44 |
|
| 45 |
(define rtd-name |
| 46 |
(lambda (rtd) |
| 47 |
($struct-ref rtd 0))) |
| 48 |
|
| 49 |
(define rtd-length |
| 50 |
(lambda (rtd) |
| 51 |
($struct-ref rtd 1))) |
| 52 |
|
| 53 |
(define rtd-fields |
| 54 |
(lambda (rtd) |
| 55 |
($struct-ref rtd 2))) |
| 56 |
|
| 57 |
(define rtd-printer |
| 58 |
(lambda (rtd) |
| 59 |
($struct-ref rtd 3))) |
| 60 |
|
| 61 |
(define rtd-symbol |
| 62 |
(lambda (rtd) |
| 63 |
($struct-ref rtd 4))) |
| 64 |
|
| 65 |
(define set-rtd-name! |
| 66 |
(lambda (rtd name) |
| 67 |
($struct-set! rtd 0 name))) |
| 68 |
|
| 69 |
(define set-rtd-length! |
| 70 |
(lambda (rtd n) |
| 71 |
($struct-set! rtd 1 n))) |
| 72 |
|
| 73 |
(define set-rtd-fields! |
| 74 |
(lambda (rtd fields) |
| 75 |
($struct-set! rtd 2 fields))) |
| 76 |
|
| 77 |
(define $set-rtd-printer! |
| 78 |
(lambda (rtd printer) |
| 79 |
($struct-set! rtd 3 printer))) |
| 80 |
|
| 81 |
(define set-rtd-symbol! |
| 82 |
(lambda (rtd symbol) |
| 83 |
($struct-set! rtd 4 symbol))) |
| 84 |
|
| 85 |
(define make-rtd |
| 86 |
(lambda (name fields printer symbol) |
| 87 |
($struct (base-rtd) name (length fields) fields printer symbol))) |
| 88 |
|
| 89 |
(define verify-field |
| 90 |
(lambda (x) |
| 91 |
(unless (symbol? x) |
| 92 |
(die 'make-struct-type "not a valid field name" x)))) |
| 93 |
|
| 94 |
(define set-fields |
| 95 |
(lambda (r f* i n) |
| 96 |
(cond |
| 97 |
[(null? f*) |
| 98 |
(if ($fx= i n) |
| 99 |
r |
| 100 |
#f)] |
| 101 |
[($fx< i n) |
| 102 |
(if (null? f*) |
| 103 |
#f |
| 104 |
(begin |
| 105 |
($struct-set! r i ($car f*)) |
| 106 |
(set-fields r ($cdr f*) ($fxadd1 i) n)))] |
| 107 |
[else #f]))) |
| 108 |
|
| 109 |
(define make-struct-type |
| 110 |
(case-lambda |
| 111 |
[(name fields) |
| 112 |
(unless (string? name) |
| 113 |
(die 'make-struct-type "name must be a string" name)) |
| 114 |
(unless (list? fields) |
| 115 |
(die 'make-struct-type "fields must be a list" fields)) |
| 116 |
(for-each verify-field fields) |
| 117 |
(let ([g (gensym name)]) |
| 118 |
(let ([rtd (make-rtd name fields #f g)]) |
| 119 |
(set-symbol-value! g rtd) |
| 120 |
rtd))] |
| 121 |
[(name fields g) |
| 122 |
(unless (string? name) |
| 123 |
(die 'make-struct-type "name must be a string" name)) |
| 124 |
(unless (list? fields) |
| 125 |
(die 'make-struct-type "fields must be a list" fields)) |
| 126 |
(for-each verify-field fields) |
| 127 |
(cond |
| 128 |
[(symbol-bound? g) |
| 129 |
(let ([rtd (symbol-value g)]) |
| 130 |
(unless (and (string=? name (struct-type-name rtd)) |
| 131 |
(equal? fields (struct-type-field-names rtd))) |
| 132 |
(die 'make-struct-type "definition mismatch")) |
| 133 |
rtd)] |
| 134 |
[else |
| 135 |
(let ([rtd (make-rtd name fields #f g)]) |
| 136 |
(set-symbol-value! g rtd) |
| 137 |
rtd)])])) |
| 138 |
|
| 139 |
(define struct-type-name |
| 140 |
(lambda (rtd) |
| 141 |
(unless (rtd? rtd) |
| 142 |
(die 'struct-type-name "not an rtd" rtd)) |
| 143 |
(rtd-name rtd))) |
| 144 |
|
| 145 |
(define struct-type-symbol |
| 146 |
(lambda (rtd) |
| 147 |
(unless (rtd? rtd) |
| 148 |
(die 'struct-type-symbol "not an rtd" rtd)) |
| 149 |
(rtd-symbol rtd))) |
| 150 |
|
| 151 |
(define struct-type-field-names |
| 152 |
(lambda (rtd) |
| 153 |
(unless (rtd? rtd) |
| 154 |
(die 'struct-type-field-names "not an rtd" rtd)) |
| 155 |
(rtd-fields rtd))) |
| 156 |
|
| 157 |
|
| 158 |
(define struct-constructor |
| 159 |
(lambda (rtd) |
| 160 |
(unless (rtd? rtd) |
| 161 |
(die 'struct-constructor "not an rtd")) |
| 162 |
(lambda args |
| 163 |
(let ([n (rtd-length rtd)]) |
| 164 |
(let ([r ($make-struct rtd n)]) |
| 165 |
(or (set-fields r args 0 n) |
| 166 |
(die 'struct-constructor |
| 167 |
"incorrect number of arguments to the constructor" |
| 168 |
rtd))))))) |
| 169 |
|
| 170 |
(define struct-predicate |
| 171 |
(lambda (rtd) |
| 172 |
(unless (rtd? rtd) |
| 173 |
(die 'struct-predicate "not an rtd")) |
| 174 |
(lambda (x) |
| 175 |
(and ($struct? x) |
| 176 |
(eq? ($struct-rtd x) rtd))))) |
| 177 |
|
| 178 |
(define field-index |
| 179 |
(lambda (i rtd who) |
| 180 |
(cond |
| 181 |
[(fixnum? i) |
| 182 |
(unless (and ($fx>= i 0) ($fx< i (rtd-length rtd))) |
| 183 |
(die who "out of range for rtd" i rtd)) |
| 184 |
i] |
| 185 |
[(symbol? i) |
| 186 |
(letrec ([lookup |
| 187 |
(lambda (n ls) |
| 188 |
(cond |
| 189 |
[(null? ls) |
| 190 |
(die who "not a field" rtd)] |
| 191 |
[(eq? i ($car ls)) n] |
| 192 |
[else (lookup ($fx+ n 1) ($cdr ls))]))]) |
| 193 |
(lookup 0 (rtd-fields rtd)))] |
| 194 |
[else (die who "not a valid index" i)]))) |
| 195 |
|
| 196 |
(define struct-field-accessor |
| 197 |
(lambda (rtd i) |
| 198 |
(unless (rtd? rtd) |
| 199 |
(die 'struct-field-accessor "not an rtd" rtd)) |
| 200 |
(let ([i (field-index i rtd 'struct-field-accessor)]) |
| 201 |
(lambda (x) |
| 202 |
(unless (and ($struct? x) |
| 203 |
(eq? ($struct-rtd x) rtd)) |
| 204 |
(die 'struct-field-accessor "not of correct type" x rtd)) |
| 205 |
($struct-ref x i))))) |
| 206 |
|
| 207 |
(define struct-field-mutator |
| 208 |
(lambda (rtd i) |
| 209 |
(unless (rtd? rtd) |
| 210 |
(die 'struct-field-mutator "not an rtd" rtd)) |
| 211 |
(let ([i (field-index i rtd 'struct-field-mutator)]) |
| 212 |
(lambda (x v) |
| 213 |
(unless (and ($struct? x) |
| 214 |
(eq? ($struct-rtd x) rtd)) |
| 215 |
(die 'struct-field-mutator "not of correct type" x rtd)) |
| 216 |
($struct-set! x i v))))) |
| 217 |
|
| 218 |
(define struct? |
| 219 |
(lambda (x . rest) |
| 220 |
(if (null? rest) |
| 221 |
($struct? x) |
| 222 |
(let ([rtd ($car rest)]) |
| 223 |
(unless (null? ($cdr rest)) |
| 224 |
(die 'struct? "too many arguments")) |
| 225 |
(unless (rtd? rtd) |
| 226 |
(die 'struct? "not an rtd")) |
| 227 |
(and ($struct? x) |
| 228 |
(eq? ($struct-rtd x) rtd)))))) |
| 229 |
|
| 230 |
(define struct-rtd |
| 231 |
(lambda (x) |
| 232 |
(if ($struct? x) |
| 233 |
($struct-rtd x) |
| 234 |
(die 'struct-rtd "not a struct" x)))) |
| 235 |
|
| 236 |
(define struct-length |
| 237 |
(lambda (x) |
| 238 |
(if ($struct? x) |
| 239 |
(rtd-length ($struct-rtd x)) |
| 240 |
(die 'struct-length "not a struct" x)))) |
| 241 |
|
| 242 |
(define struct-name |
| 243 |
(lambda (x) |
| 244 |
(if ($struct? x) |
| 245 |
(rtd-name ($struct-rtd x)) |
| 246 |
(die 'struct-name "not a struct" x)))) |
| 247 |
|
| 248 |
(define struct-printer |
| 249 |
(lambda (x) |
| 250 |
(if ($struct? x) |
| 251 |
(rtd-printer ($struct-rtd x)) |
| 252 |
(die 'struct-printer "not a struct" x)))) |
| 253 |
|
| 254 |
(define struct-ref |
| 255 |
(lambda (x i) |
| 256 |
(unless ($struct? x) (die 'struct-ref "not a struct" x)) |
| 257 |
(unless (fixnum? i) (die 'struct-ref "not a valid index" i)) |
| 258 |
(let ([n (rtd-length ($struct-rtd x))]) |
| 259 |
(unless (and ($fx>= i 0) ($fx< i n)) |
| 260 |
(die 'struct-ref "index is out of range" i x)) |
| 261 |
($struct-ref x i)))) |
| 262 |
|
| 263 |
(define struct-set! |
| 264 |
(lambda (x i v) |
| 265 |
(unless ($struct? x) (die 'struct-set! "not a struct" x)) |
| 266 |
(unless (fixnum? i) (die 'struct-set! "not a valid index" i)) |
| 267 |
(let ([n (rtd-length ($struct-rtd x))]) |
| 268 |
(unless (and ($fx>= i 0) ($fx< i n)) |
| 269 |
(die 'struct-set! "index is out of range" i x)) |
| 270 |
($struct-set! x i v)))) |
| 271 |
|
| 272 |
(define (set-rtd-printer! x p) |
| 273 |
(unless (rtd? x) |
| 274 |
(die 'set-rtd-printer! "not an rtd" x)) |
| 275 |
(unless (procedure? p) |
| 276 |
(die 'set-rtd-printer! "not a procedure" p)) |
| 277 |
($set-rtd-printer! x p)) |
| 278 |
|
| 279 |
(set-rtd-fields! (base-rtd) '(name fields length printer symbol)) |
| 280 |
(set-rtd-name! (base-rtd) "base-rtd") |
| 281 |
($set-rtd-printer! (base-rtd) |
| 282 |
(lambda (x p wr) |
| 283 |
(unless (rtd? x) |
| 284 |
(die 'struct-type-printer "not an rtd")) |
| 285 |
(display "#<" p) |
| 286 |
(display (rtd-name x) p) |
| 287 |
(display " rtd>" p))) |
| 288 |
) |
| 289 |
|
| 290 |
|
| 291 |
(library (ikarus systems structs) |
| 292 |
(export $struct-ref $struct/rtd?) |
| 293 |
(import (ikarus)) |
| 294 |
(define $struct-ref struct-ref) |
| 295 |
(define ($struct/rtd? x rtd) |
| 296 |
(import (ikarus system $structs)) |
| 297 |
($struct/rtd? x rtd))) |
| 298 |
|