Say I have a function:
function hi()
print('hi')
end
I'd like to be able to take this function as an argument and perform analysis on it, such as listing all the function calls in it, seeing all the variables it references, etc.
Note that I'm not talking about analysing source code; I don't have the source text of the function, I just have the function object, i.e. this is being done at runtime.
The way I think you'd go about doing this is to get the AST of the function. But I don't know how to do that. So how would I go about getting the AST of a function (or otherwise analysing it if there's a better way)?
Abstract Syntax Trees are (pretty much by definition) a source-level concept.
So what you are perhaps looking for in a non-reflective compiled language would be a "decompiler"
Would this work?
http://luadec.luaforge.net/
You may want to try my lbci, a bytecode inspector library.
I have to ask. Why don't you have the source? Was is obfuscated in some way?
Anyhow as mentioned, the AST - even though there really wasn't one to begin with in Lua, is not available via any runtime mechanism.
Though it is quite easy to obtain access to the source in many cases as it is being fed into the interpreter embedded or not (unless of course they only ship bytecode...