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.
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
the instrument call within the macro should be expanded to an expr within the macro expansion time.
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.
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.
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.