-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f7bf5c
commit 813b7e1
Showing
3 changed files
with
107 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package gnolang | ||
|
||
type SymbolTable struct { | ||
scopes []*Scope | ||
} | ||
|
||
type Scope struct { | ||
symbols map[string]struct{} | ||
} | ||
|
||
func NewSymbolTable() *SymbolTable { | ||
return &SymbolTable{ | ||
scopes: []*Scope{newScope()}, | ||
} | ||
} | ||
|
||
func newScope() *Scope { | ||
return &Scope{ | ||
symbols: make(map[string]struct{}), | ||
} | ||
} | ||
|
||
func (st *SymbolTable) EnterScope() { | ||
st.scopes = append(st.scopes, newScope()) | ||
} | ||
|
||
func (st *SymbolTable) ExitScope() { | ||
if len(st.scopes) > 1 { | ||
st.scopes = st.scopes[:len(st.scopes)-1] | ||
} | ||
} | ||
|
||
func (st *SymbolTable) AddIdentifier(name string) { | ||
if len(st.scopes) > 0 { | ||
st.scopes[len(st.scopes)-1].symbols[name] = struct{}{} | ||
} | ||
} | ||
|
||
func (st *SymbolTable) IdentifierExists(name string) bool { | ||
for i := len(st.scopes) - 1; i >= 0; i-- { | ||
if _, exists := st.scopes[i].symbols[name]; exists { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters