40 lines
645 B
Go
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
|
|
}
|