Getting the AST of a function

2019-05-07 05:40发布

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)?

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-07 05:54

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/

查看更多
Fickle 薄情
3楼-- · 2019-05-07 05:58

You may want to try my lbci, a bytecode inspector library.

查看更多
女痞
4楼-- · 2019-05-07 06:05

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...

查看更多
登录 后发表回答