1 ;;;; pretty-printing of backquote expansions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 (defun backq-unparse-expr (form splicing)
19 `((backq-comma-at ,form)))
21 `((backq-comma-dot ,form)))
24 (defun backq-unparse (form &optional splicing)
26 "Given a lisp form containing the magic functions BACKQ-LIST, BACKQ-LIST*,
27 BACKQ-APPEND, etc. produced by the backquote reader macro, will return a
28 corresponding backquote input form. In this form, `,' `,@' and `,.' are
29 represented by lists whose cars are BACKQ-COMMA, BACKQ-COMMA-AT, and
30 BACKQ-COMMA-DOT respectively, and whose cadrs are the form after the comma.
31 SPLICING indicates whether a comma-escape return should be modified for
32 splicing with other forms: a value of T or :NCONC meaning that an extra
33 level of parentheses should be added."
36 (backq-unparse-expr form splicing))
37 ((not (null (cdr (last form))))
38 ;; FIXME: this probably throws a recursive error
39 (bug "found illegal dotted backquote form: ~S" form))
43 (mapcar #'backq-unparse (cdr form)))
45 (do ((tail (cdr form) (cdr tail))
48 (nconc (nreverse accum)
49 (backq-unparse (car tail) t)))
50 (push (backq-unparse (car tail)) accum)))
53 (mapcar (lambda (el) (backq-unparse el t))
57 (mapcar (lambda (el) (backq-unparse el :nconc))
60 (cons (backq-unparse (cadr form) nil)
61 (backq-unparse (caddr form) t)))
63 (coerce (backq-unparse (cadr form)) 'vector))
66 ((atom (cadr form)) (cadr form))
67 ((and (consp (cadr form))
68 (member (caadr form) *backq-tokens*))
69 (backq-unparse-expr form splicing))
70 (t (cons (backq-unparse `(quote ,(caadr form)))
71 (backq-unparse `(quote ,(cdadr form)))))))
73 (backq-unparse-expr form splicing))))))
75 (defun pprint-backquote (stream form &rest noise)
76 (declare (ignore noise))
77 (write-char #\` stream)
78 (write (backq-unparse form) :stream stream))
80 (defun pprint-backq-comma (stream form &rest noise)
81 (declare (ignore noise))
84 (write-char #\, stream))
86 (write-string ",@" stream))
88 (write-string ",." stream)))
89 ;; Ha! an example of where the per-process specials for stream
90 ;; attributes rather than per-stream actually makes life easier.
91 ;; Since all of the attributes are shared in the dynamic state, we
92 ;; can do... -- CSR, 2003-09-30
94 ;; [...] above referred to the trick of printing to a string stream,
95 ;; and then simply printing the resulting sequence to the pretty
96 ;; stream, possibly with a space prepended. However, this doesn't
97 ;; work for pretty streams which need to do margin calculations. Oh
98 ;; well. It was good while it lasted. -- CSR, 2003-12-15
100 ;; This is an evil hack. If we print to a string and then print again,
101 ;; the circularity detection logic behaves as though it's already
102 ;; printed that data... and it has, to a string stream that we send
103 ;; to the bitbucket in the sky. -- PK, 2013-03-30
104 (when (eql (car form) 'backq-comma)
105 (let ((output (with-output-to-string (s)
106 ;; Patching evil with more evil. The next step is
107 ;; likely to stop the madness and unconditionally
109 (let (*circularity-hash-table*
110 *circularity-counter*)
111 (write (cadr form) :stream s)))))
112 (when (and (plusp (length output))
113 (or (char= (char output 0) #\.)
114 (char= (char output 0) #\@)))
115 (write-char #\Space stream))))
116 (write (cadr form) :stream stream))
118 ;;; This is called by !PPRINT-COLD-INIT, fairly late, because
119 ;;; SET-PPRINT-DISPATCH doesn't work until the compiler works.
121 ;;; FIXME: It might be cleaner to just make these be toplevel forms and
122 ;;; enforce the delay by putting this file late in the build sequence.
123 (defun !backq-pp-cold-init ()
124 (set-pprint-dispatch '(cons (eql backq-list)) #'pprint-backquote)
125 (set-pprint-dispatch '(cons (eql backq-list*)) #'pprint-backquote)
126 (set-pprint-dispatch '(cons (eql backq-append)) #'pprint-backquote)
127 (set-pprint-dispatch '(cons (eql backq-nconc)) #'pprint-backquote)
128 (set-pprint-dispatch '(cons (eql backq-cons)) #'pprint-backquote)
129 (set-pprint-dispatch '(cons (eql backq-vector)) #'pprint-backquote)
131 (set-pprint-dispatch '(cons (eql backq-comma)) #'pprint-backq-comma)
132 (set-pprint-dispatch '(cons (eql backq-comma-at)) #'pprint-backq-comma)
133 (set-pprint-dispatch '(cons (eql backq-comma-dot)) #'pprint-backq-comma))