This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt.lisp
323 lines (288 loc) · 7.33 KB
/
fmt.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
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
318
319
320
321
322
323
(defun fmt-loop-clause-n (col s n)
(values
(let ((u
(apply
#'concatenate
'string
(loop
for i below n
collect (fmt-inline (pop s))
collect " "))))
(concatenate 'string (indent col s) u (fmt (+ col (length u)) (pop s))))
s))
(defun fmt-loop-clause (col s)
(values
(let ((indent (indent col s))
(u (when (atom (car s)) (fmt-atom (car s)))))
(cond
((not s)
nil)
((consp (car s))
(concatenate 'string indent (fmt col (pop s))))
; 6.1.2.1.1 The for-as-arithmetic subclause
; 6.1.2.1.2 The for-as-in-list subclause
; 6.1.2.1.3 The for-as-on-list subclause
; 6.1.2.1.5 The for-as-across subclause
((and (member (car s) '(for as)) (preposition (caddr s)))
(pop s)
(apply
#'concatenate
'string
indent
u
" "
(fmt-inline (pop s))
(loop
while (preposition (car s))
collect " "
collect (fmt-atom (pop s))
collect " "
collect (fmt-inline (pop s)))))
; 6.1.2.1.4 The for-as-equals-then subclause
; 6.1.2.1.6 The for-as-hash subclause
; 6.1.2.1.7 The for-as-package subclause
((and (member (car s) '(for as)) (eq (caddr s) 'being))
(pop s)
(concatenate
'string
indent
u
" "
(fmt-inline (pop s))
" "
(fmt-inline (pop s))))
; 6.1.2.2 Local Variable Initializations
((and (eq (car s) 'with) (eq (caddr s) '=))
(setf (values u s) (fmt-loop-clause-n col s 3))
(apply
#'concatenate
'string
u
(loop
while (eq (car s) 'and)
do
(setf (values u s) (fmt-loop-clause-n col s 3))
collect u)))
; 6.1.5 Unconditional Execution Clauses
((member (car s) '(do doing))
(pop s)
(concatenate 'string indent u))
; 6.1.6 Conditional Execution Clauses
((member (car s) '(if when unless))
(pop s)
(concatenate 'string indent u " " (fmt (+ col (length u) 1) (pop s))))
; Other keyword
(t
(pop s)
(if s
(concatenate 'string indent u " " (fmt (+ col (length u) 1) (pop s)))
(concatenate 'string indent u)))
; For item = (length stack) then (pop stack)
; For i fixnum from 3
))
s))
(defun preposition (a)
(member
a
'(; 6.1.2.1.1 The for-as-arithmetic subclause
from
downfrom
upfrom
to
downto
upto
below
above
by
; 6.1.2.1.2 The for-as-in-list subclause
in
; 6.1.2.1.3 The for-as-on-list subclause
on
; 6.1.2.1.5 The for-as-across subclause
across)))
(defun fmt-loop (col a)
(destructuring-bind
(op &rest body)
a
(setf op (fmt-inline op))
(format nil "(~a~a)"
op
(apply
#'concatenate
'string
(let ((u))
(loop
while body
do
(setf (values u body) (fmt-loop-clause (1+ col) body))
collect u))))))
(defun fmt-clause (col a)
(if (eq (car a) +special+)
(fmt-special col a)
(format nil "(~a)"
(fmt-lines-indent-separator (1+ col) a))))
(defun fmt-clauses (col s)
(apply
#'concatenate
'string
(loop
for a in s
collect (indent col (list a))
collect (fmt-clause col a))))
(defun fmt-cond (col a)
(destructuring-bind
(op &rest clauses)
a
(setf op (fmt-inline op))
(format nil "(~a~a)"
op
(fmt-clauses (+ col 2) clauses))))
(defun fmt-case (col a)
(destructuring-bind
(op key &rest clauses)
a
(setf op (fmt-inline op))
(format nil "(~a ~a~a)"
op
(fmt-inline key)
(fmt-clauses (+ col 2) clauses))))
(defun fmt-atom (a)
(if (characterp a)
(format nil "#\\~:c"
a)
(let ((*print-case* :downcase))
(prin1-to-string a))))
(defun fmt-inline (a)
(cond
((atom a)
(fmt-atom a))
((eq (car a) +special+)
(concatenate
'string
(cadr a)
(format nil "~{~a~^ ~}"
(mapcar #'fmt-inline (cddr a)))))
(t
(format nil "(~{~a~^ ~})"
(mapcar #'fmt-inline a)))))
(defun fmt-params (col params)
(format nil "~{~a~^ ~}"
(mapcar #'fmt-inline params)))
(defun indent (col s)
(cond
((not s)
"")
((blankp (car s))
#.(format nil "~%"))
(t
(concatenate
'string
#.(format nil "~%")
(make-array col :initial-element #\Space)))))
(defun fmt-lines-indent-prefix (col s)
(apply
#'concatenate
'string
(loop
while s
for a
= (pop s)
collect (indent col (list a))
collect (fmt col a)
if (and (keywordp a) s)
collect " "
and collect
(fmt (+ col 1 (length (fmt-atom a))) (pop s)))))
(defun fmt-lines-indent-separator (col s)
(apply
#'concatenate
'string
(loop
for (a . more) on s
collect (fmt col a)
collect (indent col more))))
(defun fmt-let (col a)
(destructuring-bind
(op vars &rest body)
a
(setf op (fmt-inline op))
(format nil "(~a (~a)~a)"
op
(fmt-lines-indent-separator (+ col 1 (length op) 2) vars)
(fmt-lines-indent-prefix (+ col 2) body))))
(defun fmt-defun (col a)
(destructuring-bind
(op name params &rest body)
a
(setf op (fmt-inline op))
(format nil "(~a ~a (~a)~a)"
op
(fmt-inline name)
(fmt-params (+ col 1 (length op) 1) params)
(fmt-lines-indent-prefix (+ col 2) body))))
(defun fmt0 (col a)
(destructuring-bind
(op &rest body)
a
(setf op (fmt-inline op))
(format nil "(~a~a)"
op
(fmt-lines-indent-prefix (+ col 2) body))))
(defun fmt1 (col a)
(destructuring-bind
(op param1 &rest body)
a
(setf op (fmt-inline op))
(format nil "(~a ~a~a)"
op
(fmt-inline param1)
(fmt-lines-indent-prefix (+ col 2) body))))
(defun fmt2 (col a)
(destructuring-bind
(op param1 param2 &rest body)
a
(setf op (fmt-inline op))
(format nil "(~a ~a ~a~a)"
op
(fmt-inline param1)
(fmt-inline param2)
(fmt-lines-indent-prefix (+ col 2) body))))
(defun fmt-special (col a)
(if (cddr a)
(concatenate
'string
(cadr a)
(fmt (+ col (length (cadr a))) (caddr a))
(fmt-lines-indent-prefix (+ col 2) (cdddr a)))
(cadr a)))
(defun fmt (col a)
(cond
((atom a)
(fmt-atom a))
; Known forms
((member (car a) '(case))
(fmt-case col a))
((member (car a) '(acond cond))
(fmt-cond col a))
((member (car a) ' (defmacro defun))
(fmt-defun col a))
((member (car a) '(let let*))
(fmt-let col a))
((eq (car a) 'loop)
(fmt-loop col a))
; Known forms, grouped
((member (car a) '(dolist dotimes with-open-file when unless if lambda))
(fmt1 col a))
((member (car a) '(format))
(fmt2 col a))
; Fits on one line
((<= (+ col (length (fmt-inline a))) *right-margin*)
(fmt-inline a))
; Needs multiple lines
((eq (car a) +special+)
(fmt-special col a))
(t
(fmt0 col a))))
(defun fmt-all (s)
(format nil "~a~%"
(fmt-lines-indent-separator 0 s)))