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