2006-11-23 19:33:45 -05:00
|
|
|
|
|
|
|
(define-syntax record-case
|
|
|
|
(lambda (x)
|
|
|
|
(define (enumerate fld* i)
|
|
|
|
(syntax-case fld* ()
|
|
|
|
[() #'()]
|
|
|
|
[(x . x*)
|
2006-11-23 19:44:29 -05:00
|
|
|
(with-syntax ([i i] [i* (enumerate #'x* (fx+ i 1))])
|
2006-11-23 19:33:45 -05:00
|
|
|
#'(i . i*))]))
|
|
|
|
(define (generate-body ctxt cls*)
|
|
|
|
(syntax-case cls* (else)
|
2006-11-23 19:40:06 -05:00
|
|
|
[() (with-syntax ([x x]) #'(error #f "unmatched ~s in ~s" v #'x))]
|
2006-11-23 19:33:45 -05:00
|
|
|
[([else b b* ...]) #'(begin b b* ...)]
|
|
|
|
[([(rec-name rec-field* ...) b b* ...] . rest) (identifier? #'rec-name)
|
|
|
|
(with-syntax ([altern (generate-body ctxt #'rest)]
|
|
|
|
[(id* ...) (enumerate #'(rec-field* ...) 0)]
|
|
|
|
[rtd #'(type-descriptor rec-name)])
|
2006-11-23 19:44:29 -05:00
|
|
|
#'(if ($record/rtd? v rtd)
|
|
|
|
;((record-predicate rtd) v)
|
|
|
|
(let ([rec-field* ($record-ref v id*)] ...)
|
|
|
|
; ((record-field-accessor rtd id*) v)] ...)
|
2006-11-23 19:33:45 -05:00
|
|
|
b b* ...)
|
|
|
|
altern))]))
|
|
|
|
(syntax-case x ()
|
|
|
|
[(_ expr cls* ...)
|
|
|
|
(with-syntax ([body (generate-body #'_ #'(cls* ...))])
|
|
|
|
#'(let ([v expr]) body))])))
|