On Monday, March 21, 2016 09:34:19 AM Stefan Karpinski wrote:
> Tim, I'm assuming that module must assume that no macros are defined *and*
> then used within the module body. If that does occur, the only way to do
> macro expansion correctly is to evaluate the module since the module
> definition can depend on arbitrary previously evaluated code.

Probably true. I haven't played with it in a long time, but it's possible you 
could load the module (so the macros are defined) and then parse the file...but 
I can't remember if that works.

Best,
--Tim

> 
> On Sun, Mar 20, 2016 at 9:00 PM, Tim Holy <[email protected]> wrote:
> > It probably needs updating, but
> > https://github.com/timholy/MacroExpandJL.jl
> > might help. It lets you macroexpand a whole source file.
> > 
> > Best,
> > --Tim
> > 
> > On Sunday, March 20, 2016 08:53:49 PM Yichao Yu wrote:
> > > On Sun, Mar 20, 2016 at 8:26 PM,  <[email protected]> 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

Reply via email to