Fixed variable lifetime in loops

This commit is contained in:
2024-07-16 20:22:28 +02:00
parent 1825a72f8c
commit f9d72fe490
11 changed files with 90 additions and 18 deletions

View File

@ -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)

View File

@ -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
}