forked from goby-lang/goby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_table.go
59 lines (45 loc) · 916 Bytes
/
local_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 bytecode
type localTable struct {
store map[string]int
count int
depth int
upper *localTable
}
func (lt *localTable) get(v string) (int, bool) {
i, ok := lt.store[v]
return i, ok
}
func (lt *localTable) set(val string) int {
c, ok := lt.store[val]
if !ok {
c = lt.count
lt.store[val] = c
lt.count++
return c
}
return c
}
func (lt *localTable) setLCL(v string, d int) (index, depth int) {
index, depth, ok := lt.getLCL(v, d)
if !ok {
index = lt.set(v)
depth = 0
return index, depth
}
return index, depth
}
func (lt *localTable) getLCL(v string, d int) (index, depth int, ok bool) {
index, ok = lt.get(v)
if ok {
return index, d - lt.depth, ok
}
if lt.upper != nil {
index, depth, ok = lt.upper.getLCL(v, d)
return
}
return -1, 0, false
}
func newLocalTable(depth int) *localTable {
s := make(map[string]int)
return &localTable{store: s, depth: depth}
}