diff options
| author | Pietro Gagliardi <[email protected]> | 2014-05-25 00:31:40 -0400 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2014-05-25 00:31:40 -0400 |
| commit | 4632d8057696025577a165b797932b828caade32 (patch) | |
| tree | d15e60ac0d55f8d83a7bc3e51e89d559e828a9b4 /experiments/windowsconstgen.go | |
| parent | 20306d881cc8143a4a29abb3c179270441ab53af (diff) | |
More work on the constant generation tool.
Diffstat (limited to 'experiments/windowsconstgen.go')
| -rw-r--r-- | experiments/windowsconstgen.go | 65 |
1 files changed, 25 insertions, 40 deletions
diff --git a/experiments/windowsconstgen.go b/experiments/windowsconstgen.go index bfbeac4..ed9561e 100644 --- a/experiments/windowsconstgen.go +++ b/experiments/windowsconstgen.go @@ -32,52 +32,37 @@ func main() { pkg = pkgs[k] } - var run func(...ast.Decl) - var runstmt func(ast.Stmt) - var runblock func(*ast.BlockStmt) + do(pkg) +} - desired := func(name string) bool { - return strings.HasPrefix(name, "_") - } - run = func(decls ...ast.Decl) { - for _, d := range decls { - switch dd := d.(type) { - case *ast.FuncDecl: - runblock(dd.Body) - case *ast.GenDecl: - if desired(d.Name.String()) { - fmt.Println(d.Name.String()) - } - default: - panic(fmt.Errorf("unknown decl type %T: %v", dd, dd)) +type walker struct { + desired func(string) bool +} + +func (w *walker) Visit(node ast.Node) ast.Visitor { + if n, ok := node.(*ast.Ident); ok { + if w.desired(n.Name) { + known := "<unknown>" + if n.Obj != nil { + known = n.Obj.Kind.String() } + fmt.Println(n.Name + "\t\t" + known) } } - runstmt = func(s ast.Stmt) { - switch ss := s.(type) { - case *ast.DeclStmt: - run(ss.Decl) - case *ast.LabeledStmt: - runstmt(ss.Stmt) - case *ast.AssignStmt: - // TODO go through Lhs if ss.Tok type == DEFINE - case *ast.GoStmt: - // these don't have decls - case *ast.EmptyStmt: - case *ast.ExprStmt: - case *ast.SendStmt: - case *ast.IncDecStmt: - // all do nothing - default: - panic(fmt.Errorf("unknown stmt type %T: %v", dd, dd)) - } - } - runblock = func(block *ast.BlockStmt) { - for _, s := range block.Stmt { - runstmt(s) + return w +} + +func do(pkg *ast.Package) { + desired := func(name string) bool { + if strings.HasPrefix(name, "_") && len(name) > 1 { + return !strings.ContainsAny(name, + "abcdefghijklmnopqrstuvwxyz") } + return false } for _, f := range pkg.Files { - run(f.Decls...) + for _, d := range f.Decls { + ast.Walk(&walker{desired}, d) + } } } |
