52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
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, buffer []byte, kind token.Kind, name string) uint8 {
|
|
count := uint8(0)
|
|
|
|
for _, node := range body {
|
|
switch node := node.(type) {
|
|
case *Assert:
|
|
count += node.Condition.Count(buffer, kind, name)
|
|
|
|
case *Assign:
|
|
count += node.Expression.Count(buffer, kind, name)
|
|
|
|
case *Call:
|
|
count += node.Expression.Count(buffer, kind, name)
|
|
|
|
case *Define:
|
|
count += node.Expression.Count(buffer, kind, name)
|
|
|
|
case *If:
|
|
count += node.Condition.Count(buffer, kind, name)
|
|
count += Count(node.Body, buffer, kind, name)
|
|
count += Count(node.Else, buffer, kind, name)
|
|
|
|
case *Loop:
|
|
count += Count(node.Body, buffer, kind, name)
|
|
|
|
case *Return:
|
|
if node.Value != nil {
|
|
count += node.Value.Count(buffer, kind, name)
|
|
}
|
|
|
|
case *Switch:
|
|
for _, c := range node.Cases {
|
|
if c.Condition != nil {
|
|
count += c.Condition.Count(buffer, kind, name)
|
|
}
|
|
|
|
count += Count(c.Body, buffer, kind, name)
|
|
}
|
|
|
|
default:
|
|
panic("unknown AST type")
|
|
}
|
|
}
|
|
|
|
return count
|
|
}
|