2024-07-16 13:30:28 +00:00
|
|
|
package ast
|
|
|
|
|
2024-08-07 17:39:10 +00:00
|
|
|
import "git.akyoto.dev/cli/q/src/token"
|
2024-07-16 13:30:28 +00:00
|
|
|
|
|
|
|
// 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 *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
|
|
|
|
2024-08-03 20:24:40 +00:00
|
|
|
case *Return:
|
2024-08-05 10:39:07 +00:00
|
|
|
for _, value := range node.Values {
|
|
|
|
count += value.Count(buffer, kind, name)
|
2024-08-03 20:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-16 13:30:28 +00:00
|
|
|
default:
|
|
|
|
panic("unknown AST type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|