/
symbol_table.go
87 lines (73 loc) · 2.25 KB
/
symbol_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package compiler
type SymbolScope string
const (
GlobalScope SymbolScope = "GLOBAL"
LocalScope SymbolScope = "LOCAL"
BuiltinScope SymbolScope = "BUILTIN"
FreeScope SymbolScope = "FREE"
FunctionScope SymbolScope = "FUNCTION"
)
type Symbol struct {
Name string
Scope SymbolScope
Index int
}
type SymbolTable struct {
Outer *SymbolTable
store map[string]Symbol
numDefinitions int
FreeSymbols []Symbol // Note that Scopes of all the symbols are LocalScope.
}
func (s *SymbolTable) Define(name string) Symbol {
symbol := Symbol{Name: name, Index: s.numDefinitions}
if s.Outer == nil {
symbol.Scope = GlobalScope
} else {
symbol.Scope = LocalScope
}
s.store[name] = symbol
s.numDefinitions++
return symbol
}
func (s *SymbolTable) DefineBuiltin(index int, name string) Symbol {
symbol := Symbol{Name: name, Index: index, Scope: BuiltinScope}
s.store[name] = symbol // This would be overwritten when same named global/local bindings are made.
return symbol
}
/*
Append a given Scope to the current symbol table's `FreeSymbols` slice.
Also, define a corresponding LocalScope and register it in the current globals store.
This newly added scope would be returned.
*/
func (s *SymbolTable) DefineFree(original Symbol) Symbol {
s.FreeSymbols = append(s.FreeSymbols, original)
symbol := Symbol{Name: original.Name, Index: len(s.FreeSymbols) - 1, Scope: FreeScope}
s.store[original.Name] = symbol
return symbol
}
func (s *SymbolTable) DefineFunctionName(name string) Symbol {
symbol := Symbol{Name: name, Index: 0, Scope: FunctionScope} // Index does not matter.
s.store[name] = symbol
return symbol
}
func (s *SymbolTable) Resolve(name string) (Symbol, bool) {
obj, okInLocal := s.store[name]
if okInLocal || s.Outer == nil {
return obj, okInLocal
}
obj, okInOuter := s.Outer.Resolve(name)
if okInOuter && obj.Scope != GlobalScope && obj.Scope != BuiltinScope {
// Note that a free scoped symbol is registered in FreeSymbols slice in every step in the recursion.
obj = s.DefineFree(obj)
}
return obj, okInOuter
}
func NewSymbolTable() *SymbolTable {
s := make(map[string]Symbol)
return &SymbolTable{store: s}
}
func NewEnclosedSymbolTable(outer *SymbolTable) *SymbolTable {
s := NewSymbolTable()
s.Outer = outer
return s
}