Files
monkey/compiler/symbol_table.go
2024-02-20 16:07:01 -05:00

40 lines
645 B
Go

package compiler
type SymbolScope string
const (
GlobalScope SymbolScope = "Global"
)
type Symbol struct {
Name string
Scope SymbolScope
Index int
}
type SymbolTable struct {
store map[string]Symbol
numDefinitions int
}
func NewSymbolTable() *SymbolTable {
s := make(map[string]Symbol)
return &SymbolTable{store: s}
}
func (s *SymbolTable) Define(name string) Symbol {
symbol := Symbol{
Name: name,
Scope: GlobalScope,
Index: s.numDefinitions,
}
s.store[name] = symbol
s.numDefinitions++
return symbol
}
func (s *SymbolTable) Resolve(name string) (Symbol, bool) {
obj, ok := s.store[name]
return obj, ok
}