-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpattern-match-lambda.sld
135 lines (124 loc) · 4.75 KB
/
pattern-match-lambda.sld
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
(define-library (pattern-match-lambda)
(export pattern-match-lambda)
(import (scheme base))
(begin
(define-syntax if-identifier
(syntax-rules ()
((_ condition seq alt)
(let-syntax ((foo (syntax-rules () ((_) seq))))
(let-syntax ((test (syntax-rules ()
((_ condition) (foo))
((_ foo) alt))))
(test foo))))))
(define-syntax if-literal
(syntax-rules ()
((_ p (literals ...) seq alt)
(let-syntax ((bar (syntax-rules () ((_) seq))))
(let-syntax ((foo (syntax-rules (literals ...)
((_ literals) (bar)) ...
((_ bar) alt))))
(foo p))))))
(define-syntax if-placeholder
(syntax-rules (_) ;; Literals cannot include underbar in R6RS.
((_ _ seq alt) seq)
((_ p seq alt) alt)))
(define-syntax %if-match-vector
(syntax-rules ()
((_ (literals ...) #() ind e seq alt) seq)
((_ (literals ...) #(p r ...) ind e seq alt)
(%if-match (literals ...) p (vector-ref e ind)
(let ((i ind))
(%if-match-vector (literals ...) #(r ...) (+ i 1) e seq alt))
alt))))
(define-syntax %if-match
(syntax-rules ()
((_ (literals ...) #(p ...) e seq alt)
(if (and (vector? e) (= (vector-length '#(p ...)) (vector-length e)))
(%if-match-vector (literals ...) #(p ...) 0 e seq alt)
(alt)))
((_ (literals ...) (p . r) e seq alt)
(let ((temp e))
(if (pair? temp)
(%if-match (literals ...) p (car temp)
(%if-match (literals ...) r (cdr temp) seq alt)
alt)
(alt))))
((_ (literals ...) () e seq alt)
(if (null? e) seq (alt)))
((_ (literals ...) p e seq alt)
(if-identifier p
(if-literal p (literals ...)
(if (equal? 'p e) seq (alt))
(if-placeholder p
seq
(let ((p e)) seq)))
(if (equal? p e) seq (alt))))))
(define-syntax %duplicate-check
(syntax-rules ()
((_) #f)
((_ p r ...)
(letrec-syntax
((bar (syntax-rules ()
((_) (syntax-error "duplicate pattern variable in pattern-match-lambda" p ))))
(foo (syntax-rules (r ...)
((_ r) (bar))
...
((_ x) (%duplicate-check r ...)))))
(foo p)))))
(define-syntax duplicate-check
(syntax-rules ()
((_ (pvar ...) (literals ...) #(p ...))
(duplicate-check (pvar ...) (literals ...) (p ...)))
((_ (pvar ...) (literals ...) ((p) . r))
(duplicate-check (pvar ...) (literals ...) (p . r)))
((_ (pvar ...) (literals ...) ((p0 . p1) . r))
(duplicate-check (pvar ...) (literals ...) (p0 p1 . r)))
((_ (pvar ...) (literals ...) (#(p ...) . r))
(duplicate-check (pvar ...) (literals ...) (p ... . r)))
((_ (pvar ...) (literals ...) (p . r))
(if-identifier p
(if-literal p (literals ...)
(duplicate-check (pvar ...) (literals ...) r)
(if-placeholder p
(duplicate-check (pvar ...) (literals ...) r)
(duplicate-check (pvar ... p) (literals ...) r)))
(duplicate-check (pvar ...) (literals ...) r)))
((_ (pvar ...) (literals ...) ())
(%duplicate-check pvar ...))
((_ (pvar ...) (literals ...) p)
(if-identifier p
(if-literal p (literals ...)
(duplicate-check (pvar ...) (literals ...) ())
(if-placeholder p
(duplicate-check (pvar ...) (literals ...) ())
(duplicate-check (pvar ... p) (literals ...) ())))
(duplicate-check (pvar ...) (literals ...) ())))))
(define-syntax if-match
(syntax-rules ()
((_ (literals ...) pattern lst seq alt)
(let ((alt-thunk (lambda() alt)))
(begin (duplicate-check () (literals ...) pattern)
(%if-match (literals ...) pattern lst seq alt-thunk))))))
(define-syntax %pattern-match-lambda
(syntax-rules (else)
((_ (literals ...) lst) (if #f #t))
((_ (literals ...) lst (else expr))
expr)
((_ (literals ...) lst (pattern expr) (rest-pattern rest-expr) ...)
(if-match (literals ...) pattern lst
expr
(%pattern-match-lambda (literals ...) lst
(rest-pattern rest-expr) ...)))
((_ (literals ...) lst (pattern fender expr) (rest-pattern rest-expr) ...)
(let ((next
(lambda() (%pattern-match-lambda (literals ...) lst
(rest-pattern rest-expr) ...))))
(if-match (literals ...) pattern lst
(if fender expr (next))
(next))))))
(define-syntax pattern-match-lambda
(syntax-rules ()
((_ (literals ...) clause ...)
(lambda lst
(%pattern-match-lambda (literals ...) lst clause ...)))))
))