Debian Codegraph @

scheme/ikarus.pretty-print.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus pretty-print)
18 (export pretty-print pretty-width)
19 (import
20 (rnrs hashtables)
21 (only (ikarus writer) traverse traversal-helpers radix-prefix)
22 (only (ikarus.pretty-formats) get-fmt)
23 (except (ikarus) pretty-print pretty-width))
24 (define (map1ltr f ls)
25 ;;; ltr so that gensym counts get assigned properly
26 (cond
27 [(null? ls) '()]
28 [else
29 (let ([a (f (car ls))])
30 (cons a (map1ltr f (cdr ls))))]))
31
32 (define pretty-width
33 (make-parameter 60
34 (lambda (x)
35 (unless (and (exact? x) (integer? x) (> x 0))
36 (die 'pretty-width "invalid argument" x))
37 x)))
38
39 (define (pretty-indent) 1)
40 (define-struct cbox (length boxes))
41 (define-struct pbox (length ls last))
42 (define-struct mbox (length str val))
43 (define-struct vbox (length prefix ls))
44 (define-struct fbox (length box* sep*))
45 (define (box-length x)
46 (cond
47 [(string? x) (string-length x)]
48 [(cbox? x) (cbox-length x)]
49 [(pbox? x) (pbox-length x)]
50 [(mbox? x) (mbox-length x)]
51 [(vbox? x) (vbox-length x)]
52 [(fbox? x) (fbox-length x)]
53 [else (die 'boxify "invalid box" x)]))
54 (define (boxify x h)
55 (define shared-idx 0)
56 (define (conc . a*)
57 (let ([n
58 (let f ([a* a*] [len 0])
59 (cond
60 [(null? a*) len]
61 [else
62 (f (cdr a*) (fx+ len (box-length (car a*))))]))])
63 (make-cbox n a*)))
64 (define (boxify-list ls)
65 (define (sum-box* ls)
66 (cond
67 [(null? (cdr ls))
68 (box-length (car ls))]
69 [else
70 (fx+ (box-length (car ls))
71 (fxadd1 (sum-box* (cdr ls))))]))
72 (define (gensep*-default ls)
73 (cond
74 [(null? (cdr ls)) '()]
75 [else
76 (cons (pretty-indent) (gensep*-default (cdr ls)))]))
77 (define (tab-value x)
78 (cond
79 [(eq? x 'tab) (pretty-indent)]
80 [(fixnum? x) x]
81 [else #f]))
82 (define (select-alt alt-fmt* ls)
83 (define (good-match? fmt ls)
84 (cond
85 [(not (pair? fmt)) #t]
86 [(eq? (car fmt) 'read-macro)
87 (and (unshared-list? ls) (fx= (length ls) 2))]
88 [else
89 (let ([a (car fmt)] [fmt (cdr fmt)])
90 (cond
91 [(or (eq? a 'tab) (fixnum? a))
92 (good-match? fmt ls)]
93 [(and (pair? fmt) (eq? (car fmt) '...))
94 (and (unshared-list? ls)
95 (andmap (lambda (x) (good-match? a x)) ls))]
96 [(and (pair? ls) (not (graphed? ls)))
97 (and (good-match? a (car ls))
98 (good-match? fmt (cdr ls)))]
99 [else #f]))]))
100 (ormap (lambda (fmt) (and (good-match? fmt ls) fmt))
101 alt-fmt*))
102 (define (applicable-formats a alt-fmt*)
103 (cond
104 [(and (symbol? a) (get-fmt a)) =>
105 (lambda (fmt)
106 (cond
107 [(and (pair? fmt) (eq? (car fmt) 'alt))
108 (append alt-fmt* (cdr fmt))]
109 [else
110 (append alt-fmt* (list fmt))]))]
111 [(null? alt-fmt*) #f]
112 [else alt-fmt*]))
113 (define (return sep* box*)
114 (let ([n (sum-box* box*)])
115 (conc "(" (make-fbox n box* sep*) ")")))
116 (define (boxify-list ls alt-fmt*)
117 (let ([a (car ls)])
118 (cond
119 [(applicable-formats a alt-fmt*) =>
120 (lambda (fmt*)
121 (let ([fmt (select-alt fmt* ls)])
122 (module (fmt-dots? skip-fmt fmt-tab sub-fmt)
123 (define (parse-fmt x)
124 (define (parse-dots tab fmt x)
125 (cond
126 [(and (pair? x) (eq? (car x) '...))
127 (values tab fmt #t (cdr x))]
128 [else
129 (values tab fmt #f x)]))
130 (define (parse-tab tab x)
131 (cond
132 [(pair? x)
133 (parse-dots tab (car x) (cdr x))]
134 [else (values tab #f #f #f)]))
135 (cond
136 [(pair? x)
137 (let ([a0 (car x)])
138 (cond
139 [(eq? a0 'tab)
140 (parse-tab (pretty-indent) (cdr x))]
141 [(fixnum? a0)
142 (parse-tab a0 (cdr x))]
143 [else (parse-tab #f x)]))]
144 [else (values (pretty-indent) #f #f #f)]))
145 (define (fmt-dots? x)
146 (let-values ([(tab subfmt dots fmt) (parse-fmt x)])
147 dots))
148 (define (fmt-tab x)
149 (let-values ([(tab subfmt dots fmt) (parse-fmt x)])
150 tab))
151 (define (sub-fmt x)
152 (let-values ([(tab subfmt dots fmt) (parse-fmt x)])
153 subfmt))
154 (define (skip-fmt x)
155 (let-values ([(tab subfmt dots fmt) (parse-fmt x)])
156 fmt)))
157 (define (boxify/fmt fmt x)
158 (cond
159 [(and (pair? fmt) (unshared-list? x))
160 (boxify-list x
161 (if (eq? (car fmt) 'alt)
162 (cdr fmt)
163 (list fmt)))]
164 [else (boxify x)]))
165 (define (read-macro? x)
166 (and (pair? x) (eq? (car x) 'read-macro)))
167 (cond
168 [(read-macro? fmt)
169 (conc (cdr fmt) (boxify (cadr ls)))]
170 [(fmt-dots? fmt)
171 (return (fmt-tab fmt)
172 (map1ltr (lambda (x) (boxify/fmt (sub-fmt fmt) x))
173 ls))]
174 [else
175 (let ([a (boxify/fmt (sub-fmt fmt) a)])
176 (let-values ([(sep* ls)
177 (let f ([fmt (skip-fmt fmt)] [ls (cdr ls)])
178 (cond
179 [(null? ls)
180 (values '() '())]
181 [(fmt-dots? fmt)
182 (values (fmt-tab fmt)
183 (map1ltr
184 (lambda (x)
185 (boxify/fmt (sub-fmt fmt) x))
186 ls))]
187 [else
188 (let ([a
189 (boxify/fmt (sub-fmt fmt)
190 (car ls))])
191 (let-values ([(f^ l^)
192 (f (skip-fmt fmt)
193 (cdr ls))])
194 (values (cons (fmt-tab fmt) f^)
195 (cons a l^))))]))])
196 (return sep* (cons a ls))))])))]
197 [else
198 (return (gensep*-default ls) (map1ltr boxify ls))])))
199 (boxify-list ls '()))
200 (define (boxify-pair x)
201 (define (boxify-cdrs x)
202 (cond
203 [(and (pair? x) (not (graphed? x)))
204 (let ([a (boxify (car x))])
205 (let-values ([(ls last) (boxify-cdrs (cdr x))])
206 (values (cons a ls) last)))]
207 [else
208 (values '() (boxify x))]))
209 (let ([a (boxify (car x))])
210 (let-values ([(ls last) (boxify-cdrs (cdr x))])
211 (let ([ls (cons a ls)])
212 (let ([n
213 (let f ([ls ls] [n 4])
214 (cond
215 [(null? ls) n]
216 [else
217 (f (cdr ls)
218 (fx+ (fxadd1 n) (box-length (car ls))))]))])
219 (make-pbox (fx+ n (box-length last)) ls last))))))
220 (define (boxify-vector x)
221 (let ([ls (map1ltr boxify (vector->list x))])
222 (let ([n
223 (let f ([ls ls] [n 0])
224 (cond
225 [(null? ls) n]
226 [else
227 (f (cdr ls) (fx+ n (box-length (car ls))))]))])
228 (make-vbox (fx+ (fx+ n 2) (vector-length x)) "#" ls))))
229 (define (boxify-bytevector x)
230 (define prefix "#vu8")
231 (let ([ls (map (lambda (x) (radix-prefix (number->string x (print-radix))))
232 (bytevector->u8-list x))])
233 (let ([len (fold-left (lambda (ac s) (+ 1 ac (string-length s)))
234 (+ 1 (string-length prefix))
235 ls)])
236 (make-vbox len prefix ls))))
237 (define (graphed? x)
238 (import traversal-helpers)
239 (let ([b (hashtable-ref h x #f)])
240 (let ([b (if (fixnum? b) b (car b))])
241 (cond
242 [(cyclic-set? b) #t]
243 [(shared-set? b) (print-graph)]
244 [else #f]))))
245 (define (unshared-list? x)
246 ;;; all cdrs of non-empty list are not-shared?
247 (and (pair? x)
248 (let f ([x (cdr x)])
249 (or (null? x)
250 (and (pair? x)
251 (not (graphed? x))
252 (f (cdr x)))))))
253 (define (boxify-struct x)
254 (define (boxify-vanilla-struct x)
255 (cond
256 [(let ([rtd (struct-type-descriptor x)])
257 (and (record-type-descriptor? rtd)
258 (record-type-opaque? rtd)))
259 "#<unknown>"]
260 [else
261 (let* ([name (boxify (struct-name x))]
262 [ls
263 (let ([n (struct-length x)])
264 (let f ([i 0])
265 (cond
266 [(fx= i n) '()]
267 [else
268 (let ([a (boxify (struct-ref x i))])
269 (cons a (f (+ i 1))))])))]
270 [ls (cons name ls)]
271 [len (fold-left (lambda (ac s) (+ 1 ac (box-length s)))
272 -1 ls)])
273 (conc "#[" (make-fbox len ls #f) "]"))]))
274 (define (boxify-custom-struct out)
275 (import traversal-helpers)
276 (let ([ls
277 (let f ([cache (cdr out)])
278 (cond
279 [(not cache) (list (car out))]
280 [else
281 (let ([obj (boxify (cache-object cache))])
282 (let ([ls (f (cache-next cache))])
283 (cons* (cache-string cache) obj ls)))]))])
284 (let ([len (fold-left (lambda (ac s) (+ 1 ac (box-length s)))
285 -1 ls)])
286 (make-fbox len ls #f))))
287 (let ([b (hashtable-ref h x #f)])
288 (cond
289 [(pair? b) (boxify-custom-struct (cdr b))]
290 [else (boxify-vanilla-struct x)])))
291 (define (boxify-shared x k)
292 (import traversal-helpers)
293 (let ([b (hashtable-ref h x #f)])
294 (let ([b (if (fixnum? b) b (car b))])
295 (cond
296 [(mark-set? b)
297 (string-append "#"
298 (number->string (fxsra b mark-shift))
299 "#")]
300 [(or (cyclic-set? b)
301 (and (shared-set? b) (print-graph)))
302 (let ([n shared-idx])
303 (set! shared-idx (+ shared-idx 1))
304 (set-mark! x h n)
305 (let ([str (string-append "#" (number->string n) "=")])
306 (let ([xbox (k x)])
307 (make-cbox (+ (string-length str) (box-length xbox))
308 (list str xbox)))))]
309 [else (k x)]))))
310 (define (boxify x)
311 (cond
312 [(null? x) "()"]
313 [(vector? x) (boxify-shared x boxify-vector)]
314 [(unshared-list? x) (boxify-shared x boxify-list)]
315 [(pair? x) (boxify-shared x boxify-pair)]
316 [(bytevector? x) (boxify-shared x boxify-bytevector)]
317 [(struct? x) (boxify-shared x boxify-struct)]
318 ;[(setbox? x)
319 ; (let ([i (format "#~a=" (setbox-idx x))]
320 ; [b (boxify (setbox-data x))])
321 ; (make-cbox (+ (string-length i) (box-length b))
322 ; (list i b)))]
323 ;[(refbox? x) (format "#~a#" (refbox-idx x))]
324 [else (format "~s" x)]))
325 (boxify x))
326 (define string-esc-table
327 '((7 . "a")
328 (8 . "b")
329 (9 . "t")
330 (10 . "n")
331 (11 . "v")
332 (12 . "f")
333 (13 . "r")
334 (34 . "\"")
335 (92 . "\\")))
336 (define (hexify n)
337 (cond
338 [(fx< n 10) (integer->char (fx+ n (char->integer #\0)))]
339 [else (integer->char (fx+ (fx- n 10) (char->integer #\A)))]))
340 (define (output x p)
341 (define (output-cbox x p col)
342 (let g ([ls (cbox-boxes x)] [p p] [col col])
343 (cond
344 [(null? ls) col]
345 [else
346 (g (cdr ls) p
347 (f (car ls) p col))])))
348 (define (tab col p)
349 (newline p)
350 (let f ([col col] [p p])
351 (unless (fxzero? col)
352 (display #\space p)
353 (f (fxsub1 col) p))))
354 (define (output-pbox x p col)
355 (define (pbox-one-line x p col)
356 (display "(" p)
357 (let g ([ls (pbox-ls x)]
358 [p p]
359 [col (fx+ col 1)]
360 [last (pbox-last x)])
361 (cond
362 [(null? ls)
363 (display ". " p)
364 (let ([col (f last p (fx+ col 2))])
365 (display ")" p)
366 (fx+ col 1))]
367 [else
368 (let ([col (f (car ls) p col)])
369 (display " " p)
370 (g (cdr ls) p (fx+ col 1) last))])))
371 (define (pbox-multi-fill x p col)
372 (display "(" p)
373 (let g ([ls (cdr (pbox-ls x))]
374 [p p]
375 [start-col (fx+ col 1)]
376 [col (f (car (pbox-ls x)) p (fx+ col 1))]
377 [last (pbox-last x)])
378 (cond
379 [(null? ls)
380 (let ([n (box-length last)])
381 (let ([col
382 (cond
383 [(fx<= (fx+ (fx+ col n) 4) (pretty-width))
384 (display " . " p)
385 (fx+ col 3)]
386 [else
387 (tab start-col p)
388 (display ". " p)
389 (fx+ start-col 2)])])
390 (let ([col (f last p col)])
391 (display ")" p)
392 (fx+ col 1))))]
393 [(fx<= (fx+ (fx+ col 1) (box-length (car ls)))
394 (pretty-width))
395 (display " " p)
396 (g (cdr ls) p start-col
397 (f (car ls) p (fx+ col 1))
398 last)]
399 [else
400 (tab start-col p)
401 (g (cdr ls) p start-col
402 (f (car ls) p start-col)
403 last)])))
404 (cond
405 [(fx<= (fx+ col (pbox-length x)) (pretty-width))
406 (pbox-one-line x p col)]
407 [else
408 (pbox-multi-fill x p col)]))
409 (define (output-mbox x p col)
410 (display (mbox-str x) p)
411 (f (mbox-val x) p (fx+ col (string-length (mbox-str x)))))
412 (define (output-vbox x p col)
413 (display (vbox-prefix x) p)
414 (let ([ls (vbox-ls x)] [col (+ col (string-length (vbox-prefix x)))])
415 (cond
416 [(null? ls)
417 (display "()" p)
418 (fx+ col 2)]
419 [else
420 (display "(" p)
421 (let g ([ls (cdr ls)] [p p]
422 [col (f (car ls) p (fx+ col 1))]
423 [start (fx+ col 1)])
424 (cond
425 [(null? ls)
426 (display ")" p)
427 (fx+ col 1)]
428 [(fx<= (fx+ (fx+ col 1) (box-length (car ls))) (pretty-width))
429 (display " " p)
430 (g (cdr ls) p
431 (f (car ls) p (fx+ col 1))
432 start)]
433 [else
434 (tab start p)
435 (g (cdr ls) p
436 (f (car ls) p start)
437 start)]))])))
438 (define (output-fbox x p col)
439 (define (output-rest-cont box* sep* p col left)
440 (cond
441 [(null? box*) col]
442 [(pair? sep*)
443 (let* ([box (car box*)]
444 [sep (car sep*)]
445 [w (box-length box)])
446 (cond
447 [(fx<= (fx+ (fxadd1 w) col) (pretty-width))
448 (display " " p)
449 (output-rest-cont (cdr box*) (cdr sep*) p
450 (f box p (fxadd1 col)) left)]
451 [(not sep)
452 (display " " p)
453 (output-rest-multi (cdr box*) (cdr sep*) p
454 (f box p (fxadd1 col)) left)]
455 [else
456 (let ([col (fx+ left sep)])
457 (tab col p)
458 (cond
459 [(fx<= (fx+ w col) (pretty-width))
460 (output-rest-cont (cdr box*) (cdr sep*) p
461 (f box p col) left)]
462 [else
463 (output-rest-multi (cdr box*) (cdr sep*) p
464 (f box p col) left)]))]))]
465 [else
466 (output-last-cont box* sep* p col left)]))
467 (define (output-last-cont box* sep p col left)
468 (define (sum ls)
469 (cond
470 [(null? ls) 0]
471 [else (fx+ (box-length (car ls))
472 (fxadd1 (sum (cdr ls))))]))
473 (cond
474 [(not sep)
475 (output-rest-cont box* '(#f . #f) p col left)]
476 [(fx<= (fx+ (sum box*) col) (pretty-width))
477 (let g ([box* box*] [p p] [col col])
478 (cond
479 [(null? box*) col]
480 [else
481 (display " " p)
482 (g (cdr box*) p (f (car box*) p (fxadd1 col)))]))]
483 [else
484 (let g ([box* box*] [p p] [left (fx+ left sep)] [col col])
485 (cond
486 [(null? box*) col]
487 [else
488 (tab left p)
489 (g (cdr box*) p left
490 (f (car box*) p left))]))]))
491 (define (output-last-multi box* sep p col left)
492 (define (sum ls)
493 (cond
494 [(null? ls) 0]
495 [else (fx+ (box-length (car ls))
496 (fxadd1 (sum (cdr ls))))]))
497 (cond
498 [(not sep)
499 (output-rest-multi box* '(#f . #f) p col left)]
500 [else
501 (let g ([box* box*] [p p] [left (fx+ left sep)] [col col])
502 (cond
503 [(null? box*) col]
504 [else
505 (tab left p)
506 (g (cdr box*) p left
507 (f (car box*) p left))]))]))
508 (define (output-rest-multi box* sep* p col left)
509 (cond
510 [(null? box*) col]
511 [(pair? sep*)
512 (let* ([box (car box*)]
513 [sep (car sep*)]
514 [w (box-length box)])
515 (cond
516 [(not sep)
517 (display " " p)
518 (output-rest-multi (cdr box*) (cdr sep*) p
519 (f box p (fxadd1 col)) left)]
520 [else
521 (let ([col (fx+ left sep)])
522 (tab col p)
523 (cond
524 [(fx<= (fx+ w col) (pretty-width))
525 (output-rest-cont (cdr box*) (cdr sep*) p
526 (f box p col) left)]
527 [else
528 (output-rest-multi (cdr box*) (cdr sep*) p
529 (f box p col) left)]))]))]
530 [else (output-last-multi box* sep* p col left)]))
531 (define (output-box-init box box* sep* p left)
532 (let ([w (box-length box)])
533 (cond
534 [(fx<= (fx+ w left) (pretty-width))
535 (let ([col (f box p left)])
536 (output-rest-cont box* sep* p col left))]
537 [else
538 (let ([col (f box p left)])
539 (output-rest-multi box* sep* p col left))])))
540 (let ([box* (fbox-box* x)]
541 [sep* (fbox-sep* x)])
542 (output-box-init (car box*) (cdr box*) sep* p col)))
543 (define (f x p col)
544 (cond
545 [(string? x)
546 (display x p)
547 (fx+ col (string-length x))]
548 [(cbox? x) (output-cbox x p col)]
549 [(pbox? x) (output-pbox x p col)]
550 [(mbox? x) (output-mbox x p col)]
551 [(vbox? x) (output-vbox x p col)]
552 [(fbox? x) (output-fbox x p col)]
553 [else (die 'pretty-print-output "invalid" x)]))
554 (f x p 0)
555 (newline p))
556 ;;;
557
558 (define (pretty x p)
559 (let ([h (make-eq-hashtable)])
560 (traverse x h)
561 (output (boxify x h) p)))
562 ;;;
563 (define pretty-print
564 (case-lambda
565 [(x) (pretty x (current-output-port))]
566 [(x p)
567 (if (output-port? p)
568 (pretty x p)
569 (die 'pretty-print "not an output port" p))]))
570
571 )