On Sun, Mar 20, 2016 at 8:26 PM, <vish...@stanford.edu> wrote: > Hi all, > > I'd like to be able to load in a module, then macroexpand the whole thing, > then print out the macroexpanded version. > > This should be a full, recursive macroexpand. > > I've noticed there is a function called macroexpand that normally does what > i want: > >> macro m(x) 1 end > .. >> @m(2) > 1 >> macroexpand(:(1 + @m(2))) > :(1 + 1) > > so that is fine and dandy, but inside a module this doesn't seem to work: >> macroexpand(:( > module M > macro m(x) 1 end > x = 1 + @m(2) > end > )) > > :(module M > eval(x) = begin # none, line 2: > top(Core).eval(M,x) > end > eval(m,x) = begin # none, line 2: > top(Core).eval(m,x) > end # none, line 3: > $(Expr(:macro, :(m(x)), quote # none, line 3: > 1 > end)) # none, line 4: > x = 1 + @m(2) > end) > > > As you can see in the second to last line, @m(2) is not expanded, and I'm > confused as to why that is. > > Ideally, this macroexpanding of a module would allow me to also resolve > imports and includes properly, so I could just slurp up a file and dump out > the macroexpanded version.
TL;DR this is generally not possible without evaluating the whole module. Macros are executed at parse time and therefore resolved in global scope (since local scope doesn't even exist yet) or in another word module scope. Therefore when doing macro expansion in a new module, the macros needs to be resolved in the new module and since there's no way to statically know what macros are available in a module you can't do that without evaluating the module. > > Thank you! > > Vishesh