macro | since v0.0-3165 | clojure.core/macroexpand | Edit |
(macroexpand quoted)
(only intended as a REPL utility)
If the given quoted form is a macro call, expand it once, then repeat until a subsequent result is not a macro call. NOTE: nested forms are not expanded.
See macroexpand-1
if you only wish to expand a form once.
(macroexpand '(when true :foo))
;;=> (if true (do :foo))
The following goes through three expansion steps, but you can use
macroexpand-1
to do one at a time instead.
(macroexpand '(-> 2 inc))
;;=> (js* "(~{} + ~{})" 2 1)
Notice how the nested inc
form is not expanded:
(macroexpand '(inc (inc 2)))
;;=> (js* "(~{} + ~{})" (inc 2) 1)
Repeatedly calls macroexpand-1 on form until it no longer represents a macro form, then returns it. Note neither macroexpand-1 nor macroexpand expand macros in subforms.
(core/defmacro macroexpand
[quoted]
(core/assert (core/= (core/first quoted) 'quote)
"Argument to macroexpand must be quoted")
(core/let [form (second quoted)
env &env]
(if (seq? form)
(core/loop [form form form' (ana/macroexpand-1 env form)]
(core/if-not (core/identical? form form')
(recur form' (ana/macroexpand-1 env form'))
`(quote ~form')))
form)))