这里是我的宏定义:未定义的变量,而unquoting宏观
*(defmacro run-test (test)
`(format t "Run test ~a ... ~a" ',test ,test))
*(run-test (= 1 1))
Run test (= 1 1) ... T
NIL
一切正常,现在,我已经定义了一个第二个宏(运行多个测试):
*(defmacro run-tests (&body body)
`(loop for tc in ',body
do (run-test tc)))
* (run-tests (= 2 (1+ 1)) (= 1 1))
Run test TC ... (= 2 (1+ 1) Run test TC ... (= 1 1)
这个结果并不是我想要的,我希望tc
的每个值都被sexp替换,并且在运行测试中评估该值。我试着用
do (run-test ,tc)
更换线
do (run-test tc)
但这发出错误信号,
未定义的变量:TC
我怎样才能改变这种获得正确的结果?
我喜欢使用'loop'来构造宏展开的可能解决方案。一般来说,手工编写至少一次代码是宏应扩展到的_desired_代码的有用方法。在这种情况下,它应该是'(progn(run-test form1)(run-test form2)...)',并且从这个角度看,'loop'显然是获得它的好方法。 –
非常好的解决方案。我再次查看了LISP宏的细节。我错了解宏观扩展阶段和运行阶段。 –