-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_table.go
342 lines (289 loc) · 7.46 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package charlang
import (
"errors"
"fmt"
"sort"
)
// SymbolScope represents a symbol scope.
type SymbolScope string
// List of symbol scopes
const (
ScopeGlobal SymbolScope = "GLOBAL"
ScopeLocal SymbolScope = "LOCAL"
ScopeBuiltin SymbolScope = "BUILTIN"
ScopeFree SymbolScope = "FREE"
)
// Symbol represents a symbol in the symbol table.
type Symbol struct {
Name string
Index int
Scope SymbolScope
Assigned bool
Constant bool
Original *Symbol
}
func (s *Symbol) String() string {
return fmt.Sprintf("Symbol{Name:%s Index:%d Scope:%s Assigned:%v "+
"Original:%s Constant:%t}",
s.Name, s.Index, s.Scope, s.Assigned, s.Original, s.Constant)
}
// SymbolTable represents a symbol table.
type SymbolTable struct {
parent *SymbolTable
maxDefinition int
numDefinition int
numParams int
store map[string]*Symbol
disabledBuiltins map[string]struct{}
frees []*Symbol
block bool
disableParams bool
shadowedBuiltins []string
}
// NewSymbolTable creates new symbol table object.
func NewSymbolTable() *SymbolTable {
return &SymbolTable{
store: make(map[string]*Symbol),
}
}
// Fork creates a new symbol table for a new scope.
func (st *SymbolTable) Fork(block bool) *SymbolTable {
fork := NewSymbolTable()
fork.parent = st
fork.block = block
fork.disableParams = st.disableParams
return fork
}
// Parent returns the outer scope of the current symbol table.
func (st *SymbolTable) Parent(skipBlock bool) *SymbolTable {
if skipBlock && st.block {
return st.parent.Parent(skipBlock)
}
return st.parent
}
// EnableParams enables or disables definition of parameters.
func (st *SymbolTable) EnableParams(v bool) *SymbolTable {
st.disableParams = !v
return st
}
// InBlock returns true if symbol table belongs to a block.
func (st *SymbolTable) InBlock() bool {
return st.block
}
// SetParams sets parameters defined in the scope. This can be called only once.
func (st *SymbolTable) SetParams(params ...string) error {
if len(params) == 0 {
return nil
}
if st.numParams > 0 {
return errors.New("parameters already defined")
}
if st.disableParams {
return errors.New("parameters disabled")
}
st.numParams = len(params)
for _, param := range params {
if _, ok := st.store[param]; ok {
return fmt.Errorf("%q redeclared in this block", param)
}
symbol := &Symbol{
Name: param,
Index: st.NextIndex(),
Scope: ScopeLocal,
}
st.numDefinition++
st.store[param] = symbol
st.updateMaxDefs(symbol.Index + 1)
st.shadowBuiltin(param)
}
return nil
}
func (st *SymbolTable) find(name string, scopes ...SymbolScope) (*Symbol, bool) {
if symbol, ok := st.store[name]; ok {
if len(scopes) == 0 {
return symbol, ok
}
for _, s := range scopes {
if s == symbol.Scope {
return symbol, true
}
}
}
return nil, false
}
// Resolve resolves a symbol with a given name.
func (st *SymbolTable) Resolve(name string) (symbol *Symbol, ok bool) {
symbol, ok = st.store[name]
if !ok && st.parent != nil {
symbol, ok = st.parent.Resolve(name)
if !ok {
return
}
if !st.block &&
symbol.Scope != ScopeGlobal &&
symbol.Scope != ScopeBuiltin {
return st.defineFree(symbol), true
}
}
if !ok && st.parent == nil && !st.isBuiltinDisabled(name) {
if idx, exists := BuiltinsMap[name]; exists {
symbol = &Symbol{
Name: name,
Index: int(idx),
Scope: ScopeBuiltin,
}
st.store[name] = symbol
return symbol, true
}
}
return
}
// DefineLocal adds a new symbol with ScopeLocal in the current scope.
func (st *SymbolTable) DefineLocal(name string) (*Symbol, bool) {
symbol, ok := st.store[name]
if ok {
return symbol, true
}
index := st.NextIndex()
symbol = &Symbol{
Name: name,
Index: index,
Scope: ScopeLocal,
}
st.numDefinition++
st.store[name] = symbol
st.updateMaxDefs(symbol.Index + 1)
st.shadowBuiltin(name)
return symbol, false
}
func (st *SymbolTable) defineFree(original *Symbol) *Symbol {
// no duplicate symbol exists in "frees" because it is stored in map
// and next Resolve call returns existing symbol
st.frees = append(st.frees, original)
symbol := &Symbol{
Name: original.Name,
Index: len(st.frees) - 1,
Scope: ScopeFree,
Constant: original.Constant,
Original: original,
}
st.store[original.Name] = symbol
st.shadowBuiltin(original.Name)
return symbol
}
func (st *SymbolTable) updateMaxDefs(numDefs int) {
if numDefs > st.maxDefinition {
st.maxDefinition = numDefs
}
if st.block {
st.parent.updateMaxDefs(numDefs)
}
}
// NextIndex returns the next symbol index.
func (st *SymbolTable) NextIndex() int {
if st.block {
return st.parent.NextIndex() + st.numDefinition
}
return st.numDefinition
}
// DefineGlobal adds a new symbol with ScopeGlobal in the current scope.
func (st *SymbolTable) DefineGlobal(name string) (*Symbol, error) {
if st.parent != nil {
return nil, errors.New("global declaration can be at top scope")
}
sym, ok := st.store[name]
if ok {
if sym.Scope != ScopeGlobal {
return nil, fmt.Errorf("%q redeclared in this block", name)
}
return sym, nil
}
s := &Symbol{
Name: name,
Index: -1,
Scope: ScopeGlobal,
}
st.store[name] = s
st.shadowBuiltin(name)
return s, nil
}
// MaxSymbols returns the total number of symbols defined in the scope.
func (st *SymbolTable) MaxSymbols() int {
return st.maxDefinition
}
// NumParams returns number of parameters for the scope.
func (st *SymbolTable) NumParams() int {
return st.numParams
}
// FreeSymbols returns registered free symbols for the scope.
func (st *SymbolTable) FreeSymbols() []*Symbol {
return st.frees
}
// Symbols returns registered symbols for the scope.
func (st *SymbolTable) Symbols() []*Symbol {
out := make([]*Symbol, 0, len(st.store))
for _, s := range st.store {
out = append(out, s)
}
sort.Slice(out, func(i, j int) bool {
return out[i].Index < out[j].Index
})
return out
}
// DisableBuiltin disables given builtin name(s).
// Compiler returns `Compile Error: unresolved reference "builtin name"`
// if a disabled builtin is used.
func (st *SymbolTable) DisableBuiltin(names ...string) *SymbolTable {
if len(names) == 0 {
return st
}
if st.parent != nil {
return st.parent.DisableBuiltin(names...)
}
if st.disabledBuiltins == nil {
st.disabledBuiltins = make(map[string]struct{})
}
for _, n := range names {
st.disabledBuiltins[n] = struct{}{}
}
return st
}
// DisabledBuiltins returns disabled builtin names.
func (st *SymbolTable) DisabledBuiltins() []string {
if st.parent != nil {
return st.parent.DisabledBuiltins()
}
if st.disabledBuiltins == nil {
return nil
}
names := make([]string, 0, len(st.disabledBuiltins))
for n := range st.disabledBuiltins {
names = append(names, n)
}
return names
}
// isBuiltinDisabled returns true if builtin name marked as disabled.
func (st *SymbolTable) isBuiltinDisabled(name string) bool {
if st.parent != nil {
return st.parent.isBuiltinDisabled(name)
}
_, ok := st.disabledBuiltins[name]
return ok
}
// ShadowedBuiltins returns all shadowed builtin names including parent symbol
// tables'. Returing slice may contain duplicate names.
func (st *SymbolTable) ShadowedBuiltins() []string {
var out []string
if len(st.shadowedBuiltins) > 0 {
out = append(out, st.shadowedBuiltins...)
}
if st.parent != nil {
out = append(out, st.parent.ShadowedBuiltins()...)
}
return out
}
func (st *SymbolTable) shadowBuiltin(name string) {
if _, ok := BuiltinsMap[name]; ok {
st.shadowedBuiltins = append(st.shadowedBuiltins, name)
}
}