-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolTable.go
39 lines (31 loc) · 801 Bytes
/
symbolTable.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
package gymbol
type SymbolTable struct {
strings []string
indices map[string]uint32
}
func NewSymbolTable() *SymbolTable {
strings := []string{}
indices := map[string]uint32{}
table := &SymbolTable{strings, indices}
for i := range CORE_SYMBOL_NAMES {
str := CORE_SYMBOL_NAMES[i]
table.Intern(str)
}
return table
}
func (table *SymbolTable) set(str string, index uint32) {
table.strings = append(table.strings, str)
table.indices[str] = index
}
func (table *SymbolTable) Intern(str string) Symbol {
existingIndex, ok := table.indices[str]
if ok {
return Symbol{Index: existingIndex}
}
newIndex := uint32(len(table.strings))
table.set(str, newIndex)
return Symbol{Index: newIndex}
}
func (table *SymbolTable) Resolve(sym Symbol) string {
return table.strings[sym.Index]
}