Emacs 为什么不是';t在let形式的词汇上下文中计算此形式

Emacs 为什么不是';t在let形式的词汇上下文中计算此形式,emacs,elisp,lexical-scope,Emacs,Elisp,Lexical Scope,我试图创建一个宏,该宏创建一个函数,该函数接受S-expresions并在fixture的词法上下文中对其求值。这是我写的宏: (defmacro def-fixture (name bindings) "Return a function that takes the form to execute but is wrapped between a let of the bindings" `(defun ,(intern (symbol-name name)) (body)

我试图创建一个宏,该宏创建一个函数,该函数接受S-expresions并在fixture的词法上下文中对其求值。这是我写的宏:

(defmacro def-fixture (name bindings)
  "Return a function that takes the form to execute but is wrapped between a let of the bindings"
  `(defun ,(intern (symbol-name name)) (body)
     (let (,bindings)
       (unwind-protect
           (progn
             body)))))
但当我运行它时,它似乎在我提供的词法上下文之外执行

(def-fixture test-fixture '(zxvf 1))

(test-fixture '(= zxvf 1))
let: Symbol's value as variable is void: zxvf

顺便说一句,我已经启用了变量词法绑定。关于我的错误有什么想法吗?

以下注释在

此外,defun或defmacro主体中的代码不能引用 周围的词汇变量

这可能是你的问题所在


另外,我不知道您是否需要引用
def fixture
的第二个参数。我使用了
macrostep
包来检查生成的宏,没有引号,结果似乎更好。

这与词法范围无关。宏调用扩展为:

(defun test-fixture (body)
  (let ((quote (zxvf 1)))
    (unwind-protect (progn body))))
这当然不是你想要的。我不相信
(测试夹具“(=zxvf 1))
表示您引用的错误(即
变量无效
)。相反,调用发出
(无效函数zxvf)
,因为它试图计算
(zxvf 1)
(=zxvf 1)
表达式从不求值,因为它被引用了

您可能想尝试一些更像:

(defmacro def-fixture (name bindings)
  "Return a macro that takes the form to execute but is wrapped between a let of the bindings"
  `(defmacro ,name (body)
     `(let (,',bindings)
        (unwind-protect
          (progn
            ,body)))))
然后将其用作:

(def-fixture test-fixture (zxvf 1))
(test-fixture (= zxvf 1))

谢谢我“”尝试返回lambda,然后将宏的返回表达式绑定到变量。我看到您正在使用嵌套的反引号,我想知道这是否是所需的。谢谢