Hi

Recently I've been writing some macros that work on functions. Generally I 
want to take a function, change something about it and then output the 
changed function. In my experience the AST for a function can vary quite a 
bit and it is only as I add more test-cases for my macros that I realize 
about new forms that are possible. As an example the AST varies a good deal 
depending on whether the function is named or anonymous and then each of 
those has a shorthand form too, so that is 4 possibilities before we get 
into type parameters, keywords args, module-names etc.

My main question is "Am I missing something here?" is there a document that 
describes all the options when declaring a function or all the possible AST 
that can be used to declare a function? Is all macro writing this tricky or 
are functions especially bed? Or is there some neat trick I'm missing to 
make this all simpler?

As I have already written two macros that work on functions and think I may 
write more I wrote a 
MetaTools<https://github.com/burrowsa/Fixtures.jl/blob/master/src/metatools.jl>module
 that allows me to reuse my AST parsing code between macros (and 
reuse a lot of the testing effort). It allows you to write macros that 
parse the AST into a standardized form (like a web browser's DOM but for 
Julia functions) manipulate it then convert it back to AST.

Here is a somewhat silly example:

macro rename_to_bob(ex::Expr)
  # Parse the function into a function object model
  pfunc = ParsedFunction(ex)

  # manipulate the function object model
  pfunc.name = :bob

  # output the result
  return esc(quote
    $(emit(pfunc))
  end)
end


A more realistic example is the @commutative macro found at the end of this 
file <https://github.com/burrowsa/Fixtures.jl/blob/master/src/metatools.jl>.

The module is pretty well 
tested<https://github.com/burrowsa/Fixtures.jl/blob/master/test/metatools.jl> 
and 
I was wondering if others would find it useful? If so I could spin it out 
as its own package.

Cheers
Andy



Reply via email to