-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrind
More file actions
287 lines (261 loc) · 6.13 KB
/
grind
File metadata and controls
287 lines (261 loc) · 6.13 KB
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
; Kilo LISP Grinder (Pretty-Printer)
(setq obperln #123456789012)
(setq longlen #123456)
(setq off nil)
(setq lp '/()
(setq rp '/))
(setq sp '/ )
(setq nl '/
)
(setq dot '/ /./ )
(setq zerop null)
(setq dec cdr)
(setq inc
(lambda (x)
(cons 'i x)))
(setq add
(lambda (x y)
(conc x y)))
(setq spaces
(lambda (n)
(cond ((zerop n))
(else (prin1 sp)
(spaces (dec n))))))
(setq terpri
(lambda ()
(print sp)
(spaces off)))
(setq when
(macro
(lambda (p . xs)
@(if ,p (prog . ,xs) nil))))
(setq with
(macro
(lambda (b . xs)
(let ((v (car b))
(a (cadr b))
(g (gensym))
(r (gensym)))
@((lambda (,g)
(setq ,v ,a)
(let ((,r (prog . ,xs)))
(setq ,v ,g)
,r))
,v)))))
(setq shortp
(lambda (x)
(labels
((f (lambda (x n)
(cond ((atom n) nil)
((null x) n)
((atom x) (cdr n))
(else (f (car x)
(f (cdr x) n)))))))
(f x longlen))))
(setq simplep
(lambda (a)
(cond ((null a))
((atom (car a)) (simplep (cdr a)))
(else nil))))
(setq pppair
(lambda (x)
(prin1 lp)
(loop next ((x x)
(s nil)
(n obperln))
(cond ((null x))
((zerop n)
(print sp)
(spaces (inc off))
(next x nil obperln))
((not (atom (car x)))
(if s (prin1 sp) nil)
(with (off (inc off))
(pppair (car x))
(when (not (null (cdr x)))
(terpri)))
(next (cdr x) nil obperln))
((not (atom x))
(if s (prin1 sp) nil)
(ppobj (car x))
(next (cdr x) t (dec n)))
((not (null x))
(prin1 dot)
(ppobj x))))
(prin1 rp)))
(setq ppobj
(lambda (x)
(if (atom x)
(prin1 x)
(pppair x))))
(setq ppbody
(lambda (x)
(cond ((null x))
(else
(pp (car x))
(when (not (null (cdr x)))
(terpri))
(ppbody (cdr x))))))
(setq ppind
(lambda (x n)
(cond ((simplep x)
(prin1 x))
(else
(prin1 lp)
(prin1 (car x))
(when (not (null (cdr x)))
(prin1 sp)
(with (off (add n off))
(ppbody (cdr x))))
(prin1 rp)))))
(setq ppsub
(lambda (x)
(prin1 lp)
(prin1 (car x))
(when (not (null (cdr x)))
(with (off (add #12 off))
(terpri)
(ppbody (cdr x))))
(prin1 rp)))
(setq ppquote
(lambda (x)
(let ((i #1))
(cond ((eq 'quote (car x))
(prin1 '/'))
((eq 'qquote (car x))
(prin1 '/@))
((eq 'unquote (car x))
(prin1 '/,))
((eq 'splice (car x))
(prin1 '/,/@)
(setq i (inc i))))
(with (off (add i off))
(if (and (eq 'quote (car x))
(not (atom (cadr x))))
(pppair (cadr x))
(pp (cadr x)))))))
(setq pplam
(lambda (x)
(prin1 lp)
(prin1 'lambda)
(prin1 sp)
(pp (cadr x))
(with (off (add #12 off))
(terpri)
(ppbody (cddr x))
(prin1 rp))))
(setq ppapp
(lambda (x)
(prin1 lp)
(with (off (inc off))
(pp (car x))
(when (not (null (cdr x)))
(terpri))
(ppbody (cdr x))
(prin1 rp))))
(setq ppsetq
(lambda (x)
(cond ((eq 'lambda (car (caddr x)))
(prin1 lp)
(prin1 'setq)
(prin1 sp)
(prin1 (cadr x))
(prin1 sp)
(with (off (add #12 off))
(terpri)
(pp (caddr x)))
(prin1 rp))
(else
(prin1 x)))))
(setq ppbind
(lambda (x)
(prin1 lp)
(with (off (inc off))
(loop next ((x x))
(cond ((null x))
(else
(prin1 lp)
(pp (caar x))
(prin1 sp)
(pp (cadar x))
(prin1 rp)
(when (not (null (cdr x)))
(terpri))
(next (cdr x))))))
(prin1 rp)))
(setq ppcls
(lambda (x)
(loop next ((x x))
(cond ((null x))
(else
(prin1 lp)
(pp (caar x))
(cond ((null (cdar x)))
(else
(with (off (add #12 off))
(terpri)
(ppbody (cdar x)))))
(prin1 rp)
(when (not (null (cdr x)))
(terpri))
(next (cdr x)))))))
(setq ppform
(lambda (x n f)
(prin1 lp)
(prin1 (car x))
(prin1 sp)
(with (off (add n off))
(f (cdr x)))
(prin1 rp)))
(setq ppbform
(lambda (x n nl sym)
(prin1 lp)
(prin1 (car x))
(when sym
(prin1 sp)
(prin1 (cadr x))
(setq x (cdr x)))
(with (off (add n off))
(if nl
(terpri)
(prin1 sp))
(ppbind (cadr x)))
(with (off (add #12 off))
(terpri)
(ppbody (cddr x)))
(prin1 rp)))
(setq pp
(lambda (x)
(cond ((atom x)
(prin1 x))
((not (atom (car x)))
(ppapp x))
((memb (car x) '(quote qquote unquote splice))
(ppquote x))
((eq 'lambda (car x))
(pplam x))
((memb (car x) '(or if))
(ppind x #1234))
((eq x 'and)
(ppind x #12345))
((eq 'ifnot (car x))
(ppind x #1234567))
((eq 'let (car x))
(ppbform x #12345 nil nil))
((eq 'labels (car x))
(ppbform x #12 t nil))
((eq 'loop (car x))
(ppbform x #123456 t t))
((eq 'cond (car x))
(ppform x #123456 ppcls))
((eq 'setq (car x))
(ppsetq x))
((not (shortp x))
(ppsub x))
(else
(prin1 x)))))
(setq grind
(lambda (x)
(pp x)
(print sp)
t))