-
Notifications
You must be signed in to change notification settings - Fork 2
/
spam.lisp
213 lines (191 loc) · 6.68 KB
/
spam.lisp
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
;;;; SPAM: the Simple Pattern Matcher
;;;
#+org.tfeb.tools.require-module
(org.tfeb.tools.require-module:needs
(:org.tfeb.hax.simple-loops :compile t)
(:org.tfeb.hax.utilities :compile t))
(defpackage :org.tfeb.hax.spam
(:use :cl :org.tfeb.hax.simple-loops :org.tfeb.hax.utilities)
(:export
#:head-matches
#:list-matches
#:list*-matches
#:cons-matches
#:list-of
#:repeating-list-of
#:is
#:is-type
#:any
#:var
#:lambda-list-keyword
#:some-of
#:all-of
#:none-of
#:matchp
#:matching))
(in-package :org.tfeb.hax.spam)
(provide :org.tfeb.hax.spam)
;;; Predicate constructors
;;;
;;; This is about a third of a pattern matching thing all on its own
;;;
(defun head-matches (&rest predicates)
;; Return a predicate which matches a lambda list if the leading
;; section of it matches PREDICATES. A compiler macro for this
;; would be fun to write.
(declare (dynamic-extent predicates))
(let ((effective-predicates (mapcar (lambda (p)
(etypecase p
(function p)
(symbol (symbol-function p))))
predicates)))
(lambda (ll)
(looping ((lt ll) (pt effective-predicates))
(cond
((null pt) (return t))
((not (consp lt)) (return nil))
((funcall (first pt) (first lt))
(values (rest lt) (rest pt)))
(t (return nil)))))))
(defun list-matches (&rest predicates)
;; Return a predicate which matches the whole of a list, which must
;; be a list.
(declare (dynamic-extent predicates))
(let ((effective-predicates (mapcar (lambda (p)
(etypecase p
(function p)
(symbol (symbol-function p))))
predicates)))
(lambda (ll)
(looping ((lt ll) (pt effective-predicates))
(cond
((null pt) (return (null lt)))
((not (consp lt)) (return nil))
((funcall (first pt) (first lt))
(values (rest lt) (rest pt)))
(t (return nil)))))))
(defun list*-matches (&rest predicates)
(declare (dynamic-extent predicates))
(when (null predicates)
(error "need at least one predicate for the tail"))
(let ((effective-predicates (mapcar (lambda (p)
(etypecase p
(function p)
(symbol (symbol-function p))))
predicates)))
(lambda (ll)
(looping ((lt ll) (pt effective-predicates))
(cond
((null (rest pt)) (return (funcall (first pt) lt)))
((not (consp lt)) (return nil))
((funcall (first pt) (first lt))
(values (rest lt) (rest pt)))
(t (return nil)))))))
(defun cons-matches (car cdr)
;; Return a predicate which matches a cons where the car and cdr
;; match CAR & CDR.
(lambda (c)
(and (consp c)
(funcall car (car c))
(funcall cdr (cdr c)))))
(defun list-of (predicate)
(let ((effective-predicate (etypecase predicate
(function predicate)
(symbol (symbol-function predicate)))))
(lambda (ll)
(looping ((llt ll))
(cond
((null llt) (return t))
((not (consp llt)) (return nil))
((funcall effective-predicate (first llt))
(rest llt))
(t (return nil)))))))
(defun repeating-list-of (&rest predicates)
;; (repeating-list-of #'keywordp (any)), for instance
(declare (dynamic-extent predicates))
(when (null predicates)
(error "need at least one predicate to repeat"))
(let ((effective-predicates (mapcar (lambda (p)
(etypecase p
(function p)
(symbol (symbol-function p))))
predicates)))
(lambda (ll)
(looping ((llt ll)
(ept '()))
(if (null ept)
(if (null llt)
(return t)
(values llt effective-predicates))
(cond
((not (consp llt)) (return nil))
((funcall (first ept) (first llt))
(values (rest llt) (rest ept)))
(t (return nil))))))))
(defun is (x)
;; return a predicate which matches EQL
(lambda (v)
(eql v x)))
(defun is-type (type)
(lambda (v)
(typep v type)))
(defun any ()
;; return a predicate which always succeeds
(lambda (v)
(declare (ignore v))
t))
(defun var ()
(lambda (v)
(and (symbolp v)
(not (constantp v)) ;includes NIL
(not (or (keywordp v)
(member v lambda-list-keywords))))))
(defun lambda-list-keyword ()
(lambda (v)
(member v lambda-list-keywords)))
(defun some-of (&rest predicates)
(let ((effective-predicates (mapcar (lambda (p)
(etypecase p
(function p)
(symbol (symbol-function p))))
predicates)))
(lambda (e)
(some (lambda (p)
(funcall p e)) effective-predicates))))
(defun all-of (&rest predicates)
(let ((effective-predicates (mapcar (lambda (p)
(etypecase p
(function p)
(symbol (symbol-function p))))
predicates)))
(lambda (e)
(every (lambda (p)
(funcall p e))
effective-predicates))))
(defun none-of (&rest predicates)
(let ((effective-predicates (mapcar (lambda (p)
(etypecase p
(function p)
(symbol (symbol-function p))))
predicates)))
(lambda (e)
(notany (lambda (p)
(funcall p e))
effective-predicates))))
;;; Matching
;;;
(defun matchp (thing predicate)
(funcall predicate thing))
(defmacro matching (form &body clauses)
(with-names (<var>)
`(let ((,<var> ,form))
(cond
,@(mapcar (lambda (clause)
(destructuring-bind (matcher &body forms) clause
(case matcher
((otherwise t)
`(t ,@forms))
(otherwise
`((funcall ,matcher ,<var>)
,@forms)))))
clauses)))))