| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
(provide 'backquote) |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
(defun backquote-list*-function (first &rest list) |
|---|
| 45 |
"Like `list' but the last argument is the tail of the new list. |
|---|
| 46 |
|
|---|
| 47 |
For example (backquote-list* 'a 'b 'c) => (a b . c)" |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
(if list |
|---|
| 52 |
(let* ((rest list) (newlist (cons first nil)) (last newlist)) |
|---|
| 53 |
(while (cdr rest) |
|---|
| 54 |
(setcdr last (cons (car rest) nil)) |
|---|
| 55 |
(setq last (cdr last) |
|---|
| 56 |
rest (cdr rest))) |
|---|
| 57 |
(setcdr last (car rest)) |
|---|
| 58 |
newlist) |
|---|
| 59 |
first)) |
|---|
| 60 |
|
|---|
| 61 |
(defmacro backquote-list*-macro (first &rest list) |
|---|
| 62 |
"Like `list' but the last argument is the tail of the new list. |
|---|
| 63 |
|
|---|
| 64 |
For example (backquote-list* 'a 'b 'c) => (a b . c)" |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
(setq list (nreverse (cons first list)) |
|---|
| 69 |
first (car list) |
|---|
| 70 |
list (cdr list)) |
|---|
| 71 |
(if list |
|---|
| 72 |
(let* ((second (car list)) |
|---|
| 73 |
(rest (cdr list)) |
|---|
| 74 |
(newlist (list 'cons second first))) |
|---|
| 75 |
(while rest |
|---|
| 76 |
(setq newlist (list 'cons (car rest) newlist) |
|---|
| 77 |
rest (cdr rest))) |
|---|
| 78 |
newlist) |
|---|
| 79 |
first)) |
|---|
| 80 |
|
|---|
| 81 |
(defalias 'backquote-list* (symbol-function 'backquote-list*-macro)) |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
(defconst backquote-backquote-symbol '\` |
|---|
| 86 |
"Symbol used to represent a backquote or nested backquote.") |
|---|
| 87 |
|
|---|
| 88 |
(defconst backquote-unquote-symbol '\, |
|---|
| 89 |
"Symbol used to represent an unquote inside a backquote.") |
|---|
| 90 |
|
|---|
| 91 |
(defconst backquote-splice-symbol '\,@ |
|---|
| 92 |
"Symbol used to represent a splice inside a backquote.") |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
(defmacro backquote (arg) |
|---|
| 96 |
"Argument STRUCTURE describes a template to build. |
|---|
| 97 |
|
|---|
| 98 |
The whole structure acts as if it were quoted except for certain |
|---|
| 99 |
places where expressions are evaluated and inserted or spliced in. |
|---|
| 100 |
|
|---|
| 101 |
For example: |
|---|
| 102 |
|
|---|
| 103 |
b => (ba bb bc) ; assume b has this value |
|---|
| 104 |
`(a b c) => (a b c) ; backquote acts like quote |
|---|
| 105 |
`(a ,b c) => (a (ba bb bc) c) ; insert the value of b |
|---|
| 106 |
`(a ,@b c) => (a ba bb bc c) ; splice in the value of b |
|---|
| 107 |
|
|---|
| 108 |
Vectors work just like lists. Nested backquotes are permitted." |
|---|
| 109 |
(cdr (backquote-process arg))) |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
(defalias '\` (symbol-function 'backquote)) |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
(defun backquote-delay-process (s level) |
|---|
| 122 |
"Process a (un|back|splice)quote inside a backquote. |
|---|
| 123 |
This simply recurses through the body." |
|---|
| 124 |
(let ((exp (backquote-listify (list (cons 0 (list 'quote (car s)))) |
|---|
| 125 |
(backquote-process (cdr s) level)))) |
|---|
| 126 |
(if (eq (car-safe exp) 'quote) |
|---|
| 127 |
(cons 0 (list 'quote s)) |
|---|
| 128 |
(cons 1 exp)))) |
|---|
| 129 |
|
|---|
| 130 |
(defun backquote-process (s &optional level) |
|---|
| 131 |
"Process the body of a backquote. |
|---|
| 132 |
S is the body. Returns a cons cell whose cdr is piece of code which |
|---|
| 133 |
is the macro-expansion of S, and whose car is a small integer whose value |
|---|
| 134 |
can either indicate that the code is constant (0), or not (1), or returns |
|---|
| 135 |
a list which should be spliced into its environment (2). |
|---|
| 136 |
LEVEL is only used internally and indicates the nesting level: |
|---|
| 137 |
0 (the default) is for the toplevel nested inside a single backquote." |
|---|
| 138 |
(unless level (setq level 0)) |
|---|
| 139 |
(cond |
|---|
| 140 |
((vectorp s) |
|---|
| 141 |
(let ((n (backquote-process (append s ()) level))) |
|---|
| 142 |
(if (= (car n) 0) |
|---|
| 143 |
(cons 0 s) |
|---|
| 144 |
(cons 1 (cond |
|---|
| 145 |
((not (listp (cdr n))) |
|---|
| 146 |
(list 'vconcat (cdr n))) |
|---|
| 147 |
((eq (nth 1 n) 'list) |
|---|
| 148 |
(cons 'vector (nthcdr 2 n))) |
|---|
| 149 |
((eq (nth 1 n) 'append) |
|---|
| 150 |
(cons 'vconcat (nthcdr 2 n))) |
|---|
| 151 |
(t |
|---|
| 152 |
(list 'apply '(function vector) (cdr n)))))))) |
|---|
| 153 |
((atom s) |
|---|
| 154 |
(cons 0 (if (or (null s) (eq s t) (not (symbolp s))) |
|---|
| 155 |
s |
|---|
| 156 |
(list 'quote s)))) |
|---|
| 157 |
((eq (car s) backquote-unquote-symbol) |
|---|
| 158 |
(if (<= level 0) |
|---|
| 159 |
(cons 1 (nth 1 s)) |
|---|
| 160 |
(backquote-delay-process s (1- level)))) |
|---|
| 161 |
((eq (car s) backquote-splice-symbol) |
|---|
| 162 |
(if (<= level 0) |
|---|
| 163 |
(cons 2 (nth 1 s)) |
|---|
| 164 |
(backquote-delay-process s (1- level)))) |
|---|
| 165 |
((eq (car s) backquote-backquote-symbol) |
|---|
| 166 |
(backquote-delay-process s (1+ level))) |
|---|
| 167 |
(t |
|---|
| 168 |
(let ((rest s) |
|---|
| 169 |
item firstlist list lists expression) |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
(while (and (consp rest) |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
(not (or (eq (car rest) backquote-unquote-symbol) |
|---|
| 183 |
(eq (car rest) backquote-backquote-symbol)))) |
|---|
| 184 |
(setq item (backquote-process (car rest) level)) |
|---|
| 185 |
(cond |
|---|
| 186 |
((= (car item) 2) |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
(if (null lists) |
|---|
| 190 |
(setq firstlist list |
|---|
| 191 |
list nil)) |
|---|
| 192 |
|
|---|
| 193 |
(if list |
|---|
| 194 |
(push (backquote-listify list '(0 . nil)) lists)) |
|---|
| 195 |
(push (cdr item) lists) |
|---|
| 196 |
(setq list nil)) |
|---|
| 197 |
(t |
|---|
| 198 |
(setq list (cons item list)))) |
|---|
| 199 |
(setq rest (cdr rest))) |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
(if (or rest list) |
|---|
| 203 |
(push (backquote-listify list (backquote-process rest level)) |
|---|
| 204 |
lists)) |
|---|
| 205 |
|
|---|
| 206 |
(setq expression |
|---|
| 207 |
(if (or (cdr lists) |
|---|
| 208 |
(eq (car-safe (car lists)) backquote-splice-symbol)) |
|---|
| 209 |
(cons 'append (nreverse lists)) |
|---|
| 210 |
(car lists))) |
|---|
| 211 |
|
|---|
| 212 |
(if firstlist |
|---|
| 213 |
(setq expression (backquote-listify firstlist (cons 1 expression)))) |
|---|
| 214 |
(if (eq (car-safe expression) 'quote) |
|---|
| 215 |
(cons 0 (list 'quote s)) |
|---|
| 216 |
(cons 1 expression)))))) |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
(defun backquote-listify (list old-tail) |
|---|
| 223 |
(let ((heads nil) (tail (cdr old-tail)) (list-tail list) (item nil)) |
|---|
| 224 |
(if (= (car old-tail) 0) |
|---|
| 225 |
(setq tail (eval tail) |
|---|
| 226 |
old-tail nil)) |
|---|
| 227 |
(while (consp list-tail) |
|---|
| 228 |
(setq item (car list-tail)) |
|---|
| 229 |
(setq list-tail (cdr list-tail)) |
|---|
| 230 |
(if (or heads old-tail (/= (car item) 0)) |
|---|
| 231 |
(setq heads (cons (cdr item) heads)) |
|---|
| 232 |
(setq tail (cons (eval (cdr item)) tail)))) |
|---|
| 233 |
(cond |
|---|
| 234 |
(tail |
|---|
| 235 |
(if (null old-tail) |
|---|
| 236 |
(setq tail (list 'quote tail))) |
|---|
| 237 |
(if heads |
|---|
| 238 |
(let ((use-list* (or (cdr heads) |
|---|
| 239 |
(and (consp (car heads)) |
|---|
| 240 |
(eq (car (car heads)) |
|---|
| 241 |
backquote-splice-symbol))))) |
|---|
| 242 |
(cons (if use-list* 'backquote-list* 'cons) |
|---|
| 243 |
(append heads (list tail)))) |
|---|
| 244 |
tail)) |
|---|
| 245 |
(t (cons 'list heads))))) |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|