Browse Source

Loads all the functions declared in local packages

content-update
Augusto 4 years ago committed by xpetit
parent
commit
f0d1bcee39
  1. 53
      go/tests/rc/rc.go

53
go/tests/rc/rc.go

@ -8,6 +8,7 @@ import (
"go/token"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
@ -367,6 +368,58 @@ func main() {
}
}
func loadProgram(path string, functions map[string]*loadVisitor) {
l := &loadVisitor{
functions: make(map[string]ast.Node),
relImports: make(map[string]string),
fset: token.NewFileSet(),
}
pkgs, err := parser.ParseDir(l.fset, path, nil, parser.AllErrors)
if err != nil {
panic(err)
}
for _, pkg := range pkgs {
ast.Walk(l, pkg)
functions[path] = l
fmt.Println(l.relImports)
}
for _, v := range l.relImports {
if functions[v] == nil {
newPath, _ := filepath.Abs(path + "/" + v)
loadProgram(newPath, functions)
}
}
}
type loadVisitor struct {
relImports map[string]string
functions map[string]ast.Node
fset *token.FileSet
}
func (l *loadVisitor) Visit(n ast.Node) ast.Visitor {
if spec, ok := n.(*ast.ImportSpec); ok {
path, _ := strconv.Unquote(spec.Path.Value)
if isRelativeImport(path) {
var name string
if spec.Name != nil {
name = spec.Name.Name
} else {
name = filepath.Base(path)
}
l.relImports[name] = path
}
}
if decl, ok := n.(*ast.FuncDecl); ok {
l.functions[decl.Name.Name] = n
}
return l
}
func (f flags) unallowLits() {
if f.l.noLit {
for _, v := range basicLits {

Loading…
Cancel
Save