Welcome to Expanders!! :D
This project lets you define expanders. An expander is like a namespace for forms that can be expanded by a macro.
It is easier to understand with an example. Suppose we have the following form (op (+ 3 4) b). This form will be used in two different macros named plus-macro and minus-macro. Each macro receives a form. That form could start with op. In that case, op is substituted by + or - respectively.
Let's define 2 different expanders:
;; We assume (use-package #:expanders)
(defvar plus-expander (make-expander))
(defvar minus-expander (make-expander));; Returns
MINUS-EXPANDERNow we have two different expanders. The function exp:expanderp can tell us if a symbol denotes an expander:
(expanderp plus-expander);; ReturnsT(expanderp 'another-thing);; ReturnsNIL(expanderp minus-expander);; ReturnsTNow it is time to define the expansion op for each expander using exp:defexpansion:
(defexpansion plus-expander op (a b)
"OP to + expansion"`(+,a ,b))
(defexpansion minus-expander op (a b)
"OP to - expansion"`(-,a ,b));; Returns
OPWe can check if a symbol is an expansion for a given expander using exp:expansionp:
(expansionp plus-expander 'op);; ReturnsT(expansionp plus-expander 'hey);; ReturnsNIL(expansionp minus-expander 'op);; ReturnsTAlso, we can retrieve or set the docstring using documentation:
(documentation'op plus-expander);; Returns"OP to + expansion"(let ((old-docstring (documentation'op minus-expander)))
(setf (documentation'op minus-expander) "Another docstring")
(let ((new-docstring (documentation'op minus-expander)))
(formatt"Old: ~s~%New: ~s" old-docstring new-docstring)));; Output
Old: "OP to - expansion"
New: "Another docstring"
;; ReturnsNILWe can expand an expansion using exp:expand:
(expand plus-expander '(op 3 (+56)));; Returns
(+3 (+56))(expand minus-expander '(op 3 (+56)));; Returns
(-3 (+56))Finally, let's define the macros plus-macro and minus-macro:
(defmacroplus-macro (form)
(expand plus-expander form));; Returns
PLUS-MACRO(defmacrominus-macro (form)
(expand minus-expander form));; Returns
MINUS-MACROIf we use the form (op 5 4) we will see that each macro will expand to (+ 5 4) or (- 5 4) respectively.
(plus-macro (op 54));; Returns9(minus-macro (op 54));; Returns1- It is common: I have noticed that having expanders is a relatively common pattern in macros. The best example is
setfand itssetf-expanders. Another project using expanders is CFFI and its type parsers. In my own projects I ended up using the same techniques (Clith). - Duality of syntax: We can increase the duality of syntax using expanders. The best example is
setf. Thanks tosetfwe don't need names for setters because they come for free from getters.
Define an expansion named NAME for EXPANDER. Arguments are specified in a macro-lambda-list where
&whole and &environment can be used.
Expand an EXPANSION from EXPANDER. An environment object can be supplied.
Check if an object is an expander
Retrieve the expansion function named NAME from EXPANDER. Return NIL if that function does not exist.
Make an expander