Globals to compiler

This commit is contained in:
Chuck Smith
2024-02-20 16:07:01 -05:00
parent 6ba2d3abe4
commit e8254fc996
7 changed files with 213 additions and 0 deletions

39
compiler/symbol_table.go Normal file
View File

@@ -0,0 +1,39 @@
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
}