/[cmucl]/src/code/backq.lisp
ViewVC logotype

Contents of /src/code/backq.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Wed Feb 12 01:45:44 1992 UTC (22 years, 2 months ago) by ram
Branch: MAIN
Changes since 1.3: +11 -11 lines
Changed to use %READER-ERROR.
1 ;;; -*- Log: code.log; Mode: Lisp; Package: Lisp -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7 ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8 ;;;
9 (ext:file-comment
10 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/backq.lisp,v 1.4 1992/02/12 01:45:44 ram Exp $")
11 ;;;
12 ;;; **********************************************************************
13 ;;;
14 ;;; BACKQUOTE: Code Spice Lispified by Lee Schumacher.
15 ;;; (unparsing by Miles Bader)
16 ;;;
17 (in-package 'lisp)
18
19
20 ;;; The flags passed back by BACKQUOTIFY can be interpreted as follows:
21 ;;;
22 ;;; |`,|: [a] => a
23 ;;; NIL: [a] => a ;the NIL flag is used only when a is NIL
24 ;;; T: [a] => a ;the T flag is used when a is self-evaluating
25 ;;; QUOTE: [a] => (QUOTE a)
26 ;;; APPEND: [a] => (APPEND . a)
27 ;;; NCONC: [a] => (NCONC . a)
28 ;;; LIST: [a] => (LIST . a)
29 ;;; LIST*: [a] => (LIST* . a)
30 ;;;
31 ;;; The flags are combined according to the following set of rules:
32 ;;; ([a] means that a should be converted according to the previous table)
33 ;;;
34 ;;; \ car || otherwise | QUOTE or | |`,@| | |`,.|
35 ;;;cdr \ || | T or NIL | |
36 ;;;================================================================================
37 ;;; |`,| || LIST* ([a] [d]) | LIST* ([a] [d]) | APPEND (a [d]) | NCONC (a [d])
38 ;;; NIL || LIST ([a]) | QUOTE (a) | <hair> a | <hair> a
39 ;;;QUOTE or T|| LIST* ([a] [d]) | QUOTE (a . d) | APPEND (a [d]) | NCONC (a [d])
40 ;;; APPEND || LIST* ([a] [d]) | LIST* ([a] [d]) | APPEND (a . d) | NCONC (a [d])
41 ;;; NCONC || LIST* ([a] [d]) | LIST* ([a] [d]) | APPEND (a [d]) | NCONC (a . d)
42 ;;; LIST || LIST ([a] . d) | LIST ([a] . d) | APPEND (a [d]) | NCONC (a [d])
43 ;;; LIST* || LIST* ([a] . d) | LIST* ([a] . d) | APPEND (a [d]) | NCONC (a [d])
44 ;;;
45 ;;;<hair> involves starting over again pretending you had read ".,a)" instead
46 ;;; of ",@a)"
47
48 (defvar *backquote-count* 0 "How deep we are into backquotes")
49 (defvar *bq-comma-flag* '(|,|))
50 (defvar *bq-at-flag* '(|,@|))
51 (defvar *bq-dot-flag* '(|,.|))
52 (defvar *bq-vector-flag* '(|bqv|))
53
54 ;; This is the actual character macro.
55 (defun backquote-macro (stream ignore)
56 (declare (ignore ignore))
57 (let ((*backquote-count* (1+ *backquote-count*)))
58 (multiple-value-bind (flag thing)
59 (backquotify stream (read stream t nil t))
60 (if (eq flag *bq-at-flag*)
61 (%reader-error stream ",@ after backquote in ~S" thing))
62 (if (eq flag *bq-dot-flag*)
63 (%reader-error stream ",. after backquote in ~S" thing))
64 (values (backquotify-1 flag thing) 'list))))
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 ;;; This does the expansion from table 2.
84 (defun backquotify (stream code)
85 (cond ((atom code)
86 (cond ((null code) (values nil nil))
87 ((or (numberp code)
88 (eq code t))
89 ;; Keywords are self evaluating. Install after packages.
90 (values t code))
91 (t (values 'quote code))))
92 ((or (eq (car code) *bq-at-flag*)
93 (eq (car code) *bq-dot-flag*))
94 (values (car code) (cdr code)))
95 ((eq (car code) *bq-comma-flag*)
96 (comma (cdr code)))
97 ((eq (car code) *bq-vector-flag*)
98 (multiple-value-bind (dflag d) (backquotify stream (cdr code))
99 (values 'vector (backquotify-1 dflag d))))
100 (t (multiple-value-bind (aflag a) (backquotify stream (car code))
101 (multiple-value-bind (dflag d) (backquotify stream (cdr code))
102 (if (eq dflag *bq-at-flag*)
103 ;; get the errors later.
104 (%reader-error stream ",@ after dot in ~S" code))
105 (if (eq dflag *bq-dot-flag*)
106 (%reader-error stream ",. after dot in ~S" code))
107 (cond
108 ((eq aflag *bq-at-flag*)
109 (if (null dflag)
110 (comma a)
111 (values 'append
112 (cond ((eq dflag 'append)
113 (cons a d ))
114 (t (list a (backquotify-1 dflag d)))))))
115 ((eq aflag *bq-dot-flag*)
116 (if (null dflag)
117 (comma a)
118 (values 'nconc
119 (cond ((eq dflag 'nconc)
120 (cons a d))
121 (t (list a (backquotify-1 dflag d)))))))
122 ((null dflag)
123 (if (memq aflag '(quote t nil))
124 (values 'quote (list a))
125 (values 'list (list (backquotify-1 aflag a)))))
126 ((memq dflag '(quote t))
127 (if (memq aflag '(quote t nil))
128 (values 'quote (cons a d ))
129 (values 'list* (list (backquotify-1 aflag a)
130 (backquotify-1 dflag d)))))
131 (t (setq a (backquotify-1 aflag a))
132 (if (memq dflag '(list list*))
133 (values dflag (cons a d))
134 (values 'list*
135 (list a (backquotify-1 dflag d)))))))))))
136
137 ;;; This handles the <hair> cases
138 (defun comma (code)
139 (cond ((atom code)
140 (cond ((null code)
141 (values nil nil))
142 ((or (numberp code) (eq code 't))
143 (values t code))
144 (t (values *bq-comma-flag* code))))
145 ((eq (car code) 'quote)
146 (values (car code) (cadr code)))
147 ((memq (car code) '(append list list* nconc))
148 (values (car code) (cdr code)))
149 ((eq (car code) 'cons)
150 (values 'list* (cdr code)))
151 (t (values *bq-comma-flag* code))))
152
153 ;;; This handles table 1.
154 (defun backquotify-1 (flag thing)
155 (cond ((or (eq flag *bq-comma-flag*)
156 (memq flag '(t nil)))
157 thing)
158 ((eq flag 'quote)
159 (list 'quote thing))
160 ((eq flag 'list*)
161 (cond ((null (cddr thing))
162 (cons 'backq-cons thing))
163 (t
164 (cons 'backq-list* thing))))
165 ((eq flag 'vector)
166 (list 'backq-vector thing))
167 (t (cons (cdr
168 (assq flag
169 '((cons . backq-cons)
170 (list . backq-list)
171 (append . backq-append)
172 (nconc . backq-nconc))))
173 thing))))
174
175
176 ;;;; Magic backq- versions of builtin functions.
177
178 ;;; Use synonyms for the lisp functions we use, so we can recognize backquoted
179 ;;; material when pretty-printing
180
181 (defun backq-list (&rest args)
182 args)
183 (defun backq-list* (&rest args)
184 (apply #'list* args))
185 (defun backq-append (&rest args)
186 (apply #'append args))
187 (defun backq-nconc (&rest args)
188 (apply #'nconc args))
189 (defun backq-cons (x y)
190 (cons x y))
191 (defun backq-vector (list)
192 (coerce list 'vector))
193
194
195 ;;;; Unparsing
196
197 (defun backq-unparse-expr (form splicing)
198 (ecase splicing
199 ((nil)
200 `(backq-comma ,form))
201 ((t)
202 `((backq-comma-at ,form)))
203 (:nconc
204 `((backq-comma-dot ,form)))
205 ))
206
207 (defun backq-unparse (form &optional splicing)
208 "Given a lisp form containing the magic functions BACKQ-LIST, BACKQ-LIST*,
209 BACKQ-APPEND, etc. produced by the backquote reader macro, will return a
210 corresponding backquote input form. In this form, `,' `,@' and `,.' are
211 represented by lists whose cars are BACKQ-COMMA, BACKQ-COMMA-AT, and
212 BACKQ-COMMA-DOT respectively, and whose cadrs are the form after the comma.
213 SPLICING indicates whether a comma-escape return should be modified for
214 splicing with other forms: a value of T or :NCONC meaning that an extra
215 level of parentheses should be added."
216 (if (atom form)
217 (backq-unparse-expr form splicing)
218 (case (car form)
219 (backq-list
220 (mapcar #'backq-unparse (cdr form)))
221 (backq-list*
222 (do ((tail (cdr form) (cdr tail))
223 (accum nil))
224 ((null (cdr tail))
225 (nconc (nreverse accum)
226 (backq-unparse (car tail) t)))
227 (push (backq-unparse (car tail)) accum)))
228 (backq-append
229 (mapcan #'(lambda (el) (backq-unparse el t))
230 (cdr form)))
231 (backq-nconc
232 (mapcan #'(lambda (el) (backq-unparse el :nconc))
233 (cdr form)))
234 (backq-cons
235 (cons (backq-unparse (cadr form) nil)
236 (backq-unparse (caddr form) t)))
237 (backq-vector
238 (coerce (backq-unparse (cadr form)) 'vector))
239 (quote
240 (cadr form))
241 (t
242 (backq-unparse-expr form splicing)))))
243
244 (defun pprint-backquote (stream form &rest noise)
245 (declare (ignore noise))
246 (write-char #\` stream)
247 (write (backq-unparse form) :stream stream))
248
249 (defun pprint-backq-comma (stream form &rest noise)
250 (declare (ignore noise))
251 (ecase (car form)
252 (backq-comma
253 (write-char #\, stream))
254 (backq-comma-at
255 (princ ",@" stream))
256 (backq-comma-dot
257 (princ ",." stream)))
258 (write (cadr form) :stream stream))
259
260
261 ;;;; BACKQ-INIT and BACKQ-PP-INIT
262
263 ;;; BACKQ-INIT -- interface.
264 ;;;
265 ;;; This is called by %INITIAL-FUNCTION.
266 ;;;
267 (defun backq-init ()
268 (let ((*readtable* std-lisp-readtable))
269 (set-macro-character #\` #'backquote-macro)
270 (set-macro-character #\, #'comma-macro)))
271
272 ;;; BACKQ-PP-INIT -- interface.
273 ;;;
274 ;;; This is called by PPRINT-INIT. This must be seperate from BACKQ-INIT
275 ;;; because SET-PPRINT-DISPATCH doesn't work until the compiler is loaded.
276 ;;;
277 (defun backq-pp-init ()
278 (set-pprint-dispatch '(cons (eql backq-list)) #'pprint-backquote)
279 (set-pprint-dispatch '(cons (eql backq-list*)) #'pprint-backquote)
280 (set-pprint-dispatch '(cons (eql backq-append)) #'pprint-backquote)
281 (set-pprint-dispatch '(cons (eql backq-nconc)) #'pprint-backquote)
282 (set-pprint-dispatch '(cons (eql backq-cons)) #'pprint-backquote)
283 (set-pprint-dispatch '(cons (eql backq-vector)) #'pprint-backquote)
284
285 (set-pprint-dispatch '(cons (eql backq-comma)) #'pprint-backq-comma)
286 (set-pprint-dispatch '(cons (eql backq-comma-at)) #'pprint-backq-comma)
287 (set-pprint-dispatch '(cons (eql backq-comma-dot)) #'pprint-backq-comma))

  ViewVC Help
Powered by ViewVC 1.1.5