181
|
1 ;; Copyright (C) 1985 Free Software Foundation, Inc.
|
|
2 ;; Written by Dick King (king@kestrel).
|
|
3
|
|
4 ;; This file is part of GNU Emacs.
|
|
5
|
|
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 ;; it under the terms of the GNU General Public License as published by
|
|
8 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
9 ;; any later version.
|
|
10
|
|
11 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 ;; GNU General Public License for more details.
|
|
15
|
|
16 ;; You should have received a copy of the GNU General Public License
|
|
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20
|
|
21 ;;; This is a rudimentry backquote package written by D. King,
|
|
22 ;;; king@kestrel, on 8/31/85. (` x) is a macro
|
|
23 ;;; that expands to a form that produces x. (` (a b ..)) is
|
|
24 ;;; a macro that expands into a form that produces a list of what a b
|
|
25 ;;; etc. would have produced. Any element can be of the form
|
|
26 ;;; (, <form>) in which case the resulting form evaluates
|
|
27 ;;; <form> before putting it into place, or (,@ <form>), in which
|
|
28 ;;; case the evaluation of <form> is arranged for and each element
|
|
29 ;;; of the result (which must be a (possibly null) list) is inserted.
|
|
30 ;;; As an example, the immediately following macro push (v l) could
|
|
31 ;;; have been written
|
|
32 ;;; (defmacro push (v l)
|
|
33 ;;; (` (setq (, l) (cons (,@ (list v l))))))
|
|
34 ;;; although
|
|
35 ;;; (defmacro push (v l)
|
|
36 ;;; (` (setq (, l) (cons (, v) (, l)))))
|
|
37 ;;; is far more natural. The magic atoms ,
|
|
38 ;;; and ,@ are user-settable and list-valued. We recommend that
|
|
39 ;;; things never be removed from this list lest you break something
|
|
40 ;;; someone else wrote in the dim past that comes to be recompiled in
|
|
41 ;;; the distant future.
|
|
42
|
|
43 ;;; LIMITATIONS: tail consing is not handled correctly. Do not say
|
|
44 ;;; (` (a . (, b))) - say (` (a (,@ b)))
|
|
45 ;;; which works even if b is not list-valued.
|
|
46 ;;; No attempt is made to handle vectors. (` [a (, b) c]) doesn't work.
|
|
47 ;;; Sorry, you must say things like
|
|
48 ;;; (` (a (,@ 'b))) to get (a . b) and
|
|
49 ;;; (` ((, ',) c)) to get (, c) - [(` (a , b)) will work but is a bad habit]
|
|
50 ;;; I haven't taught it the joys of nconc.
|
|
51 ;;; (` atom) dies. (` (, atom)) or anything else is okay.
|
|
52
|
|
53 ;;; BEWARE BEWARE BEWARE
|
|
54 ;;; inclusion of (,atom) rather than (, atom) or (,@atom) rather than
|
|
55 ;;; (,@ atom) will result in errors that will show up very late.
|
|
56 ;;; This is so crunchy that I am considering including a check for
|
|
57 ;;; this or changing the syntax to ... ,(<form>). RMS: opinion?
|
|
58
|
|
59
|
|
60 (provide 'backquote)
|
|
61
|
|
62 ;;; a raft of general-purpose macros follows. See the nearest
|
|
63 ;;; Commonlisp manual.
|
|
64 (defmacro bq-push (v l)
|
|
65 "Pushes evaluated first form onto second unevaluated object
|
|
66 a list-value atom"
|
|
67 (list 'setq l (list 'cons v l)))
|
|
68
|
|
69 (defmacro bq-caar (l)
|
|
70 (list 'car (list 'car l)))
|
|
71
|
|
72 (defmacro bq-cadr (l)
|
|
73 (list 'car (list 'cdr l)))
|
|
74
|
|
75 (defmacro bq-cdar (l)
|
|
76 (list 'cdr (list 'car l)))
|
|
77
|
|
78
|
|
79 ;;; These two advertised variables control what characters are used to
|
|
80 ;;; unquote things. I have included , and ,@ as the unquote and
|
|
81 ;;; splice operators, respectively, to give users of MIT CADR machine
|
|
82 ;;; derivitive machines a warm, cosy feeling.
|
|
83
|
|
84 (defconst backquote-unquote '(,)
|
|
85 "*A list of all objects that stimulate unquoting in `. Memq test.")
|
|
86
|
|
87
|
|
88 (defconst backquote-splice '(,@)
|
|
89 "*A list of all objects that stimulate splicing in `. Memq test.")
|
|
90
|
|
91
|
|
92 ;;; This is the interface
|
|
93 (defmacro ` (form)
|
|
94 "(` FORM) is a macro that expands to code to construct FORM.
|
|
95 Note that this is very slow in interpreted code, but fast if you compile.
|
|
96 FORM is one or more nested lists, which are `almost quoted':
|
|
97 They are copied recursively, with non-lists used unchanged in the copy.
|
|
98 (` a b) == (list 'a 'b) constructs a new list with two elements, `a' and `b'.
|
|
99 (` a (b c)) == (list 'a (list 'b 'c)) constructs two nested new lists.
|
|
100
|
|
101 However, certain special lists are not copied. They specify substitution.
|
|
102 Lists that look like (, EXP) are evaluated and the result is substituted.
|
|
103 (` a (, (+ x 5))) == (list 'a (+ x 5))
|
|
104
|
|
105 Elements of the form (,@ EXP) are evaluated and then all the elements
|
|
106 of the result are substituted. This result must be a list; it may
|
|
107 be `nil'.
|
|
108
|
|
109 As an example, a simple macro `push' could be written:
|
|
110 (defmacro push (v l)
|
|
111 (` (setq (, l) (cons (,@ (list v l))))))
|
|
112 or as
|
|
113 (defmacro push (v l)
|
|
114 (` (setq (, l) (cons (, v) (, l)))))
|
|
115
|
|
116 LIMITATIONS: \"dotted lists\" are not allowed in FORM.
|
|
117 The ultimate cdr of each list scanned by ` must be `nil'.
|
|
118 \(This does not apply to constants inside expressions to be substituted.)
|
|
119
|
|
120 Substitution elements are not allowed as the cdr
|
|
121 of a cons cell. For example, (` (A . (, B))) does not work.
|
|
122 Instead, write (` (A (,@ B))).
|
|
123
|
|
124 You cannot construct vectors, only lists. Vectors are treated as
|
|
125 constants.
|
|
126
|
|
127 BEWARE BEWARE BEWARE
|
|
128 Inclusion of (,ATOM) rather than (, ATOM)
|
|
129 or of (,@ATOM) rather than (,@ ATOM)
|
|
130 will result in errors that will show up very late."
|
|
131 (bq-make-maker form))
|
|
132
|
|
133 ;;; We develop the method for building the desired list from
|
|
134 ;;; the end towards the beginning. The contract is that there be a
|
|
135 ;;; variable called state and a list called tailmaker, and that the form
|
|
136 ;;; (cons state tailmaker) deliver the goods. Exception - if the
|
|
137 ;;; state is quote the tailmaker is the form itself.
|
|
138 ;;; This function takes a form and returns what I will call a maker in
|
|
139 ;;; what follows. Evaluating the maker would produce the form,
|
|
140 ;;; properly evaluated according to , and ,@ rules.
|
|
141 ;;; I work backwards - it seemed a lot easier. The reason for this is
|
|
142 ;;; if I'm in some sort of a routine building a maker and I switch
|
|
143 ;;; gears, it seemed to me easier to jump into some other state and
|
|
144 ;;; glue what I've already done to the end, than to to prepare that
|
|
145 ;;; something and go back to put things together.
|
|
146 (defun bq-make-maker (form)
|
|
147 "Given one argument, a `mostly quoted' object, produces a maker.
|
|
148 See backquote.el for details"
|
|
149 (let ((tailmaker (quote nil)) (qc 0) (ec 0) (state nil))
|
|
150 (mapcar 'bq-iterative-list-builder (reverse form))
|
|
151 (and state
|
|
152 (cond ((eq state 'quote)
|
|
153 (list state tailmaker))
|
|
154 ((= (length tailmaker) 1)
|
|
155 (funcall (bq-cadr (assq state bq-singles)) tailmaker))
|
|
156 (t (cons state tailmaker))))))
|
|
157
|
|
158 ;;; There are exceptions - we wouldn't want to call append of one
|
|
159 ;;; argument, for example.
|
|
160 (defconst bq-singles '((quote bq-quotecar)
|
|
161 (append car)
|
|
162 (list bq-make-list)
|
|
163 (cons bq-id)))
|
|
164
|
|
165 (defun bq-id (x) x)
|
|
166
|
|
167 (defun bq-quotecar (x) (list 'quote (car x)))
|
|
168
|
|
169 (defun bq-make-list (x) (cons 'list x))
|
|
170
|
|
171 ;;; fr debugging use only
|
|
172 ;(defun funcalll (a b) (funcall a b))
|
|
173 ;(defun funcalll (a b) (debug nil 'enter state tailmaker a b)
|
|
174 ; (let ((ans (funcall a b))) (debug nil 'leave state tailmaker)
|
|
175 ; ans))
|
|
176
|
|
177 ;;; Given a state/tailmaker pair that already knows how to make a
|
|
178 ;;; partial tail of the desired form, this function knows how to add
|
|
179 ;;; yet another element to the burgening list. There are four cases;
|
|
180 ;;; the next item is an atom (which will certainly be quoted); a
|
|
181 ;;; (, xxx), which will be evaluated and put into the list at the top
|
|
182 ;;; level; a (,@ xxx), which will be evaluated and spliced in, or
|
|
183 ;;; some other list, in which case we first compute the form's maker,
|
|
184 ;;; and then we either launch into the quoted case if the maker's
|
|
185 ;;; top level function is quote, or into the comma case if it isn't.
|
|
186 ;;; The fourth case reduces to one of the other three, so here we have
|
|
187 ;;; a choice of three ways to build tailmaker, and cit turns out we
|
|
188 ;;; use five possible values of state (although someday I'll add
|
|
189 ;;; nconcto the possible values of state).
|
|
190 ;;; This maintains the invariant that (cons state tailmaker) is the
|
|
191 ;;; maker for the elements of the tail we've eaten so far.
|
|
192 (defun bq-iterative-list-builder (form)
|
|
193 "Called by `bq-make-maker'. Adds a new item form to tailmaker,
|
|
194 changing state if need be, so tailmaker and state constitute a recipe
|
|
195 for making the list so far."
|
|
196 (cond ((atom form)
|
|
197 (funcall (bq-cadr (assq state bq-quotefns)) form))
|
|
198 ((memq (car form) backquote-unquote)
|
|
199 (funcall (bq-cadr (assq state bq-evalfns)) (bq-cadr form)))
|
|
200 ((memq (car form) backquote-splice)
|
|
201 (funcall (bq-cadr (assq state bq-splicefns)) (bq-cadr form)))
|
|
202 (t
|
|
203 (let ((newform (bq-make-maker form)))
|
|
204 (if (and (listp newform) (eq (car newform) 'quote))
|
|
205 (funcall (bq-cadr (assq state bq-quotefns)) (bq-cadr newform))
|
|
206 (funcall (bq-cadr (assq state bq-evalfns)) newform))))
|
|
207 ))
|
|
208
|
|
209 ;;; We do a 2-d branch on the form of splicing and the old state.
|
|
210 ;;; Here's fifteen functions' names...
|
|
211 (defconst bq-splicefns '((nil bq-splicenil)
|
|
212 (append bq-spliceappend)
|
|
213 (list bq-splicelist)
|
|
214 (quote bq-splicequote)
|
|
215 (cons bq-splicecons)))
|
|
216
|
|
217 (defconst bq-evalfns '((nil bq-evalnil)
|
|
218 (append bq-evalappend)
|
|
219 (list bq-evallist)
|
|
220 (quote bq-evalquote)
|
|
221 (cons bq-evalcons)))
|
|
222
|
|
223 (defconst bq-quotefns '((nil bq-quotenil)
|
|
224 (append bq-quoteappend)
|
|
225 (list bq-quotelist)
|
|
226 (quote bq-quotequote)
|
|
227 (cons bq-quotecons)))
|
|
228
|
|
229 ;;; The name of each function is
|
|
230 ;;; (concat 'bq- <type-of-element-addition> <old-state>)
|
|
231 ;;; I'll comment the non-obvious ones before the definitions...
|
|
232 ;;; In what follows, uppercase letters and form will always be
|
|
233 ;;; metavariables that don't need commas in backquotes, and I will
|
|
234 ;;; assume the existence of something like matches that takes a
|
|
235 ;;; backquote-like form and a value, binds metavariables and returns
|
|
236 ;;; t if the pattern match is successful, returns nil otherwise. I
|
|
237 ;;; will write such a goodie someday.
|
|
238
|
|
239 ;;; (setq tailmaker
|
|
240 ;;; (if (matches ((quote X) Y) tailmaker)
|
|
241 ;;; (` ((quote (form X)) Y))
|
|
242 ;;; (` ((list form (quote X)) Y))))
|
|
243 ;;; (setq state 'append)
|
|
244 (defun bq-quotecons (form)
|
|
245 (if (and (listp (car tailmaker))
|
|
246 (eq (bq-caar tailmaker) 'quote))
|
|
247 (setq tailmaker
|
|
248 (list (list 'quote (list form (bq-cadr (car tailmaker))))
|
|
249 (bq-cadr tailmaker)))
|
|
250 (setq tailmaker
|
|
251 (list (list 'list
|
|
252 (list 'quote form)
|
|
253 (car tailmaker))
|
|
254 (bq-cadr tailmaker))))
|
|
255 (setq state 'append))
|
|
256
|
|
257 (defun bq-quotequote (form)
|
|
258 (bq-push form tailmaker))
|
|
259
|
|
260 ;;; Could be improved to convert (list 'a 'b 'c .. 'w x)
|
|
261 ;;; to (append '(a b c .. w) x)
|
|
262 ;;; when there are enough elements
|
|
263 (defun bq-quotelist (form)
|
|
264 (bq-push (list 'quote form) tailmaker))
|
|
265
|
|
266 ;;; (setq tailmaker
|
|
267 ;;; (if (matches ((quote X) (,@ Y)))
|
|
268 ;;; (` ((quote (, (cons form X))) (,@ Y)))))
|
|
269 (defun bq-quoteappend (form)
|
|
270 (cond ((and (listp tailmaker)
|
|
271 (listp (car tailmaker))
|
|
272 (eq (bq-caar tailmaker) 'quote))
|
|
273 (rplaca (bq-cdar tailmaker)
|
|
274 (cons form (car (bq-cdar tailmaker)))))
|
|
275 (t (bq-push (list 'quote (list form)) tailmaker))))
|
|
276
|
|
277 (defun bq-quotenil (form)
|
|
278 (setq tailmaker (list form))
|
|
279 (setq state 'quote))
|
|
280
|
|
281 ;;; (if (matches (X Y) tailmaker) ; it must
|
|
282 ;;; (` ((list form X) Y)))
|
|
283 (defun bq-evalcons (form)
|
|
284 (setq tailmaker
|
|
285 (list (list 'list form (car tailmaker))
|
|
286 (bq-cadr tailmaker)))
|
|
287 (setq state 'append))
|
|
288
|
|
289 ;;; (if (matches (X Y Z (,@ W)))
|
|
290 ;;; (progn (setq state 'append)
|
|
291 ;;; (` ((list form) (quote (X Y Z (,@ W))))))
|
|
292 ;;; (progn (setq state 'list)
|
|
293 ;;; (list form 'X 'Y .. ))) ; quote each one there is
|
|
294 (defun bq-evalquote (form)
|
|
295 (cond ((< (length tailmaker) 3)
|
|
296 (setq tailmaker
|
|
297 (cons form
|
|
298 (mapcar (function (lambda (x)
|
|
299 (list 'quote x)))
|
|
300 tailmaker)))
|
|
301 (setq state 'list))
|
|
302 (t
|
|
303 (setq tailmaker
|
|
304 (list (list 'list form)
|
|
305 (list 'quote tailmaker)))
|
|
306 (setq state 'append))))
|
|
307
|
|
308 (defun bq-evallist (form)
|
|
309 (bq-push form tailmaker))
|
|
310
|
|
311 ;;; (cond ((matches ((list (,@ X)) (,@ Y)))
|
|
312 ;;; (` ((list form (,@ X)) (,@ Y))))
|
|
313 ;;; ((matches (X))
|
|
314 ;;; (` (form (,@ X))) (setq state 'cons))
|
|
315 ;;; ((matches ((,@ X)))
|
|
316 ;;; (` (form (,@ X)))))
|
|
317 (defun bq-evalappend (form)
|
|
318 (cond ((and (listp tailmaker)
|
|
319 (listp (car tailmaker))
|
|
320 (eq (bq-caar tailmaker) 'list))
|
|
321 (rplacd (car tailmaker)
|
|
322 (cons form (bq-cdar tailmaker))))
|
|
323 ((= (length tailmaker) 1)
|
|
324 (setq tailmaker (cons form tailmaker)
|
|
325 state 'cons))
|
|
326 (t (bq-push (list 'list form) tailmaker))))
|
|
327
|
|
328 (defun bq-evalnil (form)
|
|
329 (setq tailmaker (list form)
|
|
330 state 'list))
|
|
331
|
|
332 ;;; (if (matches (X Y)) ; it must
|
|
333 ;;; (progn (setq state 'append)
|
|
334 ;;; (` (form (cons X Y))))) ; couldn't think of anything clever
|
|
335 (defun bq-splicecons (form)
|
|
336 (setq tailmaker
|
|
337 (list form
|
|
338 (list 'cons (car tailmaker) (bq-cadr tailmaker)))
|
|
339 state 'append))
|
|
340
|
|
341 (defun bq-splicequote (form)
|
|
342 (setq tailmaker (list form (list 'quote tailmaker))
|
|
343 state 'append))
|
|
344
|
|
345 (defun bq-splicelist (form)
|
|
346 (setq tailmaker (list form (cons 'list tailmaker))
|
|
347 state 'append))
|
|
348
|
|
349 (defun bq-spliceappend (form)
|
|
350 (bq-push form tailmaker))
|
|
351
|
|
352 (defun bq-splicenil (form)
|
|
353 (setq state 'append
|
|
354 tailmaker (list form)))
|