Macro Question / Type Problem

    function instrument(f1::EvaluatorFunction, f2::TrapFunction, ex::Expr)
      # returns an expr after modifying ex 
   end
    function instrument(trap_fun::TrapFunction, ex::Expr ;amod = current_module())
        instrument(EvaluatorFunction(amod.eval),trap_fun,ex)
    end

    function instrument(eval_fun::EvaluatorFunction,ex::Expr)
        trapless = Expr(:block, :(:T{false}) , ex)
        instrument(eval_fun,TrapFunction(deftrap),trapless)
    end

  macro  instrument(fun,ex)
      esc(quote
            instrument($fun,$ex)
         end)
  end

#Example usage
@instrument eval function f(x)
 x
end

this code doesn’t work, because in the macro, ex is being passed as a function, not an Expr. is there way to pass ex as an Expr not an Function.

Is

  macro  instrument(fun,ex)
      esc(quote
            instrument($fun,$(QuoteNode(ex))
         end)
  end

what you want?

Hi,

I think what I am looking for is a way to dispatch on the macro argument fun.
I also realized the instrument should return an expr and evaluated so macro code should look something like

  macro  instrument(fun,ex)
      esc(quote
            $(instrument($fun,$(QuoteNode(ex)))
         end)
  end

But this doesn’t work either.

Thanks.

It’s unclear from what you discribed,

  1. When do you need instrument to execute (runtime or macro expansion time)
  2. When do you want the “eval_fun” to execute and what do you want it to do. In fact, you are likely using it incorrectly.
  1. the instrument call within the macro should be expanded to an expr within the macro expansion time.
  2. the argument fun in the macro can be TrapFunction or EvaluatorFunction , and I want to dispatch to correct instrument function using the type of the fun argument.
  3. eval_fun function will be executed in runtime after macro expansion. The eval function is used to executing the expr injected into the expression at runtime.
#Example usage
@instrument evaluator function f(x)
 x
end

evaluator is a EvaluatorFunction type carrying a function confirming to type signature (amod::Module,ex) like eval.

Thanks

Then you should just do instrument(...) or esc(instrument(...)) in the macro.

That’s impossible.

That’s likely wrong. The execution of expression at runtime shouldn’t need a “eval function”. The macro return value will be executed and wrapping it in eval is almost garantee to be wrong.

care to explain why?

Thanks

That information is not available to the macro.