blob: 4d5f031c1e4047c6f968130b7e99f70563ddd4fb (
plain) (
blame)
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
|
(define-library (csc match)
(export match)
(import (scheme base))
(begin
(define-syntax matches?
(syntax-rules (! _)
((matches? x '())
(null? x))
((matches? x _) #t)
((matches? x (! bind))
#t)
((matches? x (_))
(= 1 (length x)))
((matches? x ((! bind)))
(= 1 (length x)))
((matches? x (lit))
(and (= 1 (length x))
(eqv? lit (car x))))
((matches? x (_ pattern1 pattern2 ...))
(and (pair? x)
(matches? (cdr x) (pattern1 pattern2 ...))))
((matches? x ((! bind) pattern1 pattern2 ...))
(and (pair? x)
(matches? (cdr x) (pattern1 pattern2 ...))))
((matches? x (lit pattern1 pattern2 ...))
(and (pair? x)
(eqv? lit (car x))
(matches? (cdr x) (pattern1 pattern2 ...))))
((matches? x lit)
(eqv? lit x))))
(define-syntax bind-pattern
(syntax-rules (! _)
((bind-pattern x '() result1 result2 ...)
(begin result1 result2 ...))
((bind-pattern x _ result1 result2 ...)
(begin result1 result2 ...))
((bind-pattern x (! bind) result1 result2 ...)
(let ((bind x))
result1 result2 ...))
((bind-pattern x (_) result1 result2 ...)
(begin result1 result2 ...))
((bind-pattern x ((! bind)) result1 result2 ...)
(let ((bind (car x)))
result1 result2 ...))
((bind-pattern x (lit) result1 result2 ...)
(begin result1 result2 ...))
((bind-pattern x (_ pattern1 pattern2 ...) result1 result2 ...)
(bind-pattern (cdr x) (pattern1 pattern2 ...) result1 result2 ...))
((bind-pattern x ((! bind) pattern1 pattern2 ...) result1 result2 ...)
(let ((bind (car x)))
(bind-pattern (cdr x) (pattern1 pattern2 ...) result1 result2 ...)))
((bind-pattern x (lit pattern1 pattern2 ...) result1 result2 ...)
(bind-pattern (cdr x) (pattern1 pattern2 ...) result1 result2 ...))
((bind-pattern x lit result1 result2 ...)
(begin result1 result2 ...))))
(define-syntax match
(syntax-rules ()
((match x (pattern result1 result2 ...))
(if (matches? x pattern)
(bind-pattern x pattern result1 result2 ...)))
((match x (pattern result1 result2 ...) clause1 clause2 ...)
(if (matches? x pattern)
(bind-pattern x pattern result1 result2 ...)
(match x clause1 clause2 ...)))))))
|