Skip to content

Commit

Permalink
add semantic location
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed Jul 17, 2022
1 parent 31e570e commit c96264e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions semantic/semantic.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (s *Semantic) expectIdentifierDeclare(ident *ast.Identifier) bool {
return false
}

s.localVariables[ident] = s.scopeStack.Size()
return true
}

Expand Down
4 changes: 4 additions & 0 deletions semantic/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
28 changes: 28 additions & 0 deletions semantic/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}
}

0 comments on commit c96264e

Please sign in to comment.