Macros and how to trace them

前端 未结 3 1894
迷失自我
迷失自我 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:30

    Using trace on a macro seems a little odd, but it works in CLISP:

    (trace push)
    (defparameter *stack* '())
    
    (defun push-xy (x y)
      (push x *stack*)
      (push y *stack*))
    ; 1. Trace: (push x *stack*)
    ; 1. Trace: push ==> (setq *stack* (cons x *stack*))
    ; 1. Trace: (push y *stack*)
    ; 1. Trace: push ==> (setq *stack* (cons y *stack*))
    ; ==> push-xy
    

    The standard does not say when it should expand macros so this might happen when functions and lambdas are defined, compiled and sometimes called. Some implementations run the macro twice so you get double the output.

    I never use this. I rather use macroexpand-1:

    (macroexpand-1 '(push x *stack)))
    ; ==> (setq *stack (cons x *stack))
    ; ==> t
    

    If your form returns a new form that uses macros you might want to try macroexpand instead. Its like calling macroexpand-1 over and over until there are no transformation left.

提交回复
热议问题