Skip to content

Instantly share code, notes, and snippets.

@kingcons
Created October 3, 2013 16:39
Show Gist options
  • Save kingcons/6812896 to your computer and use it in GitHub Desktop.
Save kingcons/6812896 to your computer and use it in GitHub Desktop.
(defmacro count-macro-expansions (&body body)
"Count the number of macro expansions required to execute
BODY. Return two values: the result of executing BODY and the
number of expansions."
(let ((closures (gensym "CLOSURES-")))
`(let* ((,closures
(funcall
(compile nil
`(lambda ()
(let ((count 0))
(cons (lambda (expander form env)
(incf count)
(funcall expander form env))
(lambda () count)))))))
(*macroexpand-hook* (car ,closures)))
(values (progn ,@body)
(funcall (cdr ,closures))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment