diff --git a/semantic/semantic.go b/semantic/semantic.go index 6276ac1..84ea436 100644 --- a/semantic/semantic.go +++ b/semantic/semantic.go @@ -82,6 +82,7 @@ func (s *Semantic) expectIdentifierDeclare(ident *ast.Identifier) bool { return false } + s.localVariables[ident] = s.scopeStack.Size() return true } diff --git a/semantic/stack.go b/semantic/stack.go index 76891ea..bd3c52e 100644 --- a/semantic/stack.go +++ b/semantic/stack.go @@ -38,3 +38,7 @@ func (s *Scope) Put(name string, ready bool) { func (s *Stack) Get(index int) *Scope { return (*s)[index] } + +func (s *Stack) Size() int { + return len(*s) +} diff --git a/semantic/stack_test.go b/semantic/stack_test.go index 6195d5b..6bb2b32 100644 --- a/semantic/stack_test.go +++ b/semantic/stack_test.go @@ -104,3 +104,31 @@ func TestStack_Get(t *testing.T) { t.Fatalf("Unable to get stack by index") } } + +func TestStack_Size(t *testing.T) { + tests := []struct { + stack Stack + size int + }{ + { + Stack{}, + 0, + }, + { + Stack{&Scope{}}, + 1, + }, + { + Stack{&Scope{}, &Scope{}}, + 2, + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("TestStack_Size[%d]", i), func(t *testing.T) { + if tt.stack.Size() != tt.size { + t.Errorf("Expected %d, got: %d", tt.size, tt.stack.Size()) + } + }) + } +}