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 ;;; a raft of general-purpose macros follows. See the nearest
|
|
61 ;;; Commonlisp manual.
|
|
62 (defmacro bq-push (v l)
|
|
63 "Pushes evaluated first form onto second unevaluated object
|
|
64 a list-value atom"
|
|
65 (list 'setq l (list 'cons v l)))
|
|
66
|
|
67 (defmacro bq-caar (l)
|
|
68 (list 'car (list 'car l)))
|
|
69
|
|
70 (defmacro bq-cadr (l)
|
|
71 (list 'car (list 'cdr l)))
|
|
72
|
|
73 (defmacro bq-cdar (l)
|
|
74 (list 'cdr (list 'car l)))
|
|
75
|
|
76
|
|
77 ;;; These two advertised variables control what characters are used to
|
|
78 ;;; unquote things. I have included , and ,@ as the unquote and
|
|
79 ;;; splice operators, respectively, to give users of MIT CADR machine
|
|
80 ;;; derivitive machines a warm, cosy feeling.
|
|
81
|
|
82 (defconst backquote-unquote '(,)
|
|
83 "*A list of all objects that stimulate unquoting in `. Memq test.")
|
|
84
|
|
85
|
|
86 (defconst backquote-splice '(,@)
|
|
87 "*A list of all objects that stimulate splicing in `. Memq test.")
|
|
88
|
|
89
|
|
90 ;;; This is the interface
|
256
|
91 ;;;###autoload
|
181
|
92 (defmacro ` (form)
|
|
93 "(` FORM) is a macro that expands to code to construct FORM.
|
|
94 Note that this is very slow in interpreted code, but fast if you compile.
|
|
95 FORM is one or more nested lists, which are `almost quoted':
|
|
96 They are copied recursively, with non-lists used unchanged in the copy.
|
|
97 (` a b) == (list 'a 'b) constructs a new list with two elements, `a' and `b'.
|
|
98 (` a (b c)) == (list 'a (list 'b 'c)) constructs two nested new lists.
|
|
99
|
|
100 However, certain special lists are not copied. They specify substitution.
|
|
101 Lists that look like (, EXP) are evaluated and the result is substituted.
|
|
102 (` a (, (+ x 5))) == (list 'a (+ x 5))
|
|
103
|
|
104 Elements of the form (,@ EXP) are evaluated and then all the elements
|
|
105 of the result are substituted. This result must be a list; it may
|
|
106 be `nil'.
|
|
107
|
|
108 As an example, a simple macro `push' could be written:
|
|
109 (defmacro push (v l)
|
|
110 (` (setq (, l) (cons (,@ (list v l))))))
|
|
111 or as
|
|
112 (defmacro push (v l)
|
|
113 (` (setq (, l) (cons (, v) (, l)))))
|
|
114
|
|
115 LIMITATIONS: \"dotted lists\" are not allowed in FORM.
|
|
116 The ultimate cdr of each list scanned by ` must be `nil'.
|
|
117 \(This does not apply to constants inside expressions to be substituted.)
|
|
118
|
|
119 Substitution elements are not allowed as the cdr
|
|
120 of a cons cell. For example, (` (A . (, B))) does not work.
|
|
121 Instead, write (` (A (,@ B))).
|
|
122
|
|
123 You cannot construct vectors, only lists. Vectors are treated as
|
|
124 constants.
|
|
125
|
|
126 BEWARE BEWARE BEWARE
|
|
127 Inclusion of (,ATOM) rather than (, ATOM)
|
|
128 or of (,@ATOM) rather than (,@ ATOM)
|
|
129 will result in errors that will show up very late."
|
|
130 (bq-make-maker form))
|
|
131
|
|
132 ;;; We develop the method for building the desired list from
|
|
133 ;;; the end towards the beginning. The contract is that there be a
|
|
134 ;;; variable called state and a list called tailmaker, and that the form
|
|
135 ;;; (cons state tailmaker) deliver the goods. Exception - if the
|
|
136 ;;; state is quote the tailmaker is the form itself.
|
|
137 ;;; This function takes a form and returns what I will call a maker in
|
|
138 ;;; what follows. Evaluating the maker would produce the form,
|
|
139 ;;; properly evaluated according to , and ,@ rules.
|
|
140 ;;; I work backwards - it seemed a lot easier. The reason for this is
|
|
141 ;;; if I'm in some sort of a routine building a maker and I switch
|
|
142 ;;; gears, it seemed to me easier to jump into some other state and
|
|
143 ;;; glue what I've already done to the end, than to to prepare that
|
|
144 ;;; something and go back to put things together.
|
|
145 (defun bq-make-maker (form)
|
|
146 "Given one argument, a `mostly quoted' object, produces a maker.
|
|
147 See backquote.el for details"
|
|
148 (let ((tailmaker (quote nil)) (qc 0) (ec 0) (state nil))
|
|
149 (mapcar 'bq-iterative-list-builder (reverse form))
|
|
150 (and state
|
|
151 (cond ((eq state 'quote)
|
340
|
152 (list state (if (equal form tailmaker) form tailmaker)))
|
181
|
153 ((= (length tailmaker) 1)
|
|
154 (funcall (bq-cadr (assq state bq-singles)) tailmaker))
|
|
155 (t (cons state tailmaker))))))
|
|
156
|
|
157 ;;; There are exceptions - we wouldn't want to call append of one
|
|
158 ;;; argument, for example.
|
|
159 (defconst bq-singles '((quote bq-quotecar)
|
|
160 (append car)
|
|
161 (list bq-make-list)
|
|
162 (cons bq-id)))
|
|
163
|
|
164 (defun bq-id (x) x)
|
|
165
|
|
166 (defun bq-quotecar (x) (list 'quote (car x)))
|
|
167
|
|
168 (defun bq-make-list (x) (cons 'list x))
|
|
169
|
|
170 ;;; fr debugging use only
|
|
171 ;(defun funcalll (a b) (funcall a b))
|
|
172 ;(defun funcalll (a b) (debug nil 'enter state tailmaker a b)
|
|
173 ; (let ((ans (funcall a b))) (debug nil 'leave state tailmaker)
|
|
174 ; ans))
|
|
175
|
|
176 ;;; Given a state/tailmaker pair that already knows how to make a
|
|
177 ;;; partial tail of the desired form, this function knows how to add
|
|
178 ;;; yet another element to the burgening list. There are four cases;
|
|
179 ;;; the next item is an atom (which will certainly be quoted); a
|
|
180 ;;; (, xxx), which will be evaluated and put into the list at the top
|
|
181 ;;; level; a (,@ xxx), which will be evaluated and spliced in, or
|
|
182 ;;; some other list, in which case we first compute the form's maker,
|
|
183 ;;; and then we either launch into the quoted case if the maker's
|
|
184 ;;; top level function is quote, or into the comma case if it isn't.
|
|
185 ;;; The fourth case reduces to one of the other three, so here we have
|
|
186 ;;; a choice of three ways to build tailmaker, and cit turns out we
|
|
187 ;;; use five possible values of state (although someday I'll add
|
|
188 ;;; nconcto the possible values of state).
|
|
189 ;;; This maintains the invariant that (cons state tailmaker) is the
|
|
190 ;;; maker for the elements of the tail we've eaten so far.
|
|
191 (defun bq-iterative-list-builder (form)
|
|
192 "Called by `bq-make-maker'. Adds a new item form to tailmaker,
|
|
193 changing state if need be, so tailmaker and state constitute a recipe
|
|
194 for making the list so far."
|
|
195 (cond ((atom form)
|
|
196 (funcall (bq-cadr (assq state bq-quotefns)) form))
|
|
197 ((memq (car form) backquote-unquote)
|
|
198 (funcall (bq-cadr (assq state bq-evalfns)) (bq-cadr form)))
|
|
199 ((memq (car form) backquote-splice)
|
|
200 (funcall (bq-cadr (assq state bq-splicefns)) (bq-cadr form)))
|
|
201 (t
|
|
202 (let ((newform (bq-make-maker form)))
|
|
203 (if (and (listp newform) (eq (car newform) 'quote))
|
|
204 (funcall (bq-cadr (assq state bq-quotefns)) (bq-cadr newform))
|
|
205 (funcall (bq-cadr (assq state bq-evalfns)) newform))))
|
|
206 ))
|
|
207
|
|
208 ;;; We do a 2-d branch on the form of splicing and the old state.
|
|
209 ;;; Here's fifteen functions' names...
|
|
210 (defconst bq-splicefns '((nil bq-splicenil)
|
|
211 (append bq-spliceappend)
|
|
212 (list bq-splicelist)
|
|
213 (quote bq-splicequote)
|
|
214 (cons bq-splicecons)))
|
|
215
|
|
216 (defconst bq-evalfns '((nil bq-evalnil)
|
|
217 (append bq-evalappend)
|
|
218 (list bq-evallist)
|
|
219 (quote bq-evalquote)
|
|
220 (cons bq-evalcons)))
|
|
221
|
|
222 (defconst bq-quotefns '((nil bq-quotenil)
|
|
223 (append bq-quoteappend)
|
|
224 (list bq-quotelist)
|
|
225 (quote bq-quotequote)
|
|
226 (cons bq-quotecons)))
|
|
227
|
|
228 ;;; The name of each function is
|
|
229 ;;; (concat 'bq- <type-of-element-addition> <old-state>)
|
|
230 ;;; I'll comment the non-obvious ones before the definitions...
|
|
231 ;;; In what follows, uppercase letters and form will always be
|
|
232 ;;; metavariables that don't need commas in backquotes, and I will
|
|
233 ;;; assume the existence of something like matches that takes a
|
|
234 ;;; backquote-like form and a value, binds metavariables and returns
|
|
235 ;;; t if the pattern match is successful, returns nil otherwise. I
|
|
236 ;;; will write such a goodie someday.
|
|
237
|
|
238 ;;; (setq tailmaker
|
|
239 ;;; (if (matches ((quote X) Y) tailmaker)
|
|
240 ;;; (` ((quote (form X)) Y))
|
|
241 ;;; (` ((list form (quote X)) Y))))
|
|
242 ;;; (setq state 'append)
|
|
243 (defun bq-quotecons (form)
|
|
244 (if (and (listp (car tailmaker))
|
|
245 (eq (bq-caar tailmaker) 'quote))
|
|
246 (setq tailmaker
|
|
247 (list (list 'quote (list form (bq-cadr (car tailmaker))))
|
|
248 (bq-cadr tailmaker)))
|
|
249 (setq tailmaker
|
|
250 (list (list 'list
|
|
251 (list 'quote form)
|
|
252 (car tailmaker))
|
|
253 (bq-cadr tailmaker))))
|
|
254 (setq state 'append))
|
|
255
|
|
256 (defun bq-quotequote (form)
|
|
257 (bq-push form tailmaker))
|
|
258
|
|
259 ;;; Could be improved to convert (list 'a 'b 'c .. 'w x)
|
|
260 ;;; to (append '(a b c .. w) x)
|
|
261 ;;; when there are enough elements
|
|
262 (defun bq-quotelist (form)
|
|
263 (bq-push (list 'quote form) tailmaker))
|
|
264
|
|
265 ;;; (setq tailmaker
|
|
266 ;;; (if (matches ((quote X) (,@ Y)))
|
|
267 ;;; (` ((quote (, (cons form X))) (,@ Y)))))
|
|
268 (defun bq-quoteappend (form)
|
|
269 (cond ((and (listp tailmaker)
|
|
270 (listp (car tailmaker))
|
|
271 (eq (bq-caar tailmaker) 'quote))
|
|
272 (rplaca (bq-cdar tailmaker)
|
|
273 (cons form (car (bq-cdar tailmaker)))))
|
|
274 (t (bq-push (list 'quote (list form)) tailmaker))))
|
|
275
|
|
276 (defun bq-quotenil (form)
|
|
277 (setq tailmaker (list form))
|
|
278 (setq state 'quote))
|
|
279
|
|
280 ;;; (if (matches (X Y) tailmaker) ; it must
|
|
281 ;;; (` ((list form X) Y)))
|
|
282 (defun bq-evalcons (form)
|
|
283 (setq tailmaker
|
|
284 (list (list 'list form (car tailmaker))
|
|
285 (bq-cadr tailmaker)))
|
|
286 (setq state 'append))
|
|
287
|
|
288 ;;; (if (matches (X Y Z (,@ W)))
|
|
289 ;;; (progn (setq state 'append)
|
|
290 ;;; (` ((list form) (quote (X Y Z (,@ W))))))
|
|
291 ;;; (progn (setq state 'list)
|
|
292 ;;; (list form 'X 'Y .. ))) ; quote each one there is
|
|
293 (defun bq-evalquote (form)
|
|
294 (cond ((< (length tailmaker) 3)
|
|
295 (setq tailmaker
|
|
296 (cons form
|
|
297 (mapcar (function (lambda (x)
|
|
298 (list 'quote x)))
|
|
299 tailmaker)))
|
|
300 (setq state 'list))
|
|
301 (t
|
|
302 (setq tailmaker
|
|
303 (list (list 'list form)
|
|
304 (list 'quote tailmaker)))
|
|
305 (setq state 'append))))
|
|
306
|
|
307 (defun bq-evallist (form)
|
|
308 (bq-push form tailmaker))
|
|
309
|
|
310 ;;; (cond ((matches ((list (,@ X)) (,@ Y)))
|
|
311 ;;; (` ((list form (,@ X)) (,@ Y))))
|
|
312 ;;; ((matches (X))
|
|
313 ;;; (` (form (,@ X))) (setq state 'cons))
|
|
314 ;;; ((matches ((,@ X)))
|
|
315 ;;; (` (form (,@ X)))))
|
|
316 (defun bq-evalappend (form)
|
|
317 (cond ((and (listp tailmaker)
|
|
318 (listp (car tailmaker))
|
|
319 (eq (bq-caar tailmaker) 'list))
|
|
320 (rplacd (car tailmaker)
|
|
321 (cons form (bq-cdar tailmaker))))
|
|
322 ((= (length tailmaker) 1)
|
|
323 (setq tailmaker (cons form tailmaker)
|
|
324 state 'cons))
|
|
325 (t (bq-push (list 'list form) tailmaker))))
|
|
326
|
|
327 (defun bq-evalnil (form)
|
|
328 (setq tailmaker (list form)
|
|
329 state 'list))
|
|
330
|
|
331 ;;; (if (matches (X Y)) ; it must
|
|
332 ;;; (progn (setq state 'append)
|
|
333 ;;; (` (form (cons X Y))))) ; couldn't think of anything clever
|
|
334 (defun bq-splicecons (form)
|
|
335 (setq tailmaker
|
|
336 (list form
|
|
337 (list 'cons (car tailmaker) (bq-cadr tailmaker)))
|
|
338 state 'append))
|
|
339
|
|
340 (defun bq-splicequote (form)
|
|
341 (setq tailmaker (list form (list 'quote tailmaker))
|
|
342 state 'append))
|
|
343
|
|
344 (defun bq-splicelist (form)
|
|
345 (setq tailmaker (list form (cons 'list tailmaker))
|
|
346 state 'append))
|
|
347
|
|
348 (defun bq-spliceappend (form)
|
|
349 (bq-push form tailmaker))
|
|
350
|
|
351 (defun bq-splicenil (form)
|
|
352 (setq state 'append
|
|
353 tailmaker (list form)))
|
584
|
354
|
|
355 (provide 'backquote)
|
|
356
|