| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;; ********************************************************************* |
| 6 |
;;; This code was written as part of the CMU Common Lisp project at |
| 7 |
;;; Carnegie Mellon University, and has been placed in the public domain. |
| 8 |
;;; |
| 9 |
;;; ********************************************************************* |
| 10 |
|
| 11 |
(in-package "SI") |
| 12 |
|
| 13 |
|
| 14 |
#+ecl-min |
| 15 |
(defmacro handler-bind (bindings &body body) |
| 16 |
`(progn ,@body)) |
| 17 |
|
| 18 |
|
| 19 |
;;;; The Collect macro: |
| 20 |
|
| 21 |
;;; Collect-Normal-Expander -- Internal |
| 22 |
;;; |
| 23 |
;;; This function does the real work of macroexpansion for normal |
| 24 |
;;; collection macros. N-Value is the name of the variable which |
| 25 |
;;; holds the current value. Fun is the function which does |
| 26 |
;;; collection. Forms is the list of forms whose values we are |
| 27 |
;;; supposed to collect. |
| 28 |
;;; |
| 29 |
(defun collect-normal-expander (n-value fun forms) |
| 30 |
`(progn |
| 31 |
,@(mapcar #'(lambda (form) `(setq ,n-value (,fun ,form ,n-value))) forms) |
| 32 |
,n-value)) |
| 33 |
|
| 34 |
;;; Collect-List-Expander -- Internal |
| 35 |
;;; |
| 36 |
;;; This function deals with the list collection case. N-Tail is |
| 37 |
;;; the pointer to the current tail of the list, which is NIL if the |
| 38 |
;;; list is empty. |
| 39 |
;;; |
| 40 |
(defun collect-list-expander (n-value n-tail forms) |
| 41 |
(let ((n-res (gensym))) |
| 42 |
`(progn |
| 43 |
,@(mapcar #'(lambda (form) |
| 44 |
`(let ((,n-res (cons ,form nil))) |
| 45 |
(cond (,n-tail |
| 46 |
(rplacd ,n-tail ,n-res) |
| 47 |
(setq ,n-tail ,n-res)) |
| 48 |
(t |
| 49 |
(setq ,n-tail ,n-res ,n-value ,n-res))))) |
| 50 |
forms) |
| 51 |
,n-value))) |
| 52 |
|
| 53 |
|
| 54 |
;;; Collect -- Public |
| 55 |
;;; |
| 56 |
;;; The ultimate collection macro... |
| 57 |
;;; |
| 58 |
(defmacro collect (collections &body body) |
| 59 |
"Collect ({(Name [Initial-Value] [Function])}*) {Form}* |
| 60 |
|
| 61 |
Collect some values somehow. Each of the collections specifies a |
| 62 |
bunch of things which collected during the evaluation of the body of |
| 63 |
the form. The name of the collection is used to define a local macro, |
| 64 |
a la MACROLET. Within the body, this macro will evaluate each of its |
| 65 |
arguments and collect the result, returning the current value after |
| 66 |
the collection is done. The body is evaluated as a PROGN; to get the |
| 67 |
final values when you are done, just call the collection macro with no |
| 68 |
arguments. |
| 69 |
|
| 70 |
Initial-Value is the value that the collection starts out with, which |
| 71 |
defaults to NIL. Function is the function which does the collection. |
| 72 |
It is a function which will accept two arguments: the value to be |
| 73 |
collected and the current collection. The result of the function is |
| 74 |
made the new value for the collection. As a totally magical |
| 75 |
special-case, the Function may be Collect, which tells us to build a |
| 76 |
list in forward order; this is the default. If an Initial-Value is |
| 77 |
supplied for Collect, the stuff will be rplacd'd onto the end. Note |
| 78 |
that Function may be anything that can appear in the functional |
| 79 |
position, including macros and lambdas. |
| 80 |
|
| 81 |
Bare symbols in `specs' are equivalent to: |
| 82 |
|
| 83 |
(symbol nil 'collect) |
| 84 |
|
| 85 |
Example: |
| 86 |
|
| 87 |
(collect (a b) |
| 88 |
(a 3) |
| 89 |
(a 4) |
| 90 |
(b 5) |
| 91 |
(list (a) (b))) |
| 92 |
; => ((3 4) (5)) |
| 93 |
" |
| 94 |
|
| 95 |
(let (macros binds) |
| 96 |
(dolist (spec collections) |
| 97 |
(cond ((atom spec) |
| 98 |
(setq spec (list spec))) |
| 99 |
((not (<= 1 (length spec) 3)) |
| 100 |
(error "Malformed collection specifier: ~S." spec))) |
| 101 |
(let ((n-value (gensym)) |
| 102 |
(name (first spec)) |
| 103 |
(default (second spec)) |
| 104 |
(kind (or (third spec) 'collect))) |
| 105 |
(push `(,n-value ,default) binds) |
| 106 |
(if (eq kind 'collect) |
| 107 |
(let ((n-tail (gensym))) |
| 108 |
(if default |
| 109 |
(push `(,n-tail (last ,n-value)) binds) |
| 110 |
(push n-tail binds)) |
| 111 |
(push `(,name (&rest args) |
| 112 |
(collect-list-expander ',n-value ',n-tail args)) |
| 113 |
macros)) |
| 114 |
(push `(,name (&rest args) |
| 115 |
(collect-normal-expander ',n-value ',kind args)) |
| 116 |
macros)))) |
| 117 |
`(macrolet ,macros (let* ,(nreverse binds) ,@body)))) |
| 118 |
|
| 119 |
|
| 120 |
;;;; The Once-Only macro: |
| 121 |
|
| 122 |
;;; Once-Only -- Interface |
| 123 |
;;; |
| 124 |
;;; Once-Only is a utility useful in writing source transforms and |
| 125 |
;;; macros. It provides an easy way to wrap a let around some code |
| 126 |
;;; to ensure that some forms are only evaluated once. |
| 127 |
;;; |
| 128 |
(defmacro once-only (specs &body body) |
| 129 |
"Once-Only ({(Var Value-Expression)}*) Form* |
| 130 |
|
| 131 |
Create a Let* which evaluates each Value-Expression, binding a |
| 132 |
temporary variable to the result, and wrapping the Let* around the |
| 133 |
result of the evaluation of Body. Within the body, each Var is bound |
| 134 |
to the corresponding temporary variable. |
| 135 |
|
| 136 |
Bare symbols in `specs' are equivalent to: |
| 137 |
|
| 138 |
(symbol symbol) |
| 139 |
|
| 140 |
Example: |
| 141 |
|
| 142 |
(defmacro cons1 (x) |
| 143 |
(once-only (x) `(cons ,x ,x))) |
| 144 |
(let ((y 0)) |
| 145 |
(cons1 (incf y))) |
| 146 |
; => (1 . 1) |
| 147 |
" |
| 148 |
(labels ((frob (specs body) |
| 149 |
(if (null specs) |
| 150 |
`(progn ,@body) |
| 151 |
(let ((spec (first specs))) |
| 152 |
(cond ((atom spec) |
| 153 |
(setq spec (list spec spec))) |
| 154 |
((/= (length spec) 2) |
| 155 |
(error "Malformed Once-Only binding spec: ~S." spec))) |
| 156 |
(let ((name (first spec)) |
| 157 |
(exp-temp (gensym))) |
| 158 |
`(let ((,exp-temp ,(second spec)) |
| 159 |
(,name (gensym "OO-"))) |
| 160 |
`(let ((,,name ,,exp-temp)) |
| 161 |
,,(frob (rest specs) body)))))))) |
| 162 |
(frob specs body))) |
| 163 |
|
| 164 |
|
| 165 |
;;; Automate an idiom often found in macros: |
| 166 |
;;; (LET ((FOO (GENSYM "FOO")) |
| 167 |
;;; (MAX-INDEX (GENSYM "MAX-INDEX-"))) |
| 168 |
;;; ...) |
| 169 |
;;; |
| 170 |
;;; "Good notation eliminates thought." -- Eric Siggia |
| 171 |
;;; |
| 172 |
;;; Incidentally, this is essentially the same operator which |
| 173 |
;;; _On Lisp_ calls WITH-GENSYMS. |
| 174 |
(defmacro with-unique-names (symbols &body body) |
| 175 |
"Binds each variable named by a symbol in `symbols' to a unique |
| 176 |
symbol around `body'." |
| 177 |
`(let* ,(mapcar (lambda (symbol) |
| 178 |
(let* ((symbol-name (symbol-name symbol)) |
| 179 |
(stem symbol-name)) |
| 180 |
`(,symbol (gensym ,stem)))) |
| 181 |
symbols) |
| 182 |
,@body)) |
| 183 |
|
| 184 |
(defmacro with-gensyms (symbols &body body) |
| 185 |
"Alias for `with-unique-names'." |
| 186 |
`(with-unique-names ,symbols ,@body)) |
| 187 |
|
| 188 |
|
| 189 |
(defmacro with-clean-symbols (symbols &body body) |
| 190 |
"Rewrites the given forms replacing the given symbols with uninterned |
| 191 |
ones, which is useful for creating hygienic macros." |
| 192 |
`(progn ,@(sublis |
| 193 |
(mapcar #'(lambda (s) |
| 194 |
(cons s (make-symbol (symbol-name s)))) |
| 195 |
symbols) |
| 196 |
body))) |
| 197 |
|
| 198 |
|
| 199 |
;;; Curbed from alexandria |
| 200 |
(defmacro if-let (bindings &body (then-form &optional else-form)) |
| 201 |
(let* ((binding-list (if (and (consp bindings) (symbolp (car bindings))) |
| 202 |
(list bindings) |
| 203 |
bindings)) |
| 204 |
(variables (mapcar #'car binding-list))) |
| 205 |
`(let ,binding-list |
| 206 |
(if (and ,@variables) |
| 207 |
,then-form |
| 208 |
,else-form)))) |
| 209 |
|
| 210 |
(defmacro when-let (bindings &body forms) |
| 211 |
`(if-let ,bindings |
| 212 |
(progn ,@forms))) |
| 213 |
|
| 214 |
(defmacro when-let* (bindings &body body) |
| 215 |
(let ((binding-list (if (and (consp bindings) (symbolp (car bindings))) |
| 216 |
(list bindings) |
| 217 |
bindings))) |
| 218 |
(labels ((bind (bindings body) |
| 219 |
(if bindings |
| 220 |
`((let (,(car bindings)) |
| 221 |
(when ,(caar bindings) |
| 222 |
,@(bind (cdr bindings) body)))) |
| 223 |
body))) |
| 224 |
`(let (,(car binding-list)) |
| 225 |
(when ,(caar binding-list) |
| 226 |
,@(bind (cdr binding-list) body)))))) |