Debian Codegraph @

scheme/ikarus.pretty-formats.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-formats)
18 (export get-fmt pretty-format)
19 (import (except (ikarus) pretty-format))
20
21 (define h (make-eq-hashtable))
22
23 (define (get-fmt name)
24 (hashtable-ref h name #f))
25
26 (define (set-fmt! name fmt)
27 (hashtable-set! h name fmt))
28
29 (define pretty-format
30 (lambda (x)
31 (unless (symbol? x)
32 (die 'pretty-format "not a symbol" x))
33 (case-lambda
34 [() (hashtable-ref h x #f)]
35 [(v) (hashtable-set! h x v)])))
36
37 ;;; standard formats
38 (set-fmt! 'quote '(read-macro . "'"))
39 (set-fmt! 'unquote '(read-macro . ","))
40 (set-fmt! 'unquote-splicing '(read-macro . ",@"))
41 (set-fmt! 'quasiquote '(read-macro . "`"))
42 (set-fmt! 'syntax '(read-macro . "#'"))
43 (set-fmt! 'quasisyntax '(read-macro . "#`"))
44 (set-fmt! 'unsyntax '(read-macro . "#,"))
45 (set-fmt! 'unsyntax-splicing '(read-macro . "#,@"))
46 ;(set-fmt! '|#primitive| '(read-macro . "#%"))
47 (set-fmt! 'let '(alt
48 (_ (0 [e 0 e] ...) tab e ...)
49 (_ x (0 [e 0 e] ...) tab e ...)))
50 (set-fmt! 'letrec '(_ (0 [e 0 e] ...) tab e ...))
51 (set-fmt! 'letrec* '(_ (0 [e 0 e] ...) tab e ...))
52 (set-fmt! 'let-syntax '(_ (0 [e 0 e] ...) tab e ...))
53 (set-fmt! 'letrec-syntax '(_ (0 [e 0 e] ...) tab e ...))
54 (set-fmt! 'let* '(_ (0 [e 0 e] ...) tab e ...))
55 (set-fmt! 'let-values '(_ (0 [e 0 e] ...) tab e tab e* ...))
56 (set-fmt! 'cond '(_ tab [0 e ...] ...))
57 (set-fmt! 'define '(_ name tab e ...))
58 (set-fmt! 'set! '(_ name tab e))
59 (set-fmt! 'case-lambda
60 '(_ tab [0 e ...] ...))
61 (set-fmt! 'struct-case
62 '(_ e tab [e 0 e ...] ...))
63 (set-fmt! 'if '(_ test 3 e ...))
64 (set-fmt! 'and '(and test 4 e ...))
65 (set-fmt! 'or '(or test 3 e ...))
66 (set-fmt! 'begin '(_ tab e ...))
67 (set-fmt! 'lambda '(_ fmls tab e tab e* ...))
68 (set-fmt! 'case '(_ e tab [e 0 e] ...))
69 (set-fmt! 'syntax-rules '(_ kwd* tab [e 0 e] ...))
70 (set-fmt! 'syntax-case '(_ expr kwd*
71 tab (e 0 e 0 e ...) ...))
72 (set-fmt! 'module '(alt (_ (fill ...) tab e ...)
73 (_ name (fill ...) tab e ...)))
74 (set-fmt! 'library '(_ name tab e ...))
75 (set-fmt! 'import '(_ tab e ...))
76
77 )
78
79
80 #!eof
81
82 (define (test x)
83 (pretty-print x)
84 (printf "====================================\n"))
85
86 (test 12)
87 (test #t)
88 (test #f)
89 (test (if #f #f))
90 (test '())
91 (test "string")
92 (test "\n")
93 (test "\r")
94 (test (string (integer->char 0)))
95 (test (string (integer->char 240)))
96 (test 'hello)
97 (test '(foo bar))
98 (test '
99 (define pp
100 (case-lambda
101 [(x) (pretty x (current-output-port))]
102 [(x p)
103 (if (output-port? p)
104 (pretty x p)
105 (die 'pretty-print "not an output port" p))])))
106
107 (test '(384 7384 83947 893478 9137489 3894789 134789314 79817238
108 97314897 318947138974 981374 89137489 1374897 13498713
109 894713894 137894 89137489 1374 891348314 12 17 9000000 . 17))
110
111 (test '(',,@#''(quote (syntax unquote-splicing . 2) 2)))
112
113 (test '#(1 2 3))
114
115 (test '#(384 7384 83947 893478 9137489 3894789 134789314 79817238
116 97314897 318947138974 981374 89137489 1374897 13498713
117 894713894 137894 89137489))
118
119
120 (define (test-file x)
121 (printf "testing file ~s ...\n" x)
122 (with-input-from-file x
123 (lambda ()
124 (let f ([i 0])
125 (let ([x (read)] [fname (format "tmp.~a.pp" i)])
126 (unless (eof-object? x)
127 (let ([y
128 (begin
129 (call-with-output-file fname
130 (lambda (p)
131 (pretty-print x p))
132 'replace)
133 (with-input-from-file fname read))])
134 (if (equal? x y)
135 (f (fxadd1 i))
136 (die 'test-file "mismatch" x y)))))))))
137