39 lines
743 B
Go
Raw Normal View History

2024-07-16 13:30:28 +00:00
package ast
import "git.akyoto.dev/cli/q/src/build/token"
// Count counts how often the given token appears in the AST.
func Count(body AST, kind token.Kind, name string) int {
count := 0
for _, node := range body {
switch node := node.(type) {
case *Assign:
2024-07-20 15:35:26 +00:00
count += node.Expression.Count(kind, name)
2024-07-16 13:30:28 +00:00
case *Call:
count += node.Expression.Count(kind, name)
case *Define:
count += node.Value.Count(kind, name)
case *Return:
2024-07-16 18:22:28 +00:00
if node.Value != nil {
count += node.Value.Count(kind, name)
}
2024-07-16 13:30:28 +00:00
case *If:
count += node.Condition.Count(kind, name)
count += Count(node.Body, kind, name)
case *Loop:
count += Count(node.Body, kind, name)
default:
panic("unknown AST type")
}
}
return count
}