slightly less verbose runtime AST code

And rewrite a loop to be a bit simpler.
pull/54/head
Daniel Martí 5 years ago
parent 7ede37cc0b
commit b171463a47

@ -48,21 +48,18 @@ func addRuntimeAPI(filename string, file *ast.File) {
// the runtime.hex values in a way not consistent with normal panic // the runtime.hex values in a way not consistent with normal panic
// outputs // outputs
for _, decl := range file.Decls { for _, decl := range file.Decls {
funcDecl, ok := decl.(*ast.FuncDecl) decl, ok := decl.(*ast.FuncDecl)
if ok && funcDecl.Name.Name == "printany" { if !ok || decl.Name.Name != "printany" {
for _, stmt := range funcDecl.Body.List { continue
switchStmt, ok := stmt.(*ast.TypeSwitchStmt) }
if !ok { for _, stmt := range decl.Body.List {
continue if stmt, ok := stmt.(*ast.TypeSwitchStmt); ok {
} stmt.Body.List = append(stmt.Body.List, printanyHexCase)
switchStmt.Body.List = append(switchStmt.Body.List, printanyHexCase)
break break
} }
funcDecl.Body.List = append([]ast.Stmt{fatalErrorsHiddenCheckStmt}, funcDecl.Body.List...)
break
} }
decl.Body.List = append([]ast.Stmt{fatalErrorsHiddenCheckStmt}, decl.Body.List...)
break
} }
file.Decls = append(file.Decls, panicprintDecl) file.Decls = append(file.Decls, panicprintDecl)
@ -83,74 +80,48 @@ var fatalErrorsHiddenCheckStmt = &ast.IfStmt{
var hideFatalErrorsDecls = []ast.Decl{ var hideFatalErrorsDecls = []ast.Decl{
&ast.GenDecl{ &ast.GenDecl{
Tok: token.VAR, Tok: token.VAR,
Specs: []ast.Spec{ Specs: []ast.Spec{&ast.ValueSpec{
&ast.ValueSpec{ Names: []*ast.Ident{{Name: "fatalErrorsHidden"}},
Names: []*ast.Ident{ Type: &ast.Ident{Name: "bool"},
{Name: "fatalErrorsHidden"}, }},
},
Type: &ast.Ident{Name: "bool"},
},
},
}, },
&ast.FuncDecl{ &ast.FuncDecl{
Name: &ast.Ident{Name: "hideFatalErrors"}, Name: &ast.Ident{Name: "hideFatalErrors"},
Type: &ast.FuncType{ Type: &ast.FuncType{Params: &ast.FieldList{
Params: &ast.FieldList{ List: []*ast.Field{{
List: []*ast.Field{ Names: []*ast.Ident{{Name: "hide"}},
{ Type: &ast.Ident{Name: "bool"},
Names: []*ast.Ident{ }},
{Name: "hide"}, }},
}, Body: &ast.BlockStmt{List: []ast.Stmt{
Type: &ast.Ident{Name: "bool"}, &ast.AssignStmt{
}, Lhs: []ast.Expr{&ast.Ident{Name: "fatalErrorsHidden"}},
}, Tok: token.ASSIGN,
}, Rhs: []ast.Expr{&ast.Ident{Name: "hide"}},
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.AssignStmt{
Lhs: []ast.Expr{
&ast.Ident{Name: "fatalErrorsHidden"},
},
Tok: token.ASSIGN,
Rhs: []ast.Expr{
&ast.Ident{Name: "hide"},
},
},
}, },
}, }},
}, },
} }
var printanyHexCase = &ast.CaseClause{ var printanyHexCase = &ast.CaseClause{
List: []ast.Expr{ List: []ast.Expr{&ast.Ident{Name: "hex"}},
&ast.Ident{Name: "hex"},
},
Body: []ast.Stmt{ Body: []ast.Stmt{
&ast.ExprStmt{ &ast.ExprStmt{X: &ast.CallExpr{
X: &ast.CallExpr{ Fun: &ast.Ident{Name: "print"},
Fun: &ast.Ident{Name: "print"}, Args: []ast.Expr{&ast.Ident{Name: "v"}},
Args: []ast.Expr{ }},
&ast.Ident{Name: "v"},
},
},
},
}, },
} }
var panicprintDecl = &ast.FuncDecl{ var panicprintDecl = &ast.FuncDecl{
Name: &ast.Ident{Name: "panicprint"}, Name: &ast.Ident{Name: "panicprint"},
Type: &ast.FuncType{Params: &ast.FieldList{ Type: &ast.FuncType{Params: &ast.FieldList{
List: []*ast.Field{ List: []*ast.Field{{
{ Names: []*ast.Ident{{Name: "args"}},
Names: []*ast.Ident{ Type: &ast.Ellipsis{Elt: &ast.InterfaceType{
{Name: "args"}, Methods: &ast.FieldList{},
}, }},
Type: &ast.Ellipsis{Elt: &ast.InterfaceType{ }},
Methods: &ast.FieldList{},
}},
},
},
}}, }},
Body: &ast.BlockStmt{List: []ast.Stmt{ Body: &ast.BlockStmt{List: []ast.Stmt{
&ast.IfStmt{ &ast.IfStmt{
@ -166,10 +137,8 @@ var panicprintDecl = &ast.FuncDecl{
X: &ast.Ident{Name: "args"}, X: &ast.Ident{Name: "args"},
Body: &ast.BlockStmt{List: []ast.Stmt{ Body: &ast.BlockStmt{List: []ast.Stmt{
&ast.ExprStmt{X: &ast.CallExpr{ &ast.ExprStmt{X: &ast.CallExpr{
Fun: &ast.Ident{Name: "printany"}, Fun: &ast.Ident{Name: "printany"},
Args: []ast.Expr{ Args: []ast.Expr{&ast.Ident{Name: "arg"}},
&ast.Ident{Name: "arg"},
},
}}, }},
}}, }},
}, },

@ -317,7 +317,6 @@ func keyStmt(key []byte) *ast.GenDecl {
} }
func constBlacklist(node ast.Node, info *types.Info, blacklist map[types.Object]struct{}) { func constBlacklist(node ast.Node, info *types.Info, blacklist map[types.Object]struct{}) {
blacklistObjects := func(node ast.Node) bool { blacklistObjects := func(node ast.Node) bool {
ident, ok := node.(*ast.Ident) ident, ok := node.(*ast.Ident)
if !ok { if !ok {

Loading…
Cancel
Save