宏以及如何跟踪它们 [英] Macros and how to trace them

查看:145
本文介绍了宏以及如何跟踪它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

trace 宏对于调试非常有用.但它涉及到一个停顿,在任何宏观使用时.就像如果我尝试做以下内容:

The trace macro is very useful for debugging. But it comes to a halt, when used upon any macro. Like if I try to do the following :

CL-USER> (trace push)

然后,它会给出一个错误说:

Then, it'll give an error saying:

can't use encapsulation to trace anonymous function #<FUNCTION (MACRO-FUNCTION
                                                                PUSH) {100053FB9B}>
   [Condition of type SIMPLE-ERROR]

好吧,这很明显,因为 trace 的clhs页面明确定义了它在功能上.那么,在Common Lisp中没有任何用于跟踪宏的功能的原因是什么?
还有其他(非常规)跟踪Common Lisp中的宏的方法吗?

Well, that's obvious because the clhs page of trace, clearly defines it upon functions. So, what is the reason for not having any facility for tracing macros in Common Lisp?
Is there any other (unconventional) way to trace macros in Common Lisp?

推荐答案

Common Lisp标准仅提及函数的跟踪.在已编译的实现中,宏扩展通常在编译时进行,因此通常不支持对宏进行跟踪.

The Common Lisp standard only mentions tracing of functions. In compiled implementations, macro expansion usually takes place at compile time and thus tracing of macros is usually not supported.

但是某些Common Lisp实现可以通过Lisp解释器(!)跟踪宏:

But some Common Lisp implementations can trace macros via a Lisp interpreter (!):

CLISP可以跟踪宏:

[1]> (defmacro foo (a) a)
FOO
[2]> (trace foo)
;; Tracing macro FOO.
(FOO)
[3]> (loop for i below 4 collect (foo i))
1. Trace: (FOO I)
1. Trace: FOO ==> I
1. Trace: (FOO I)
1. Trace: FOO ==> I
1. Trace: (FOO I)
1. Trace: FOO ==> I
1. Trace: (FOO I)
1. Trace: FOO ==> I
(0 1 2 3)

LispWorks是另一个支持宏跟踪的实现.

LispWorks is another implementation which supports tracing of macros.

那么,为什么没有在Common Lisp中跟踪宏的功能的原因是什么?

So, what is the reason for not having any facility for tracing macros in Common Lisp?

如上所述,这是语言标准.除了语言标准实现以外,还以各种方式提供各种语言扩展,包括某些Lisp解释器(!)跟踪宏的能力.

As mentioned that's the language standard. Beyond the language standard implementations provide all kinds of language extensions in various ways, including the ability of some Lisp interpreters (!) to trace macros.

如果代码已经编译,则跟踪将无法进行.拥有Lisp解释器会有所帮助,但是并不需要具有解释器的实现. Lisp解释器在这里是指一个执行引擎,它以Lisp代码作为数据工作.

If the code is already compiled, tracing won't work anyway. Having a Lisp interpreter helps, but implementations are not required to have an interpreter. Lisp interpreter here means an executing engine which works from Lisp code as data.

这篇关于宏以及如何跟踪它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆