-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecords.ss
More file actions
325 lines (309 loc) · 12.2 KB
/
records.ss
File metadata and controls
325 lines (309 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
(define source-rtd
(let-values ([(src start?) (#%$syntax->src #'*)])
(record-rtd src)))
(define source-sfd (csv7:record-field-accessor source-rtd 'sfd))
(define source-bfp (csv7:record-field-accessor source-rtd 'bfp))
(define source-file-descriptor-rtd
(let-values ([(src start?) (#%$syntax->src #'*)])
(record-rtd (source-sfd src))))
(define source-file-descriptor-name
(csv7:record-field-accessor source-file-descriptor-rtd 'name))
(define (find-source x)
(let-values ([(src start?) (#%$syntax->src x)])
(if src
(datum->syntax #'quote
(format " ~a offset ~a of ~a"
(if (eq? start? 'near) 'near 'at)
(if start? (source-bfp src) (source-efp src))
(source-file-descriptor-name (source-sfd src))))
#'"")))
(define (snull? x) (syntax-case x () [() #t] [_ #f]))
(define (scar x) (syntax-case x () [(x . _) #'x]))
(define (scdr x) (syntax-case x () [(_ . y) #'y]))
(define (syntax-datum-eq? x y)
(eq? (syntax->datum x) (syntax->datum y)))
(define (bad-syntax msg form subform)
(raise
(condition
(make-message-condition msg)
(make-syntax-violation form subform))))
(module ((match
bad-match
fail-false
fail-match
match-help
match-one
match-pattern
match-record
match-vector
)
match-let*)
(define-syntax (match x)
(syntax-case x ()
[(_ exp (pattern b1 b2 ...) ...)
#`(let ([v exp])
((or (match-pattern v pattern b1 b2 ...) ...
(bad-match v #,(find-source x)))))]))
(define-syntax match-pattern
(syntax-rules ()
[(_ e pat (guard g) b1 b2 ...)
(eq? (datum guard) 'guard)
(match-one e pat fail-false (and g (lambda () b1 b2 ...)))]
[(_ e pat b1 b2 ...)
(match-one e pat fail-false (lambda () b1 b2 ...))]))
(define-syntax (match-let* x)
(syntax-case x ()
[(_ () b1 b2 ...)
#'(let () b1 b2 ...)]
[(_ ([pattern exp] . rest) b1 b2 ...)
#'(match-one exp pattern fail-match (match-let* rest b1 b2 ...))]
[(_ ([pattern (guard g) exp] . rest) b1 b2 ...)
(eq? (datum guard) 'guard)
#`(let ([v exp])
(match-one v pattern fail-match
(if g
(match-let* rest b1 b2 ...)
(bad-match v #,(find-source #'g)))))]))
(define-syntax fail-false
(syntax-rules ()
[(_ v pattern) #f]))
(define-syntax (fail-match x)
(syntax-case x ()
[(_ v pattern)
#`(bad-match v #,(find-source #'pattern))]))
(define (bad-match v src)
(errorf #f "no matching clause for ~s~a" v src))
(define-syntax (match-one x)
(define (bad-pattern x)
(syntax-error x "invalid match pattern"))
(define (add-identifier id ids)
(if (duplicate-id? id ids)
(syntax-error id "duplicate pattern variable")
(cons id ids)))
(define (duplicate-id? id ids)
(and (not (null? ids))
(or (bound-identifier=? (car ids) id)
(duplicate-id? id (cdr ids)))))
(syntax-case x ()
[(_ e pattern fail body)
(lambda (lookup)
(let check ([ids '()] [x #'pattern])
(syntax-case x (unquote unquote-splicing quasiquote)
[(unquote (v <= pattern))
(and (identifier? #'v) (eq? (datum <=) '<=))
(check (add-identifier #'v ids) #'pattern)]
[(unquote v)
(let ([s (datum v)])
(cond
[(eq? s '_) ids]
[(symbol? s) (add-identifier #'v ids)]
[else (bad-pattern x)]))]
[(unquote-splicing var)
(if (identifier? #'var)
ids
(bad-pattern x))]
[(quasiquote (record spec ...))
(identifier? #'record)
(let ([fields (lookup #'record #'fields)])
(unless fields
(syntax-error x "unknown record type in pattern"))
(let check-specs ([ids ids] [specs #'(spec ...)])
(syntax-case specs (unquote)
[() ids]
[((unquote field) . rest)
(identifier? #'field)
(if (memq (datum field) fields)
(check-specs (add-identifier #'field ids) #'rest)
(syntax-error x
(format "unknown field ~a in pattern"
(datum field))))]
[([field pattern] . rest)
(identifier? #'field)
(if (memq (datum field) fields)
(check-specs (check ids #'pattern) #'rest)
(syntax-error x
(format "unknown field ~a in pattern"
(datum field))))]
[_ (bad-pattern x)])))]
[(quasiquote _) (bad-pattern x)]
[lit
(let ([x (datum lit)])
(or (symbol? x) (number? x) (boolean? x) (char? x)
(string? x) (bytevector? x) (null? x)))
ids]
[(first . rest) (check (check ids #'first) #'rest)]
[#(element ...) (fold-left check ids #'(element ...))]
[_ (bad-pattern x)]))
#'(match-help e pattern fail body))]))
(define-syntax (match-help x)
(define (get-pattern x)
(syntax-case x ()
[(_ e pattern fail body) #'pattern]))
(syntax-case x (unquote unquote-splicing quasiquote)
[(_ e (unquote (v <= pattern)) fail body)
#'(let ([v e]) (match-help v pattern fail body))]
[(_ e (unquote v) fail body)
(if (eq? (datum v) '_)
#'(begin e body)
#'(let ([v e]) body))]
[(_ e (unquote-splicing var) fail body)
#`(let ([v e])
(if (equal? v var)
body
(fail v #,(get-pattern x))))]
[(_ e (quasiquote (record spec ...)) fail body)
#`(let ([v e])
(if (record is? v)
(match-record v (record spec ...) fail body)
(fail v #,(get-pattern x))))]
[(_ e lit fail body)
(let ([x (datum lit)])
(or (symbol? x) (number? x) (boolean? x) (char? x)))
#`(let ([v e])
(if (eqv? v 'lit)
body
(fail v #,(get-pattern x))))]
[(_ e s fail body)
(string? (datum s))
#`(let ([v e])
(if (and (string? v) (#3%string=? v s))
body
(fail v #,(get-pattern x))))]
[(_ e bv fail body)
(bytevector? (datum bv))
#`(let ([v e])
(if (and (bytevector? v) (#3%bytevector=? v bv))
body
(fail v #,(get-pattern x))))]
[(_ e () fail body)
#`(let ([v e])
(if (null? v)
body
(fail v #,(get-pattern x))))]
[(_ e (first . rest) fail body)
#`(let ([v e])
(if (pair? v)
(match-help (#3%car v) first fail
(match-help (#3%cdr v) rest fail body))
(fail v #,(get-pattern x))))]
[(_ e #(element ...) fail body)
#`(let ([v e])
(if (and (vector? v)
(#3%fx= (#3%vector-length v) (length '(element ...))))
(match-vector v 0 (element ...) fail body)
(fail v #,(get-pattern x))))]))
(define-syntax match-record
(syntax-rules (unquote)
[(_ v (record) fail body) body]
[(_ v (record (unquote field) . rest) fail body)
(let ([field (record no-check field v)])
(match-record v (record . rest) fail body))]
[(_ v (record [field pattern] . rest) fail body)
(match-help (record no-check field v) pattern fail
(match-record v (record . rest) fail body))]))
(define-syntax match-vector
(syntax-rules ()
[(_ v i () fail body) body]
[(_ v i (pattern . rest) fail body)
(match-help (#3%vector-ref v i) pattern fail
(match-vector v (fx+ i 1) rest fail body))]))
)
(define-syntax (define-record x)
(syntax-case x ()
[(_ name field ...)
(and (identifier? #'name)
(let valid-fields? ([fields #'(field ...)] [seen '()])
(syntax-case fields ()
[(fn . rest)
(and (identifier? #'fn)
(let ([f (datum fn)])
(when (memq f seen)
(bad-syntax "duplicate field" x #'fn))
(valid-fields? #'rest (cons f seen))))]
[() #t]
[_ #f])))
#'(begin
(define-syntax (name x)
(define (valid-bindings? bindings seen)
(syntax-case bindings ()
[((fn fv) . rest)
(and (identifier? #'fn)
(let ([f (datum fn)])
(when (memq f seen)
(bad-syntax "duplicate field" x #'fn))
(unless (memq f '(field ...))
(bad-syntax "unknown field" x #'fn))
(valid-bindings? #'rest (cons f seen))))]
[() #t]
[_ #f]))
(define (make-record fields bindings)
(if (snull? fields)
'()
(let* ([f (scar fields)]
[v (find-binding f bindings)])
(unless v
(syntax-error x
(format "missing field ~a in" (syntax->datum f))))
(cons v
(make-record (scdr fields) (remove-binding f bindings))))))
(define (copy-record fields index bindings)
(if (snull? fields)
'()
(let* ([f (scar fields)]
[v (find-binding f bindings)])
(if v
(cons v (copy-record (scdr fields) (+ index 1)
(remove-binding f bindings)))
(cons #`(#3%vector-ref src #,(datum->syntax f index))
(copy-record (scdr fields) (+ index 1) bindings))))))
(define (find-binding f bindings)
(syntax-case bindings ()
[((fn fv) . rest)
(if (syntax-datum-eq? #'fn f)
#'fv
(find-binding f #'rest))]
[() #f]))
(define (remove-binding f bindings)
(syntax-case bindings ()
[((fn fv) . rest)
(if (syntax-datum-eq? #'fn f)
#'rest
#`((fn fv) #,@(remove-binding f #'rest)))]))
(define (find-index fn fields index)
(let ([f (scar fields)])
(if (syntax-datum-eq? f fn)
index
(find-index fn (scdr fields) (+ index 1)))))
(syntax-case x ()
[(name make . bindings)
(and (eq? (datum make) 'make)
(valid-bindings? #'bindings '()))
#`(vector 'name #,@(make-record #'(field ...) #'bindings))]
[(name copy e . bindings)
(and (eq? (datum copy) 'copy)
(valid-bindings? #'bindings '()))
#`(let ([src e])
(unless (name is? src)
(errorf 'record-copy "~s is not a ~a~a" src 'name
#,(find-source x)))
(vector 'name #,@(copy-record #'(field ...) 1 #'bindings)))]
[(name is? e)
(eq? (datum is?) 'is?)
#'(let ([x e])
(and (vector? x)
(#3%fx= (#3%vector-length x) (length '(name field ...)))
(eq? (#3%vector-ref x 0) 'name)))]
[(name fn e)
(syntax-datum-eq? #'fn #'field)
#`(let ([x e])
(unless (name is? x)
(errorf 'record-ref "~s is not a ~a~a" x 'name
#,(find-source x)))
(#3%vector-ref x #,(find-index #'fn #'(field ...) 1)))]
...
[(name no-check fn e)
(and (eq? (datum no-check) 'no-check)
(syntax-datum-eq? #'fn #'field))
#`(#3%vector-ref e #,(find-index #'fn #'(field ...) 1))]
...))
(define-property name fields '(field ...)))]))