Globals to compiler
This commit is contained in:
39
compiler/symbol_table.go
Normal file
39
compiler/symbol_table.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user