What I would like to do is use macros and a nested structure such as the let's in the third example, below, and have the macros read the *b* var as set by the let's. Is there an elegant way to do this? I suppose I could roll my own nested construct to do this relying on a stack and setf, but I was wondering if there is something part of lisp that already does this. I am also curious as to why lisp behaves differently in regards to setf vs let in association to macros (but this isn't really that important)
Thanks in advance.
Code:
Code: Select all
(defvar *b*)
(defmacro foo-mac (name)
`(format t "mac ~A: ~S~%" ,name ',*b*)
)
(defun foo-fun (name)
(format t "fun ~A: ~S~%" name *b*)
)
;;works as expected
(setf *b* 'x)
(foo-mac "setf")
(foo-fun "setf")
;;*b* isn't set for macro foo-mac, but is for function foo-fun
(let ((*b* 'b))
(foo-mac "let")
(foo-fun "let")
)
;;what I really want to do is set vars in a nested way, such as this:
(let ((*b* 'b1))
(foo-mac "let1")
(foo-fun "let1")
(let ((*b* 'b2))
(foo-mac "let2")
(foo-fun "let2")
)
(foo-mac "let1")
(foo-fun "let1")
)
Code: Select all
mac setf: X
fun setf: X
mac let: X
fun let: B
mac let1: X
fun let1: B1
mac let2: X
fun let2: B2
mac let1: X
fun let1: B1