Comments (4)
这是需要自己定义的属性,完整的代码如下:
package main
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"os"
"golang.org/x/tools/go/ssa"
)
func main() {
prog := NewProgram(map[string]string{
"main": `
package main
func main() {
for i := 0; i < 3; i++ {
println(i, "hello chai2010")
}
}
`,
})
pkg, f, info, _ := prog.LoadPackage("main")
var ssaProg = ssa.NewProgram(prog.fset, ssa.SanityCheckFunctions)
var ssaPkg = ssaProg.CreatePackage(pkg, []*ast.File{f}, info, true)
ssaPkg.Build()
ssaPkg.WriteTo(os.Stdout)
ssaPkg.Func("main").WriteTo(os.Stdout)
}
type Program struct {
fs map[string]string
ast map[string]*ast.File
pkgs map[string]*types.Package
infos map[string]*types.Info
fset *token.FileSet
}
func NewProgram(fs map[string]string) *Program {
return &Program{
fs: fs,
ast: make(map[string]*ast.File),
pkgs: make(map[string]*types.Package),
infos: make(map[string]*types.Info),
fset: token.NewFileSet(),
}
}
func (p *Program) LoadPackage(path string) (pkg *types.Package, f *ast.File, info *types.Info, err error) {
if pkg, ok := p.pkgs[path]; ok {
return pkg, p.ast[path], p.infos[path], nil
}
f, err = parser.ParseFile(p.fset, path, p.fs[path], parser.AllErrors)
if err != nil {
return nil, nil, nil, err
}
info = &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
}
conf := types.Config{Importer: p}
pkg, err = conf.Check(path, p.fset, []*ast.File{f}, info)
if err != nil {
return nil, nil, nil, err
}
p.ast[path] = f
p.pkgs[path] = pkg
p.infos[path] = info
return pkg, f, info, nil
}
func (p *Program) Import(path string) (*types.Package, error) {
if pkg, ok := p.pkgs[path]; ok {
return pkg, nil
}
pkg, _, _, err := p.LoadPackage(path)
return pkg, err
}
from go-ast-book.
我看书里后面有 exitCode := interp.Interpret(ssaPkg, 0, &types.StdSizes{8, 8}, "main", []string{})
if exitCode != 0 {
fmt.Println("exitCode:", exitCode)
}
这个放在后面执行会报错
panic: ssa.Program doesn't include runtime package
from go-ast-book.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/interp"
)
func main() {
prog := NewProgram(map[string]string{
"main": `
package main
func main() {
for i := 0; i < 3; i++ {
println(i, "hello chai2010")
}
}
`,
"runtime": `
package runtime
type errorString string
func (e errorString) RuntimeError() {}
func (e errorString) Error() string { return "runtime error: " + string(e) }
type Error interface {
error
RuntimeError()
}
`,
})
prog.LoadPackage("main")
prog.LoadPackage("runtime")
var ssaProg = ssa.NewProgram(prog.fset, ssa.SanityCheckFunctions)
var ssaMainPkg *ssa.Package
for name, pkg := range prog.pkgs {
ssaPkg := ssaProg.CreatePackage(pkg, []*ast.File{prog.ast[name]}, prog.infos[name], true)
if name == "main" {
ssaMainPkg = ssaPkg
}
}
ssaProg.Build()
exitCode := interp.Interpret(
ssaMainPkg, 0, &types.StdSizes{8, 8},
"main", []string{},
)
if exitCode != 0 {
fmt.Println("exitCode:", exitCode)
}
}
type Program struct {
fs map[string]string
ast map[string]*ast.File
pkgs map[string]*types.Package
infos map[string]*types.Info
fset *token.FileSet
}
func NewProgram(fs map[string]string) *Program {
return &Program{
fs: fs,
ast: make(map[string]*ast.File),
pkgs: make(map[string]*types.Package),
infos: make(map[string]*types.Info),
fset: token.NewFileSet(),
}
}
func (p *Program) LoadPackage(path string) (pkg *types.Package, f *ast.File, err error) {
if pkg, ok := p.pkgs[path]; ok {
return pkg, p.ast[path], nil
}
f, err = parser.ParseFile(p.fset, path, p.fs[path], parser.AllErrors)
if err != nil {
return nil, nil, err
}
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
}
conf := types.Config{Importer: p}
pkg, err = conf.Check(path, p.fset, []*ast.File{f}, info)
if err != nil {
return nil, nil, err
}
p.ast[path] = f
p.pkgs[path] = pkg
p.infos[path] = info
return pkg, f, nil
}
func (p *Program) Import(path string) (*types.Package, error) {
if pkg, ok := p.pkgs[path]; ok {
return pkg, nil
}
pkg, _, err := p.LoadPackage(path)
return pkg, err
}
from go-ast-book.
代码在每章的 examples 目录:https://github.com/chai2010/go-ast-book/tree/master/ch13/examples
from go-ast-book.
Related Issues (8)
- 12章之后就没有了 HOT 11
- 想问问您,实体书什么时候出版 HOT 3
- 5.5按组声明图片错误
- 请问如何下载?已满足条件
- 在第一章1.3节关于FileSet和File的描述的疑问 HOT 1
- 章节1.3 FileSet和File的描述让人感到困惑 HOT 1
- wa语言不支持导入外部包
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-ast-book.