-
Notifications
You must be signed in to change notification settings - Fork 2
/
rules.lisp
252 lines (211 loc) · 7.65 KB
/
rules.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
(in-package :rules)
(defvar *NOW* nil)
(defun now ()
(or *NOW* (get-universal-time)))
(defmacro aif (test &body body)
`(let ((it ,test))
(if it ,@body)))
(defun weekday (&optional n)
(nth (or n (nth-value 6 (decode-universal-time (now))))
'(monday tuesday wednesday thursday friday
saturday sunday)))
(defun time-of-day ()
(multiple-value-bind
(s m h)
(decode-universal-time (now))
(+ (* h 3600)
(* m 60)
s)))
(defun hhmm-seconds (hhmm meridian)
(+ (* 60 (+ (mod hhmm 100)
(* 60 (floor (/ hhmm 100)))))
(cond ((eq meridian 'am) 0)
((eq meridian 'pm) 43200)
(t (error "invalid meridian ~A" meridian)))))
(defun assocify (lst)
(if lst
(acons (car lst)
(cadr lst)
(assocify (cddr lst)))))
(defvar *environment* '())
(defvar *metadata* '())
(defvar *parameters* '())
(defvar *plugin-handlers* '())
(defun register-plugin (plugin fn)
(setf *plugin-handlers*
(acons (sym->key plugin) fn *plugin-handlers*)))
(defun registered-plugin? (plugin)
(not (null (assoc (sym->key plugin) *plugin-handlers*))))
(defun dispatch-to-plugin (plugin args)
(let ((fn (cdr (assoc (sym->key plugin) *plugin-handlers*))))
(if fn
(funcall fn args)
(error "no such plugin ~A" plugin))))
(defun param (name &optional default)
(aif (assoc name *parameters*)
(cdr it)
default))
(defun replace-all (subj old new)
(with-output-to-string (out)
(loop with part-length = (length old)
for old-pos = 0 then (+ pos part-length)
for pos = (search old subj :start2 old-pos)
do (write-string subj out
:start old-pos
:end (or pos (length subj)))
when pos do (write-string new out)
while pos)))
(defun interpolate (s)
(loop for n in '(:topic :status :message :link) do
(setf s (replace-all s (format nil "$~(~A~)" (symbol-name n)) (param n))))
(loop for pair in *metadata* do
(setf s (replace-all s (format nil "$[~(~A~)]" (symbol-name (car pair))) (cdr pair))))
s)
; pre-declaration -- will be re-defined shortly.
(eval-when (:execute)
(defun -eval/args (args)
(declare (ignore args))))
(defun sym->key (sym)
(find-symbol (symbol-name sym)
(find-package "KEYWORD")))
(defun -eval/expr (expr)
(cond ((atom expr)
(cond ((symbolp expr)
(param (sym->key expr)))
((stringp expr)
(interpolate expr))
(t expr)))
((eq (car expr) 'not)
(not (-eval/expr (cadr expr))))
((eq (car expr) 'and)
(loop for sub in (cdr expr) do
(if (not (-eval/expr sub))
(return-from -eval/expr nil)))
t)
((eq (car expr) 'or)
(loop for sub in (cdr expr) do
(if (-eval/expr sub)
(return-from -eval/expr t)))
nil)
((eq (car expr) 'map)
(assocify (cdr expr)))
((eq (car expr) 'concat)
(format nil "~{~a~%~}"
(remove-if #'null (-eval/args (cdr expr)))))
((eq (car expr) 'value)
(cdr (assoc (cadr expr) *environment*)))
((eq (car expr) 'lookup)
(let ((map (cdr (assoc (cadr expr) *environment*))))
(loop for var in (cddr expr) do
(aif (assoc var map :test #'equal)
(return-from -eval/expr (cdr it))))
nil))
((eq (car expr) 'metadata?)
(not (null (assoc (sym->key (cadr expr)) *metadata*))))
((eq (car expr) 'metadata)
(let ((pair (assoc (sym->key (cadr expr)) *metadata*)))
(if pair (cdr pair) "")))
((eq (car expr) 'if)
(cond ((-eval/expr (cadr expr))
(-eval/expr (caddr expr)))
((cadddr expr)
(-eval/expr (cadddr expr)))
(t nil)))
((eq (car expr) 'matches)
;; FIXME : implement cl-ppcre and regex
nil)
((eq (car expr) 'is)
(equal (param :topic)
(-eval/expr (cadr expr))))
((eq (car expr) 'on)
(cond ((equal (cdr expr) '(weekdays))
(find (weekday) '(monday
tuesday
wednesday
thursday
friday)))
((equal (cdr expr) '(weekends))
(find (weekday) '(sunday
saturday)))
(t (find (weekday) (cdr expr)))))
((eq (car expr) 'from)
(let ((start (nthcdr 1 expr))
(stop (nthcdr 4 expr)))
(and (>= (time-of-day)
(hhmm-seconds (car start) (cadr start)))
(<= (time-of-day)
(hhmm-seconds (car stop) (cadr stop))))))
((eq (car expr) 'after)
(> (time-of-day)
(hhmm-seconds (cadr expr) (caddr expr))))
((eq (car expr) 'before)
(< (time-of-day)
(hhmm-seconds (cadr expr) (caddr expr))))
(t (error "expression syntax error ~A" (car expr)))))
(defun -eval/args (args)
(loop for arg in args collect
(if (keywordp arg)
arg
(-eval/expr arg))))
(defvar *reminder* nil)
(defun time-lapse (magnitude unit)
(ecase (sym->key unit)
((:d :day :days) (* magnitude 86400))
((:h :hour :hours) (* magnitude 3600))
((:m :min :minute :minutes) (* magnitude 60))
((:s :sec :second :seconds) magnitude)))
(defun -eval/body (body)
;; handle SLACK, EMAIL, and other handlers
;; properly evlis'ing the arguments
(loop for call in body do
(cond ((atom call)
(error "invalid WHEN body (~A is not a list)" call))
((eq (sym->key (car call)) :remind)
(setf *reminder*
(cons :remind
(time-lapse (cadr call)
(caddr call)))))
((not (registered-plugin? (car call)))
(error "no plugin ~A registered..." (car call)))
(t (dispatch-to-plugin (car call)
(-eval/args (cdr call))))))
t)
(defun -eval/when (clause)
(if (or (eq (car clause) '*)
(-eval/expr `(and ,@(car clause))))
(-eval/body (cdr clause))))
(defun -eval/for (form)
(if (or (null form)
(and (atom form)
(not (eq form '*))))
(error "invalid form for FOR"))
(when (or (eq (car form) '*)
(-eval/expr (if (atom (car form))
`(is ,(car form))
(car form))))
(loop for clause in (cdr form) do
(if (not (eq (car clause) 'when))
(error "unexpected ~A clause in FOR (should have been a WHEN)" (car clause)))
(when (-eval/when (cdr clause))
(return-from -eval/for t))))
nil)
(defun -eval/set (form)
(if (not (eq (length form) 2))
(error "bad SET form"))
(setf *environment* (acons (car form)
(-eval/expr (cadr form))
*environment*)))
(defun eval/rules (rules params metadata)
(let ((*environment* '())
(*reminder* nil)
(*parameters* params)
(*metadata* metadata))
(loop for rule in rules do
(case (car rule)
(for (-eval/for (cdr rule)))
(set (-eval/set (cdr rule)))
(otherwise (error "unexpected top-level form (~A ...)" (car rule)))))
*reminder*))
(defun load/rules (str)
(let ((*package* (find-package "RULES")))
(read-from-string str)))