49d1ba8549a94dbbd740c452be3db44260351efc
[sbcl.git] / src / code / backq.lisp
1 ;;;; the backquote reader macro
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!IMPL")
13
14 (/show0 "entering backq.lisp")
15
16 ;;; The flags passed back by BACKQUOTIFY can be interpreted as follows:
17 ;;;
18 ;;;   |`,|: [a] => a
19 ;;;    NIL: [a] => a            ;the NIL flag is used only when a is NIL
20 ;;;      T: [a] => a            ;the T flag is used when a is self-evaluating
21 ;;;  QUOTE: [a] => (QUOTE a)
22 ;;; APPEND: [a] => (APPEND . a)
23 ;;;  NCONC: [a] => (NCONC . a)
24 ;;;   LIST: [a] => (LIST . a)
25 ;;;  LIST*: [a] => (LIST* . a)
26 ;;;
27 ;;; The flags are combined according to the following set of rules:
28 ;;;  ([a] means that a should be converted according to the previous table)
29 ;;;
30 ;;;   \ car  ||    otherwise    |    QUOTE or     |     |`,@|      |     |`,.|
31 ;;;cdr \     ||                 |    T or NIL     |                |
32 ;;;================================================================================
33 ;;;  |`,|    || LIST* ([a] [d]) | LIST* ([a] [d]) | APPEND (a [d]) | NCONC  (a [d])
34 ;;;  NIL     || LIST    ([a])   | QUOTE    (a)    | <hair>    a    | <hair>    a
35 ;;;QUOTE or T|| LIST* ([a] [d]) | QUOTE  (a . d)  | APPEND (a [d]) | NCONC (a [d])
36 ;;; APPEND   || LIST* ([a] [d]) | LIST* ([a] [d]) | APPEND (a . d) | NCONC (a [d])
37 ;;; NCONC    || LIST* ([a] [d]) | LIST* ([a] [d]) | APPEND (a [d]) | NCONC (a . d)
38 ;;;  LIST    || LIST  ([a] . d) | LIST  ([a] . d) | APPEND (a [d]) | NCONC (a [d])
39 ;;;  LIST*   || LIST* ([a] . d) | LIST* ([a] . d) | APPEND (a [d]) | NCONC  (a [d])
40 ;;;
41 ;;;<hair> involves starting over again pretending you had read ".,a)" instead
42 ;;; of ",@a)"
43
44 (defvar *backquote-count* 0 #!+sb-doc "how deep we are into backquotes")
45 (defvar *bq-comma-flag* '(|,|))
46 (defvar *bq-at-flag* '(|,@|))
47 (defvar *bq-dot-flag* '(|,.|))
48 (defvar *bq-vector-flag* '(|bqv|))
49
50 (/show0 "backq.lisp 50")
51
52 ;;; the actual character macro
53 (defun backquote-macro (stream ignore)
54   (declare (ignore ignore))
55   (let ((*backquote-count* (1+ *backquote-count*)))
56     (multiple-value-bind (flag thing)
57         (backquotify stream (read stream t nil t))
58       (when (eq flag *bq-at-flag*)
59         (%reader-error stream ",@ after backquote in ~S" thing))
60       (when (eq flag *bq-dot-flag*)
61         (%reader-error stream ",. after backquote in ~S" thing))
62       (backquotify-1 flag thing))))
63
64 (/show0 "backq.lisp 64")
65
66 (defun comma-macro (stream ignore)
67   (declare (ignore ignore))
68   (unless (> *backquote-count* 0)
69     (when *read-suppress*
70       (return-from comma-macro nil))
71     (%reader-error stream "comma not inside a backquote"))
72   (let ((c (read-char stream))
73         (*backquote-count* (1- *backquote-count*)))
74     (cond ((char= c #\@)
75            (cons *bq-at-flag* (read stream t nil t)))
76           ((char= c #\.)
77            (cons *bq-dot-flag* (read stream t nil t)))
78           (t (unread-char c stream)
79              (cons *bq-comma-flag* (read stream t nil t))))))
80
81 (/show0 "backq.lisp 83")
82
83 ;;;
84 (defun expandable-backq-expression-p (object)
85   (and (consp object)
86        (let ((flag (car object)))
87          (or (eq flag *bq-at-flag*)
88              (eq flag *bq-dot-flag*)))))
89
90 ;;; This does the expansion from table 2.
91 (defun backquotify (stream code)
92   (cond ((atom code)
93          (cond ((null code) (values nil nil))
94                ((or (consp code)
95                     (symbolp code))
96                 ;; Keywords are self-evaluating. Install after packages.
97                 (values 'quote code))
98                (t (values t code))))
99         ((or (eq (car code) *bq-at-flag*)
100              (eq (car code) *bq-dot-flag*))
101          (values (car code) (cdr code)))
102         ((eq (car code) *bq-comma-flag*)
103          (comma (cdr code)))
104         ((eq (car code) *bq-vector-flag*)
105          (multiple-value-bind (dflag d) (backquotify stream (cdr code))
106            (values 'vector (backquotify-1 dflag d))))
107         (t (multiple-value-bind (aflag a) (backquotify stream (car code))
108              (multiple-value-bind (dflag d) (backquotify stream (cdr code))
109                (when (eq dflag *bq-at-flag*)
110                  ;; Get the errors later.
111                  (%reader-error stream ",@ after dot in ~S" code))
112                (when (eq dflag *bq-dot-flag*)
113                  (%reader-error stream ",. after dot in ~S" code))
114                (cond
115                 ((eq aflag *bq-at-flag*)
116                  (if (null dflag)
117                      (if (expandable-backq-expression-p a)
118                          (values 'append (list a))
119                          (comma a))
120                      (values 'append
121                              (cond ((eq dflag 'append)
122                                     (cons a d ))
123                                    (t (list a (backquotify-1 dflag d)))))))
124                 ((eq aflag *bq-dot-flag*)
125                  (if (null dflag)
126                      (if (expandable-backq-expression-p a)
127                          (values 'nconc (list a))
128                          (comma a))
129                      (values 'nconc
130                              (cond ((eq dflag 'nconc)
131                                     (cons a d))
132                                    (t (list a (backquotify-1 dflag d)))))))
133                 ((null dflag)
134                  (if (member aflag '(quote t nil))
135                      (values 'quote (list a))
136                      (values 'list (list (backquotify-1 aflag a)))))
137                 ((member dflag '(quote t))
138                  (if (member aflag '(quote t nil))
139                      (values 'quote (cons a d ))
140                      (values 'list* (list (backquotify-1 aflag a)
141                                           (backquotify-1 dflag d)))))
142                 (t (setq a (backquotify-1 aflag a))
143                    (if (member dflag '(list list*))
144                        (values dflag (cons a d))
145                        (values 'list*
146                                (list a (backquotify-1 dflag d)))))))))))
147
148 (/show0 "backq.lisp 139")
149
150 ;;; This handles the <hair> cases.
151 (defun comma (code)
152   (cond ((atom code)
153          (cond ((null code)
154                 (values nil nil))
155                ((or (numberp code) (eq code t))
156                 (values t code))
157                (t (values *bq-comma-flag* code))))
158         ((and (eq (car code) 'quote)
159               (not (expandable-backq-expression-p (cadr code))))
160          (values (car code) (cadr code)))
161         ((member (car code) '(append list list* nconc))
162          (values (car code) (cdr code)))
163         ((eq (car code) 'cons)
164          (values 'list* (cdr code)))
165         (t (values *bq-comma-flag* code))))
166
167 (/show0 "backq.lisp 157")
168
169 ;;; This handles table 1.
170 (defun backquotify-1 (flag thing)
171   (cond ((or (eq flag *bq-comma-flag*)
172              (member flag '(t nil)))
173          thing)
174         ((eq flag 'quote)
175          (list  'quote thing))
176         ((eq flag 'list*)
177          (cond ((and (null (cddr thing))
178                      (not (expandable-backq-expression-p (cadr thing))))
179                 (cons 'backq-cons thing))
180                ((expandable-backq-expression-p (car (last thing)))
181                 (list 'backq-append
182                       (cons 'backq-list (butlast thing))
183                       ;; Can it be optimized further? -- APD, 2001-12-21
184                       (car (last thing))))
185                (t
186                 (cons 'backq-list* thing))))
187         ((eq flag 'vector)
188          (list 'backq-vector thing))
189         (t (cons (ecase flag
190                    ((list) 'backq-list)
191                    ((append) 'backq-append)
192                    ((nconc) 'backq-nconc))
193                  thing))))
194 \f
195 ;;;; magic BACKQ- versions of builtin functions
196
197 (/show0 "backq.lisp 184")
198
199 ;;; Define synonyms for the lisp functions we use, so that by using
200 ;;; them, the backquoted material will be recognizable to the
201 ;;; pretty-printer.
202 (macrolet ((def (b-name name)
203              (let ((args (gensym "ARGS")))
204                ;; FIXME: This function should be INLINE so that the lists
205                ;; aren't consed twice, but I ran into an optimizer bug the
206                ;; first time I tried to make this work for BACKQ-LIST. See
207                ;; whether there's still an optimizer bug, and fix it if so, and
208                ;; then make these INLINE.
209                `(defun ,b-name (&rest ,args)
210                   (apply #',name ,args)))))
211   (def backq-list list)
212   (def backq-list* list*)
213   (def backq-append append)
214   (def backq-nconc nconc)
215   (def backq-cons cons))
216
217 (/show0 "backq.lisp 204")
218
219 (defun backq-vector (list)
220   (declare (list list))
221   (coerce list 'simple-vector))
222 \f
223 ;;;; initialization
224
225 (/show0 "backq.lisp 212")
226
227 ;;; Install BACKQ stuff in the current *READTABLE*.
228 ;;;
229 ;;; In the target Lisp, we have to wait to do this until the readtable
230 ;;; has been created. In the cross-compilation host Lisp, we can do
231 ;;; this right away. (You may ask: In the cross-compilation host,
232 ;;; which already has its own implementation of the backquote
233 ;;; readmacro, why do we do this at all? Because the cross-compilation
234 ;;; host might -- as SBCL itself does -- express the backquote
235 ;;; expansion in terms of internal, nonportable functions. By
236 ;;; redefining backquote in terms of functions which are guaranteed to
237 ;;; exist on the target Lisp, we ensure that backquote expansions in
238 ;;; code-generating code work properly.)
239 (defun !backq-cold-init ()
240   (set-macro-character #\` #'backquote-macro)
241   (set-macro-character #\, #'comma-macro))
242 #+sb-xc-host (!backq-cold-init)
243
244 ;;; The pretty-printer needs to know about our special tokens
245 (defvar *backq-tokens*
246   '(backq-comma backq-comma-at backq-comma-dot backq-list
247     backq-list* backq-append backq-nconc backq-cons backq-vector))
248
249 ;;; Since our backquote is installed on the host lisp, and since
250 ;;; developers make mistakes with backquotes and commas too, let's
251 ;;; ensure that we can report errors rather than get an undefined
252 ;;; function condition on %READER-ERROR.
253 #+sb-xc-host ; proper definition happens for the target
254 (defun %reader-error (stream format-string &rest format-args)
255   (bug "READER-ERROR on stream ~S: ~?" stream format-string format-args))
256
257 (/show0 "done with backq.lisp")