-
Notifications
You must be signed in to change notification settings - Fork 690
Closed
Description
I have parsed lua to AST, How to revert to lua source?
Lua
function Main(a, b, c)
print("Hello Lua")
return "OK"
end
Go
func Test_parse_ast(t *testing.T) {
script := "parser_test.lua"
file, err := os.Open(script)
if err != nil {
t.Fatal(err)
return
}
chunk, err2 := parse.Parse(file, script)
if err2 != nil {
t.Fatal(err2)
return
}
for _, ck := range chunk {
Type := reflect.TypeOf(ck)
t.Log(Type)
}
HelloFunc := &ast.FuncDefStmt{
Name: &ast.FuncName{
Func: &ast.IdentExpr{
Value: "HelloFunc",
},
Receiver: nil,
Method: "",
},
Func: &ast.FunctionExpr{
ParList: &ast.ParList{
Names: []string{"a1", "a2"},
},
Stmts: []ast.Stmt{&ast.ReturnStmt{
Exprs: []ast.Expr{
&ast.StringExpr{Value: "OK2"},
&ast.StringExpr{Value: "OK3"},
},
}},
},
}
chunk = append(chunk, HelloFunc)
t.Log(parse.Dump(chunk))
}Output
➜ gopher-lua git:(master) ✗ go test
=== RUN Test_parse_ast
parser_test.go:27: *ast.FuncDefStmt
parser_test.go:50: - Node$FuncDefStmt
Name:
- Node$FuncName
Func:
- Node$IdentExpr: Main
Receiver:
<nil>
Method:
Func:
- Node$FunctionExpr
ParList:
- Node$ParList
HasVargs: false
Names:
a
b
c
Stmts:
- Node$FuncCallStmt
Expr:
- Node$FuncCallExpr
Func:
- Node$IdentExpr: print
Receiver:
<nil>
Method:
Args:
- Node$StringExpr: Hello Lua
AdjustRet: false
- Node$ReturnStmt
Exprs:
- Node$StringExpr: OK
- Node$FuncDefStmt
Name:
- Node$FuncName
Func:
- Node$IdentExpr: HelloFunc
Receiver:
<nil>
Method:
Func:
- Node$FunctionExpr
ParList:
- Node$ParList
HasVargs: false
Names:
a1
a2
Stmts:
- Node$ReturnStmt
Exprs:
- Node$StringExpr: OK2
- Node$StringExpr: OK3
--- PASS: Test_parse_ast (0.00s)
PASS
ok github.com/i4de/gopher-lua 0.001s
So I Expect have a function which named like: parse_chunk_to_source([]ast.Stmt) string
Metadata
Metadata
Assignees
Labels
No labels