Fixed variable lifetime in loops
This commit is contained in:
@ -9,6 +9,10 @@ func Count(body AST, kind token.Kind, name string) int {
|
||||
for _, node := range body {
|
||||
switch node := node.(type) {
|
||||
case *Assign:
|
||||
if node.Name.Kind == kind && node.Name.Text() == name {
|
||||
count++
|
||||
}
|
||||
|
||||
count += node.Value.Count(kind, name)
|
||||
|
||||
case *Call:
|
||||
@ -18,7 +22,9 @@ func Count(body AST, kind token.Kind, name string) int {
|
||||
count += node.Value.Count(kind, name)
|
||||
|
||||
case *Return:
|
||||
count += node.Value.Count(kind, name)
|
||||
if node.Value != nil {
|
||||
count += node.Value.Count(kind, name)
|
||||
}
|
||||
|
||||
case *If:
|
||||
count += node.Condition.Count(kind, name)
|
||||
|
@ -30,6 +30,10 @@ func toASTNode(tokens token.List) (Node, error) {
|
||||
word := tokens[0].Text()
|
||||
|
||||
if word == keyword.Return {
|
||||
if len(tokens) == 1 {
|
||||
return &Return{}, nil
|
||||
}
|
||||
|
||||
value := expression.Parse(tokens[1:])
|
||||
return &Return{Value: value}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user