Moved register state to scopes
This commit is contained in:
parent
3bd5b20af3
commit
545c8dd4f6
22 changed files with 230 additions and 129 deletions
36
src/build/ast/Count.go
Normal file
36
src/build/ast/Count.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
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:
|
||||
count += node.Value.Count(kind, name)
|
||||
|
||||
case *Call:
|
||||
count += node.Expression.Count(kind, name)
|
||||
|
||||
case *Define:
|
||||
count += node.Value.Count(kind, name)
|
||||
|
||||
case *Return:
|
||||
count += node.Value.Count(kind, name)
|
||||
|
||||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue