-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: add a function to directly compile Go AST
Adds CompileAST, which can be used to compile Go AST directly. This allows users to delegate parsing of source to their own code instead of relying on the interpreter. CLoses #1251
- Loading branch information
1 parent
c5c6012
commit 808f0bd
Showing
5 changed files
with
136 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package interp | ||
|
||
import ( | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"testing" | ||
|
||
"github.com/traefik/yaegi/stdlib" | ||
) | ||
|
||
func TestCompileAST(t *testing.T) { | ||
file, err := parser.ParseFile(token.NewFileSet(), "_.go", ` | ||
package main | ||
import "fmt" | ||
type Foo struct{} | ||
var foo Foo | ||
const bar = "asdf" | ||
func main() { | ||
fmt.Println(1) | ||
} | ||
`, 0) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if len(file.Imports) != 1 || len(file.Decls) != 5 { | ||
panic("wrong number of imports or decls") | ||
} | ||
|
||
dType := file.Decls[1].(*ast.GenDecl) | ||
dVar := file.Decls[2].(*ast.GenDecl) | ||
dConst := file.Decls[3].(*ast.GenDecl) | ||
dFunc := file.Decls[4].(*ast.FuncDecl) | ||
|
||
if dType.Tok != token.TYPE { | ||
panic("decl[1] is not a type") | ||
} | ||
if dVar.Tok != token.VAR { | ||
panic("decl[2] is not a var") | ||
} | ||
if dConst.Tok != token.CONST { | ||
panic("decl[3] is not a const") | ||
} | ||
|
||
cases := []struct { | ||
desc string | ||
node ast.Node | ||
skip string | ||
}{ | ||
{desc: "file", node: file}, | ||
{desc: "import", node: file.Imports[0]}, | ||
{desc: "type", node: dType}, | ||
{desc: "var", node: dVar, skip: "not supported"}, | ||
{desc: "const", node: dConst}, | ||
{desc: "func", node: dFunc}, | ||
{desc: "block", node: dFunc.Body}, | ||
{desc: "expr", node: dFunc.Body.List[0]}, | ||
} | ||
|
||
i := New(Options{}) | ||
_ = i.Use(stdlib.Symbols) | ||
|
||
for _, c := range cases { | ||
t.Run(c.desc, func(t *testing.T) { | ||
if c.skip != "" { | ||
t.Skip(c.skip) | ||
} | ||
|
||
i := i | ||
if _, ok := c.node.(*ast.File); ok { | ||
i = New(Options{}) | ||
_ = i.Use(stdlib.Symbols) | ||
} | ||
_, err := i.CompileAST(c.node) | ||
if err != nil { | ||
t.Fatalf("Failed to compile %s: %v", c.desc, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters