Moved register state to scopes

This commit is contained in:
2024-07-16 15:30:28 +02:00
parent d1ccd60139
commit d6d018c5c5
22 changed files with 230 additions and 129 deletions

14
src/build/token/Count.go Normal file
View File

@ -0,0 +1,14 @@
package token
// Count counts how often the given token appears in the token list.
func Count(tokens List, kind Kind, name string) int {
count := 0
for _, t := range tokens {
if t.Kind == Identifier && t.Text() == name {
count++
}
}
return count
}

View File

@ -95,6 +95,14 @@ func TestArray(t *testing.T) {
})
}
func TestCount(t *testing.T) {
tokens := token.Tokenize([]byte(`a b b c c c`))
assert.Equal(t, token.Count(tokens, token.Identifier, "a"), 1)
assert.Equal(t, token.Count(tokens, token.Identifier, "b"), 2)
assert.Equal(t, token.Count(tokens, token.Identifier, "c"), 3)
assert.Equal(t, token.Count(tokens, token.Identifier, "d"), 0)
}
func TestNewline(t *testing.T) {
tokens := token.Tokenize([]byte("\n\n"))
assert.DeepEqual(t, tokens, token.List{