-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtch.ss
76 lines (68 loc) · 2.14 KB
/
mtch.ss
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
(define-for-syntax mtch-show-expansion #f)
(define-for-syntax (shew . args) (map pretty-print args))
(define-for-syntax (mtch-literal? o)
(or ;(is-quote? o)
(string? o)
(number? o)
(null? o)
(eq? #t o)
(eq? #f o)))
(define-for-syntax (mtch-is-quote? o)
(and (pair? o)
(eq? (car o) 'quote)
(pair? (cdr o))
(null? (cddr o))))
(define-for-syntax mtch-vargen
(let ((serial 0))
(lambda ()
(let ((s serial))
(set! serial (+ serial 1))
(string->symbol (string-append "_m" (number->string s)))))))
(define-for-syntax (mtch-clause target pat body)
(let ((carvar (mtch-vargen))
(cdrvar (mtch-vargen)))
(cond
((mtch-is-quote? pat)
`(if (equal? ',(cadr pat) ,target)
,body
(fail))) ((pair? pat)
`(if (pair? ,target)
(let ((,carvar (car ,target))
(,cdrvar (cdr ,target)))
,(mtch-clause carvar
(car pat)
(mtch-clause cdrvar
(cdr pat)
body)))
(fail)))
((eq? '_ pat) body)
((symbol? pat)
`(let ((,pat ,target)) ,body))
((mtch-literal? pat)
`(if (equal? ',pat ,target)
,body
(fail)))
(#t (err 'mtch-clause target pat body)))))
(define-for-syntax (mtch-clauses target clauses all-clauses)
(if (null? clauses)
`(err 'match-failure target ',all-clauses)
(let ((pat (car clauses))
(body (cadr clauses))
(rest (cddr clauses)))
(let ((next (mtch-clauses target rest all-clauses)))
`(let ((fail (lambda () ,next)))
,(mtch-clause target pat body))))))
(define-for-syntax (mtch-render target clauses)
(let ((code
`(let ((target ,target))
,(mtch-clauses 'target clauses clauses))))
(if mtch-show-expansion
(begin
(pretty-print 'mtch)
(pretty-print target)
(pretty-print clauses)
(pretty-print code))
'())
code))
(define-macro (mtch target . clauses)
(mtch-render target clauses))