| 123456789101112131415161718192021222324252627282930313233343536 |
- package compiler
- type SymbolScope string
- const (
- GlobalScope SymbolScope = "GLOBAL"
- )
- type Symbol struct {
- Name string // identifier
- 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, Index: s.numDefinitions, Scope: GlobalScope}
- s.store[name] = symbol
- s.numDefinitions++
- return symbol
- }
- func (s *SymbolTable) Resolve(name string) (Symbol, bool) {
- obj, ok := s.store[name]
- return obj, ok
- }
|