-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmacro-utils.el
317 lines (272 loc) · 8.11 KB
/
macro-utils.el
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
(require 'utils)
(require 'cl)
(progn
(defun non-empty-listp (o)
(and (listp o)
(> (length o) 0)))
(defun quotep (o)
(and (listp o)
(> (length o) 0)
(eq (car o) 'quote)))
(setq *setter-symbols*
'(setq setf))
(defun setterp (o)
(and (listp o)
(> (length o) 1)
(let ((type (first o)))
(foldl
(lambda (it ac)
(or ac (eq o it)))
nil
*setter-symbols*))))
(defun get-setter-symbols (form)
(let-repeatedly _ (cdr form)
(loop for f in _ and
i from 1
when (oddp i) collect f)))
(defun get-setter-expressions (form)
(let-repeatedly _ (cdr form)
(loop for f in _ and
i from 1
when (evenp i) collect f)))
(defun setter-binds-symbolp (sym form)
($ sym in (get-setter-symbols form)))
(dont-do
(get-setter-symbols '(setq x 10 y 11 z 12))
(get-setter-expressions '(setq x 10 y 11 z 12))
)
(setq *letter-symbols*
'(let let* lexical-let lexical-let*))
(defun function-formp (form)
(and (non-empty-listp form)
(eq (car form) 'function)))
(defun prognp (form)
(and (non-empty-listp form)
(eq (car form) 'progn)))
(defun prog1p (form)
(and (non-empty-listp form)
(eq (car form) 'prog1)))
(defun prog-like (form)
(or (prognp form)
(prog1p form)))
(defun letp (o)
(and (non-empty-listp o)
(eq (car o) 'let)))
(defun let*p (o)
(and (non-empty-listp o)
(eq (car o) 'let*)))
(defun let/*p (o) (or (letp o)
(let*p o)))
(defun defunp (o)
(and (non-empty-listp o)
(eq (car o) 'defun)))
(defun setqp (o)
(and (non-empty-listp o)
(eq (car o) 'setq)))
(defun let-likep (o)
(and (non-empty-listp o)
(let ((type (car o)))
(foldl
(lambda (it ac)
(or ac
(eq it type)))
nil
*letter-symbols*))))
(defun let-form-binds-symbolp (sym form)
($ sym in (mapcar #'car (second form))))
(defun condp (o)
(and (non-empty-listp o)
(eq (car o) 'cond)))
(defun* count-free-usages-cond (sym form &optional (n 0))
(sum (cons n (mapcar
(lambda (x)
(+ (count-free-usages sym (car x))
(count-free-usages sym (cadr x))))
(cdr form)))))
(defun get-let-body (let-form)
(cddr let-form))
(defun get-let-binders (let-form)
(cadr let-form))
(defun get-cond-predicates (cond-form)
(mapcar #'car (cdr cond-form)))
(defun get-cond-bodies (cond-form)
(mapcar #'cdr (cdr cond-form)))
(dont-do
(get-let-body '(let ((a b) (c d)) (+ a b) (+ c d))))
(defun lambdap (o)
(and (non-empty-listp o)
(eq (car o) 'lambda)))
(defun lambda-list-optionals (llist)
(let-repeatedly _ (member-if (lambda (x) (eq '&optional x))
llist)
(if _ (cdr _) _)))
(defun lambda-list-rest (llist)
(let-repeatedly _ (member-if (lambda (x) (eq '&rest x))
llist)
(if _ (cdr _) _)))
(defun lambda-list-normal-args (llist)
(let-repeatedly _
(split-list-right
llist
(lambda (x)
(or
(eq x '&rest)
(eq x '&optional))))
(first _)))
(defun lambda-form-binds-symbolp (sym form)
(let ((llist (cadr form)))
($ sym in
(filter (lambda (x) (not (or (eq x '&optional)
(eq x '&rest))))
llist))))
(defun get-lambda-body (form)
(cddr form))
(dont-do
(lambda-form-binds-symbolp 'x (lambda (x y &rest z) (+ x y)))
(funcall (lambda (x &optional y) (list x y ))
1 2)
(get-lambda-body '(lambda (x y z) (+ a b c) (+ q r)))
)
(setq *defunners* '(defun defun*))
(defun defun-likep (o)
(and (non-empty-listp o)
($ (car o) in *defunners* #'eq)))
(defun third-on (lst)
(nthcdr 2 lst))
(defun fletp (form)
(and (listp form)
(eq (car form) 'flet)))
(defun* count-free-usages-let (sym form &optional (n 0))
(let ((n-in-binders
(apply #'+
(mapcar
(lambda (x) (count-free-usages sym (cadr x) 0))
(get-let-binders form))))
(n-in-body (count-free-usages sym `(progn ,@(get-let-body form)) 0)))
(+ n-in-body n-in-binders)))
(defun* count-free-usages-let* (sym form &optional (n 0))
(let* ((binders-before-binding-sym
(car (split-list-left (get-let-binders form) (lambda (br) (eq (car br) sym)))))
(n-in-unbound-binders
(apply #'+
(mapcar
(lambda (x) (count-free-usages sym (cadr x) 0))
binders-before-binding-sym))))
(if (= (length (get-let-binders form))
(length binders-before-binding-sym))
(+ n-in-unbound-binders (count-free-usages sym `(progn ,@(get-let-body form)) 0))
n-in-unbound-binders)))
(defun* count-free-usages-let-like (sym form &optional (n 0))
(cond
((letp form) (count-free-usages-let sym form n))
((let*p form) (count-free-usages-let* sym form n))))
(defun* count-free-usages (sym form &optional (n 0))
(cond
((symbolp form)
(if (eq form sym) (+ n 1) n))
((numberp form) n)
((stringp form) n)
((listp form)
(cond
((or (prog1p form) (prognp form))
(apply #'+ (cons n (mapcar
(lambda (sform) (count-free-usages sym sform))
(cdr form)))))
((let-likep form)
(count-free-usages-let-like sym form n))
((lambdap form)
(if (lambda-form-binds-symbolp sym form) n
(apply #'+ (cons
n
(mapcar
(lambda (sform)
(count-free-usages sym sform))
(get-lambda-body form))))))
((condp form)
(count-free-usages-cond sym form n))
((setterp form)
(apply #'+
(cons n
(mapcar
(lambda (sform)
(count-free-usages sym sform))
(get-setter-expressions form)))))
(t (apply #'+ (cons n (mapcar (lambda (subform)
(count-free-usages sym subform 0))
(cdr form)))))))))
(defun* count-free-usages-functions (sym form &optional (n 0))
(cond
((symbolp form)
(if (eq form sym) (+ n 1) n))
((numberp form) n)
((stringp form) n)
((listp form)
(cond
((or (prog1p form) (prognp form))
(apply #'+ (cons n (mapcar
(lambda (sform) (count-free-usages sym sform))
(cdr form)))))
((let-likep form)
(count-free-usages-let-like sym form n))
((lambdap form)
(if (lambda-form-binds-symbolp sym form) n
(apply #'+ (cons
n
(mapcar
(lambda (sform)
(count-free-usages sym sform))
(get-lambda-body form))))))
((condp form)
(count-free-usages-cond sym form n))
((setterp form)
(apply #'+
(cons n
(mapcar
(lambda (sform)
(count-free-usages sym sform))
(get-setter-expressions form)))))
(t (apply #'+ (cons n (mapcar (lambda (subform)
(count-free-usages sym subform 0))
(cdr form)))))))))
(dont-do
(count-free-usages 'x '(let* ((z (+ x 1)) (x (+ x 10)) (y (+ x 10)) (x 10)) (+ x x)))
(count-free-usages 'x '(let ((a b)) x x (lambda (x) (+ x x)))))
(defun print-and-return (f)
(print f)
f)
(defun optimize-let-form-once (form)
(let ((type-of-let (first form))
(bindings (second form))
(body (third-on form)))
`(,type-of-let ,(loop
with new-bindings = nil
for binding in bindings
and i from 1
do
(let* ((sym (car binding))
(val-form (cadr binding))
(subsequent-bindings
(nthcdr i bindings))
(n-uses-in-sub-bindings
(count-free-usages sym `(,type-of-let ,subsequent-bindings ,@body))))
(if (> n-uses-in-sub-bindings 0) (push binding new-bindings)))
finally (return (reverse new-bindings))) ,@body)))
(defun optimize-let-form (form)
(fix #'optimize-let-form-once form 10))
(defun symbols-in-form (form)
(unique (filter #'symbolp (flatten form))))
(defun nil->tbl (x)
(if x x (tbl!)))
(setf *fundamentals* '(if let let* cond lambda defun))
(dont-do
(symbols-in-form '(let ((a 10) (y 11)) (+ a y x)))
(optimize-let-form '(let* ((x 10) (y 11) (z (+ x x))) (+ x z)))
(count-free-usages 'x '(let ((y 11) (z (+ x x))) (+ x z)))
(get-let-body '(let ((y 11) (z (+ x x))) (+ x z)))
(count-free-usages 'x '(let ((x 10) (y 11) (z (+ x x))) (+ x z)))
(count-free-usages 'x '(let* ((x 10) (y 11) (z (x x x))) (+ x z)))
(count-free-usages 'z '(let* nil (+ x z)))
(length (get-let-binders '(let* nil (+ x z))))
(count-free-usages-let* 'z '(let* nil (+ x z)))
(count-free-usages 'z (get-let-body '(let* nil (+ x z))) 0)))
(provide 'macro-utils)