-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
symbol_table.go
59 lines (49 loc) · 1.18 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
package core
// A symbolScope represents a symbol scope during compilation.
type symbolScope int
const (
GLOBAL_SCOPE symbolScope = iota + 1
LOCAL_SCOPE
)
// symbol represents a symbol in the symbol table.
type symbol struct {
Name string
Index int
IsConstant bool
}
// A symbolTable represents a symbol table for a single module during compilation.
type symbolTable struct {
store map[string]*symbol
nextSymbolIndex int
}
func newSymbolTable() *symbolTable {
return &symbolTable{
store: make(map[string]*symbol),
}
}
// Define defines a new symbol in the table and returns it.
func (t *symbolTable) Define(name string) *symbol {
nextIndex := t.nextSymbolIndex
symbol := &symbol{
Name: name,
Index: nextIndex,
}
t.nextSymbolIndex++
t.store[name] = symbol
return symbol
}
// Resolve returns the symbol with a given name in the table.
func (t *symbolTable) Resolve(name string) (*symbol, bool) {
symbol, ok := t.store[name]
return symbol, ok
}
func (t *symbolTable) SymbolCount() int {
return t.nextSymbolIndex
}
func (t *symbolTable) SymbolNames() []string {
var names []string
for name := range t.store {
names = append(names, name)
}
return names
}