| 1 |
;;;; Anaphora: The Anaphoric Macro Package from Hell |
|---|
| 2 |
;;;; |
|---|
| 3 |
;;;; This been placed in Public Domain by the author, |
|---|
| 4 |
;;;; Nikodemus Siivola <nikodemus@random-state.net> |
|---|
| 5 |
|
|---|
| 6 |
(in-package :anaphora) |
|---|
| 7 |
|
|---|
| 8 |
(defmacro anaphoric (op test &body body) |
|---|
| 9 |
;; Note: multiple values discarded. Handling them would be nice, but also |
|---|
| 10 |
;; requires consing up a values-list, which seems a bit harsh for something |
|---|
| 11 |
;; that is supposed to be "simple syntactic sugar". |
|---|
| 12 |
`(let ((it ,test)) |
|---|
| 13 |
(,op it ,@body))) |
|---|
| 14 |
|
|---|
| 15 |
;;; This was the original implementation of SYMBOLIC -- and still good |
|---|
| 16 |
;;; for getting the basic idea. Brian Masterbrooks solution to |
|---|
| 17 |
;;; infinite recusion during macroexpansion, that nested forms of this |
|---|
| 18 |
;;; are subject to, is in symbolic.lisp. |
|---|
| 19 |
;;; |
|---|
| 20 |
;;; (defmacro symbolic (op test &body body &environment env) |
|---|
| 21 |
;;; `(symbol-macrolet ((it ,test)) |
|---|
| 22 |
;;; (,op it ,@body))) |
|---|
| 23 |
|
|---|
| 24 |
(defmacro aand (first &rest rest) |
|---|
| 25 |
"Like AND, except binds the first argument to IT (via LET) for the |
|---|
| 26 |
scope of the rest of the arguments." |
|---|
| 27 |
`(anaphoric and ,first ,@rest)) |
|---|
| 28 |
|
|---|
| 29 |
(defmacro sor (first &rest rest) |
|---|
| 30 |
"Like OR, except binds the first argument to IT (via SYMBOL-MACROLET) for |
|---|
| 31 |
the scope of the rest of the arguments. IT can be set with SETF." |
|---|
| 32 |
`(symbolic or ,first ,@rest)) |
|---|
| 33 |
|
|---|
| 34 |
(defmacro aif (test then &optional else) |
|---|
| 35 |
"Like IF, except binds the result of the test to IT (via LET) for |
|---|
| 36 |
the scope of the then and else expressions." |
|---|
| 37 |
`(anaphoric if ,test ,then ,else)) |
|---|
| 38 |
|
|---|
| 39 |
(defmacro sif (test then &optional else &environment env) |
|---|
| 40 |
"Like IF, except binds the test form to IT (via SYMBOL-MACROLET) for |
|---|
| 41 |
the scope of the then and else expressions. IT can be set with SETF" |
|---|
| 42 |
`(symbolic if ,test ,then ,else)) |
|---|
| 43 |
|
|---|
| 44 |
(defmacro asif (test then &optional else) |
|---|
| 45 |
"Like IF, except binds the result of the test to IT (via LET) for |
|---|
| 46 |
the the scope of the then-expression, and the test form to IT (via |
|---|
| 47 |
SYMBOL-MACROLET) for the scope of the else-expression. Within scope of |
|---|
| 48 |
the else-expression IT can be set with SETF." |
|---|
| 49 |
`(let ((it ,test)) |
|---|
| 50 |
(if it |
|---|
| 51 |
,then |
|---|
| 52 |
(symbolic ignore-first ,test ,else)))) |
|---|
| 53 |
|
|---|
| 54 |
(defmacro aprog1 (first &body rest) |
|---|
| 55 |
"Binds IT to the first form so that it can be used in the rest of the |
|---|
| 56 |
forms. The whole thing returns IT." |
|---|
| 57 |
`(anaphoric prog1 ,first ,@rest)) |
|---|
| 58 |
|
|---|
| 59 |
(defmacro awhen (test &body body) |
|---|
| 60 |
"Like WHEN, except binds the result of the test to IT (via LET) for the scope |
|---|
| 61 |
of the body." |
|---|
| 62 |
`(anaphoric when ,test ,@body)) |
|---|
| 63 |
|
|---|
| 64 |
(defmacro swhen (test &body body) |
|---|
| 65 |
"Like WHEN, except binds the test form to IT (via SYMBOL-MACROLET) for the |
|---|
| 66 |
scope of the body. IT can be set with SETF." |
|---|
| 67 |
`(symbolic when ,test ,@body)) |
|---|
| 68 |
|
|---|
| 69 |
(defmacro sunless (test &body body) |
|---|
| 70 |
"Like UNLESS, except binds the test form to IT (via SYMBOL-MACROLET) for the |
|---|
| 71 |
scope of the body. IT can be set with SETF." |
|---|
| 72 |
`(symbolic unless ,test ,@body)) |
|---|
| 73 |
|
|---|
| 74 |
(defmacro acase (keyform &body cases) |
|---|
| 75 |
"Like CASE, except binds the result of the keyform to IT (via LET) for the |
|---|
| 76 |
scope of the cases." |
|---|
| 77 |
`(anaphoric case ,keyform ,@cases)) |
|---|
| 78 |
|
|---|
| 79 |
(defmacro scase (keyform &body cases) |
|---|
| 80 |
"Like CASE, except binds the keyform to IT (via SYMBOL-MACROLET) for the |
|---|
| 81 |
scope of the body. IT can be set with SETF." |
|---|
| 82 |
`(symbolic case ,keyform ,@cases)) |
|---|
| 83 |
|
|---|
| 84 |
(defmacro aecase (keyform &body cases) |
|---|
| 85 |
"Like ECASE, except binds the result of the keyform to IT (via LET) for the |
|---|
| 86 |
scope of the cases." |
|---|
| 87 |
`(anaphoric ecase ,keyform ,@cases)) |
|---|
| 88 |
|
|---|
| 89 |
(defmacro secase (keyform &body cases) |
|---|
| 90 |
"Like ECASE, except binds the keyform to IT (via SYMBOL-MACROLET) for the |
|---|
| 91 |
scope of the cases. IT can be set with SETF." |
|---|
| 92 |
`(symbolic ecase ,keyform ,@cases)) |
|---|
| 93 |
|
|---|
| 94 |
(defmacro accase (keyform &body cases) |
|---|
| 95 |
"Like CCASE, except binds the result of the keyform to IT (via LET) for the |
|---|
| 96 |
scope of the cases. Unlike CCASE, the keyform/place doesn't receive new values |
|---|
| 97 |
possibly stored with STORE-VALUE restart; the new value is received by IT." |
|---|
| 98 |
`(anaphoric ccase ,keyform ,@cases)) |
|---|
| 99 |
|
|---|
| 100 |
(defmacro sccase (keyform &body cases) |
|---|
| 101 |
"Like CCASE, except binds the keyform to IT (via SYMBOL-MACROLET) for the |
|---|
| 102 |
scope of the cases. IT can be set with SETF." |
|---|
| 103 |
`(symbolic ccase ,keyform ,@cases)) |
|---|
| 104 |
|
|---|
| 105 |
(defmacro atypecase (keyform &body cases) |
|---|
| 106 |
"Like TYPECASE, except binds the result of the keyform to IT (via LET) for |
|---|
| 107 |
the scope of the cases." |
|---|
| 108 |
`(anaphoric typecase ,keyform ,@cases)) |
|---|
| 109 |
|
|---|
| 110 |
(defmacro stypecase (keyform &body cases) |
|---|
| 111 |
"Like TYPECASE, except binds the keyform to IT (via SYMBOL-MACROLET) for the |
|---|
| 112 |
scope of the cases. IT can be set with SETF." |
|---|
| 113 |
`(symbolic typecase ,keyform ,@cases)) |
|---|
| 114 |
|
|---|
| 115 |
(defmacro aetypecase (keyform &body cases) |
|---|
| 116 |
"Like ETYPECASE, except binds the result of the keyform to IT (via LET) for |
|---|
| 117 |
the scope of the cases." |
|---|
| 118 |
`(anaphoric etypecase ,keyform ,@cases)) |
|---|
| 119 |
|
|---|
| 120 |
(defmacro setypecase (keyform &body cases) |
|---|
| 121 |
"Like ETYPECASE, except binds the keyform to IT (via SYMBOL-MACROLET) for |
|---|
| 122 |
the scope of the cases. IT can be set with SETF." |
|---|
| 123 |
`(symbolic etypecase ,keyform ,@cases)) |
|---|
| 124 |
|
|---|
| 125 |
(defmacro actypecase (keyform &body cases) |
|---|
| 126 |
"Like CTYPECASE, except binds the result of the keyform to IT (via LET) for |
|---|
| 127 |
the scope of the cases. Unlike CTYPECASE, new values possible stored by the |
|---|
| 128 |
STORE-VALUE restart are not received by the keyform/place, but by IT." |
|---|
| 129 |
`(anaphoric ctypecase ,keyform ,@cases)) |
|---|
| 130 |
|
|---|
| 131 |
(defmacro sctypecase (keyform &body cases) |
|---|
| 132 |
"Like CTYPECASE, except binds the keyform to IT (via SYMBOL-MACROLET) for |
|---|
| 133 |
the scope of the cases. IT can be set with SETF." |
|---|
| 134 |
`(symbolic ctypecase ,keyform ,@cases)) |
|---|
| 135 |
|
|---|
| 136 |
(defmacro acond (&body clauses) |
|---|
| 137 |
"Like COND, except result of each test-form is bound to IT (via LET) for the |
|---|
| 138 |
scope of the corresponding clause." |
|---|
| 139 |
(labels ((rec (clauses) |
|---|
| 140 |
(if clauses |
|---|
| 141 |
(destructuring-bind ((test &body body) . rest) clauses |
|---|
| 142 |
(if body |
|---|
| 143 |
`(anaphoric if ,test (progn ,@body) ,(rec rest)) |
|---|
| 144 |
`(anaphoric if ,test it ,(rec rest)))) |
|---|
| 145 |
nil))) |
|---|
| 146 |
(rec clauses))) |
|---|
| 147 |
|
|---|
| 148 |
(defmacro scond (&body clauses) |
|---|
| 149 |
"Like COND, except each test-form is bound to IT (via SYMBOL-MACROLET) for the |
|---|
| 150 |
scope of the corresponsing clause. IT can be seet with SETF." |
|---|
| 151 |
(labels ((rec (clauses) |
|---|
| 152 |
(if clauses |
|---|
| 153 |
(destructuring-bind ((test &body body) . rest) clauses |
|---|
| 154 |
(if body |
|---|
| 155 |
`(symbolic if ,test (progn ,@body) ,(rec rest)) |
|---|
| 156 |
`(symbolic if ,test it ,(rec rest)))) |
|---|
| 157 |
nil))) |
|---|
| 158 |
(rec clauses))) |
|---|