Beside having to parse the script file.
It looks I can call “code_lowered” on the function and get a CodeInfo, from which I can find the statements of the function. Is that a reliable way?
I don’t know a way to get the code of a function as String
. But there are plenty of ways to do similar things:
- As you mentioned the
@code_...
macros give you the code after some compiler processing -
@which
gives you a link to the source location -
@less
shows you the code -
@edit
opens the code in your favorite editor
Rebugger.jl does this (somehow).
You wouldn’t recommend using CodeIndo?
CodeInfo is the type that is returned by eg @code_llvm.
So, what exactly are you saying?
Yes code_lowered is a good option, but as its same suggests it returns a lower-level representation of the function, not the source code as it is written.
Try Espresso.funexpr() or MacroTools.macro_form() which Espresso calls internally. They try to find source file of a functions first, and when it’s not available, reconstruct source from lowered code. For me it works in 85-95% of cases, failing mostly on non-trivially defined functions like the ones generated from macros.