Moved register state to scopes

This commit is contained in:
Eduard Urbach 2024-07-16 15:30:28 +02:00
parent 3bd5b20af3
commit 545c8dd4f6
Signed by: eduard
GPG key ID: 49226B848C78F6C8
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{