Skip to content

Commit

Permalink
Merge pull request #1480 from visualfc/ast
Browse files Browse the repository at this point in the history
ast: walk add Go+ expr and stmt
  • Loading branch information
xushiwei committed Oct 21, 2023
2 parents d0f4cc8 + c93d724 commit 3b5675b
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions ast/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func walkDeclList(v Visitor, list []Decl) {
// v.Visit(node) is not nil, Walk is invoked recursively with visitor
// w for each of the non-nil children of node, followed by a call of
// w.Visit(nil).
//
func Walk(v Visitor, node Node) {
if v = v.Visit(node); v == nil {
return
Expand Down Expand Up @@ -372,6 +371,62 @@ func Walk(v Visitor, node Node) {
Walk(v, f)
}

// Go+ expr and stmt
case *SliceLit:
walkExprList(v, n.Elts)

case *ErrWrapExpr:
Walk(v, n.X)
if n.Default != nil {
Walk(v, n.Default)
}

case *LambdaExpr:
walkIdentList(v, n.Lhs)
walkExprList(v, n.Rhs)

case *LambdaExpr2:
walkIdentList(v, n.Lhs)
Walk(v, n.Body)

case *ForPhrase:
if n.Key != nil {
Walk(v, n.Key)
}
if n.Value != nil {
Walk(v, n.Value)
}
if n.Init != nil {
Walk(v, n.Init)
}
if n.Cond != nil {
Walk(v, n.Cond)
}
Walk(v, n.X)

case *ComprehensionExpr:
if n.Elt != nil {
Walk(v, n.Elt)
}
for _, x := range n.Fors {
Walk(v, x)
}

case *ForPhraseStmt:
Walk(v, n.ForPhrase)
Walk(v, n.Body)

case *RangeExpr:
if n.First != nil {
Walk(v, n.First)
}
if n.Last != nil {
Walk(v, n.Last)
}
if n.Expr3 != nil {
Walk(v, n.Expr3)
}

default:
panic(fmt.Sprintf("ast.Walk: unexpected node type %T", n))
}
Expand All @@ -392,7 +447,6 @@ func (f inspector) Visit(node Node) Visitor {
// f(node); node must not be nil. If f returns true, Inspect invokes f
// recursively for each of the non-nil children of node, followed by a
// call of f(nil).
//
func Inspect(node Node, f func(Node) bool) {
Walk(inspector(f), node)
}

0 comments on commit 3b5675b

Please sign in to comment.