问题描述
我的代码库中大约有两个宏(和攀爬宏),如下所示:
I have about two macros (and climbing) in my codebase that look like this:
(defmacro def-stat-method (method-name stat)
`(progn
(defmethod ,method-name ((monster monster))
(getf (stats monster) ,stat))
(defmethod (setf ,method-name) (value (monster monster))
(setf (getf (stats monster) ,stat) value))))
和这个:
(defmacro def-energy-method (method-name stat)
`(progn
(defmethod ,method-name ((monster monster))
(getf (energies monster) ,stat))
(defmethod (setf ,method-name) (value (monster monster))
(setf (getf (energies monster) ,stat) value))))
每个宏具有以下调用语法:(def-stat-method ranged-weapon :ranged-weapon)
Each of the macros has the following calling syntax: (def-stat-method ranged-weapon :ranged-weapon)
我想将宏(def-foo-method macro-name method)
扩展为适当的宏,以便(def-foo-method def-stat-method stats)
扩展为上述第一个示例.我是个新手,不知道怎么做.感谢所有帮助.
I would like a macro (def-foo-method macro-name method)
that expands into the appropriate macros so that (def-foo-method def-stat-method stats)
expands into the first example above. I am a lisp newbie and don't really know how to do this. All help appreciated.
推荐答案
只需编写一个扩展为另一个defmacro的宏,如下所示:
Just write a macro that expands into another defmacro, like this:
(defmacro def-foo-method (macro-name method)
`(defmacro ,macro-name (method-name stat)
(let ((method ',method))
`(progn
(defmethod ,method-name ((monster monster))
(getf (,method monster) ,stat))
(defmethod (setf ,method-name) (value (monster monster))
(setf (getf (,method monster) ,stat) value))))))
我认为没有let
可能会有一种方法,但是嵌套的反引号令人困惑:)
I think there might be a way to do this without the let
, but nested backticks are confusing :)
这篇关于如何在通用Lisp中编写宏定义宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!