Macros and how to trace them

前端 未结 3 1932
迷失自我
迷失自我 2021-01-25 23:10

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)
         


        
3条回答
  •  难免孤独
    2021-01-25 23:43

    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.

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

    CLISP can trace macros:

    [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 is another implementation which supports tracing of macros.

    So, what is the reason for not having any facility for tracing macros in Common 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.

    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.

提交回复
热议问题