4c13b437b737705d1fc8b99ba2babb2e890a3086
[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       (if (eq flag *bq-at-flag*)
59           (%reader-error stream ",@ after backquote in ~S" thing))
60       (if (eq flag *bq-dot-flag*)
61           (%reader-error stream ",. after backquote in ~S" thing))
62       (values (backquotify-1 flag thing) 'list))))
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     (values
75      (cond ((char= c #\@)
76             (cons *bq-at-flag* (read stream t nil t)))
77            ((char= c #\.)
78             (cons *bq-dot-flag* (read stream t nil t)))
79            (t (unread-char c stream)
80               (cons *bq-comma-flag* (read stream t nil t))))
81      'list)))
82
83 (/show0 "backq.lisp 83")
84
85 ;;;
86 (defun expandable-backq-expression-p (object)
87   (and (consp object)
88        (let ((flag (car object)))
89          (or (eq flag *bq-at-flag*)
90              (eq flag *bq-dot-flag*)))))
91
92 ;;; This does the expansion from table 2.
93 (defun backquotify (stream code)
94   (cond ((atom code)
95          (cond ((null code) (values nil nil))
96                ((or (consp code)
97                     (symbolp code))
98                 ;; Keywords are self-evaluating. Install after packages.
99                 (values 'quote code))
100                (t (values t code))))
101         ((or (eq (car code) *bq-at-flag*)
102              (eq (car code) *bq-dot-flag*))
103          (values (car code) (cdr code)))
104         ((eq (car code) *bq-comma-flag*)
105          (comma (cdr code)))
106         ((eq (car code) *bq-vector-flag*)
107          (multiple-value-bind (dflag d) (backquotify stream (cdr code))
108            (values 'vector (backquotify-1 dflag d))))
109         (t (multiple-value-bind (aflag a) (backquotify stream (car code))
110              (multiple-value-bind (dflag d) (backquotify stream (cdr code))
111                (if (eq dflag *bq-at-flag*)
112                    ;; Get the errors later.
113                    (%reader-error stream ",@ after dot in ~S" code))
114                (if (eq dflag *bq-dot-flag*)
115                    (%reader-error stream ",. after dot in ~S" code))
116                (cond
117                 ((eq aflag *bq-at-flag*)
118                  (if (null dflag)
119                      (if (expandable-backq-expression-p a)
120                          (values 'append (list a))
121                          (comma a))
122                      (values 'append
123                              (cond ((eq dflag 'append)
124                                     (cons a d ))
125                                    (t (list a (backquotify-1 dflag d)))))))
126                 ((eq aflag *bq-dot-flag*)
127                  (if (null dflag)
128                      (if (expandable-backq-expression-p a)
129                          (values 'nconc (list a))
130                          (comma a))
131                      (values 'nconc
132                              (cond ((eq dflag 'nconc)
133                                     (cons a d))
134                                    (t (list a (backquotify-1 dflag d)))))))
135                 ((null dflag)
136                  (if (member aflag '(quote t nil))
137                      (values 'quote (list a))
138                      (values 'list (list (backquotify-1 aflag a)))))
139                 ((member dflag '(quote t))
140                  (if (member aflag '(quote t nil))
141                      (values 'quote (cons a d ))
142                      (values 'list* (list (backquotify-1 aflag a)
143                                           (backquotify-1 dflag d)))))
144                 (t (setq a (backquotify-1 aflag a))
145                    (if (member dflag '(list list*))
146                        (values dflag (cons a d))
147                        (values 'list*
148                                (list a (backquotify-1 dflag d)))))))))))
149
150 (/show0 "backq.lisp 139")
151
152 ;;; This handles the <hair> cases.
153 (defun comma (code)
154   (cond ((atom code)
155          (cond ((null code)
156                 (values nil nil))
157                ((or (numberp code) (eq code t))
158                 (values t code))
159                (t (values *bq-comma-flag* code))))
160         ((and (eq (car code) 'quote)
161               (not (expandable-backq-expression-p (cadr code))))
162          (values (car code) (cadr code)))
163         ((member (car code) '(append list list* nconc))
164          (values (car code) (cdr code)))
165         ((eq (car code) 'cons)
166          (values 'list* (cdr code)))
167         (t (values *bq-comma-flag* code))))
168
169 (/show0 "backq.lisp 157")
170
171 ;;; This handles table 1.
172 (defun backquotify-1 (flag thing)
173   (cond ((or (eq flag *bq-comma-flag*)
174              (member flag '(t nil)))
175          thing)
176         ((eq flag 'quote)
177          (list  'quote thing))
178         ((eq flag 'list*)
179          (cond ((and (null (cddr thing))
180                      (not (expandable-backq-expression-p (cadr thing))))
181                 (cons 'backq-cons thing))
182                ((expandable-backq-expression-p (car (last thing)))
183                 (list 'backq-append
184                       (cons 'backq-list (butlast thing))
185                       ;; Can it be optimized further? -- APD, 2001-12-21
186                       (car (last thing))))
187                (t
188                 (cons 'backq-list* thing))))
189         ((eq flag 'vector)
190          (list 'backq-vector thing))
191         (t (cons (cdr
192                   (assoc flag
193                          '((cons . backq-cons)
194                            (list . backq-list)
195                            (append . backq-append)
196                            (nconc . backq-nconc))
197                          :test #'equal))
198                  thing))))
199 \f
200 ;;;; magic BACKQ- versions of builtin functions
201
202 (/show0 "backq.lisp 184")
203
204 ;;; Define synonyms for the lisp functions we use, so that by using
205 ;;; them, the backquoted material will be recognizable to the
206 ;;; pretty-printer.
207 (macrolet ((def-frob (b-name name)
208              (let ((args (gensym "ARGS")))
209                ;; FIXME: This function should be INLINE so that the lists
210                ;; aren't consed twice, but I ran into an optimizer bug the
211                ;; first time I tried to make this work for BACKQ-LIST. See
212                ;; whether there's still an optimizer bug, and fix it if so, and
213                ;; then make these INLINE.
214                `(defun ,b-name (&rest ,args)
215                   (apply #',name ,args)))))
216   (def-frob backq-list list)
217   (def-frob backq-list* list*)
218   (def-frob backq-append append)
219   (def-frob backq-nconc nconc)
220   (def-frob backq-cons cons))
221
222 (/show0 "backq.lisp 204")
223
224 (defun backq-vector (list)
225   (declare (list list))
226   (coerce list 'simple-vector))
227 \f
228 ;;;; initialization
229
230 (/show0 "backq.lisp 212")
231
232 ;;; Install BACKQ stuff in the current *READTABLE*.
233 ;;;
234 ;;; In the target Lisp, we have to wait to do this until the readtable
235 ;;; has been created. In the cross-compilation host Lisp, we can do
236 ;;; this right away. (You may ask: In the cross-compilation host,
237 ;;; which already has its own implementation of the backquote
238 ;;; readmacro, why do we do this at all? Because the cross-compilation
239 ;;; host might -- as SBCL itself does -- express the backquote
240 ;;; expansion in terms of internal, nonportable functions. By
241 ;;; redefining backquote in terms of functions which are guaranteed to
242 ;;; exist on the target Lisp, we ensure that backquote expansions in
243 ;;; code-generating code work properly.)
244 (defun !backq-cold-init ()
245   (set-macro-character #\` #'backquote-macro)
246   (set-macro-character #\, #'comma-macro))
247 #+sb-xc-host (!backq-cold-init)
248
249 (/show0 "done with backq.lisp")