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.
|
2024-07-22 20:54:24 +00:00
|
|
|
func Count(body AST, buffer []byte, kind token.Kind, name string) uint8 {
|
|
|
|
count := uint8(0)
|
2024-07-16 13:30:28 +00:00
|
|
|
|
|
|
|
for _, node := range body {
|
|
|
|
switch node := node.(type) {
|
2024-08-02 10:16:35 +00:00
|
|
|
case *Assert:
|
|
|
|
count += node.Condition.Count(buffer, kind, name)
|
|
|
|
|
2024-07-16 13:30:28 +00:00
|
|
|
case *Assign:
|
2024-07-21 12:35:06 +00:00
|
|
|
count += node.Expression.Count(buffer, kind, name)
|
2024-07-16 13:30:28 +00:00
|
|
|
|
|
|
|
case *Call:
|
2024-07-21 12:35:06 +00:00
|
|
|
count += node.Expression.Count(buffer, kind, name)
|
2024-07-16 13:30:28 +00:00
|
|
|
|
|
|
|
case *Define:
|
2024-07-28 16:12:42 +00:00
|
|
|
count += node.Expression.Count(buffer, kind, name)
|
2024-07-16 13:30:28 +00:00
|
|
|
|
|
|
|
case *Return:
|
2024-07-16 18:22:28 +00:00
|
|
|
if node.Value != nil {
|
2024-07-21 12:35:06 +00:00
|
|
|
count += node.Value.Count(buffer, kind, name)
|
2024-07-16 18:22:28 +00:00
|
|
|
}
|
2024-07-16 13:30:28 +00:00
|
|
|
|
|
|
|
case *If:
|
2024-07-21 12:35:06 +00:00
|
|
|
count += node.Condition.Count(buffer, kind, name)
|
|
|
|
count += Count(node.Body, buffer, kind, name)
|
2024-08-01 12:40:47 +00:00
|
|
|
count += Count(node.Else, buffer, kind, name)
|
2024-07-16 13:30:28 +00:00
|
|
|
|
|
|
|
case *Loop:
|
2024-07-21 12:35:06 +00:00
|
|
|
count += Count(node.Body, buffer, kind, name)
|
2024-07-16 13:30:28 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic("unknown AST type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|