-
Notifications
You must be signed in to change notification settings - Fork 8
/
dict.go
112 lines (96 loc) · 1.67 KB
/
dict.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
package main
type Dict struct {
list []*DictEntry
parent *Dict
}
type DictValue struct {
ast *Ast
ctype *Ctype
tok *Token
}
type DictEntry struct {
key string
val *DictValue
}
func MakeDict(parent *Dict) *Dict {
return &Dict{
parent: parent,
}
}
func (dict *Dict) Parent() *Dict {
return dict.parent
}
func (dict *Dict) GetAst(key string) *Ast {
val := dict.Get(key)
if val == nil {
return nil
}
return val.ast
}
func (dict *Dict) GetCtype(key string) *Ctype {
val := dict.Get(key)
if val == nil {
return nil
}
return val.ctype
}
func (dict *Dict) GetToken(key string) *Token {
val := dict.Get(key)
if val == nil {
return nil
}
return val.tok
}
func (dict *Dict) Get(key string) *DictValue {
for d := dict; d != nil; d = d.parent {
for _, e := range d.list {
if e.key == key {
return e.val
}
}
}
return nil
}
func (dict *Dict) PutAst(key string, val *Ast) {
dict.Put(key, &DictValue{
ast: val,
})
}
func (dict *Dict) PutCtype(key string, val *Ctype) {
dict.Put(key, &DictValue{
ctype: val,
})
}
func (dict *Dict) PutToken(key string, val *Token) {
dict.Put(key, &DictValue{
tok: val,
})
}
func (dict *Dict) Put(key string, val *DictValue) {
e := &DictEntry{
key: key,
val: val,
}
dict.list = append(dict.list, e)
}
func (dict *Dict) Empty() bool {
return len(dict.list) == 0
}
func (dict *Dict) Keys() []string {
var r []string
for d := dict; d != nil; d = d.parent {
for _, e := range d.list {
r = append(r, e.key)
}
}
return r
}
func (dict *Dict) Values() []*DictValue {
var r []*DictValue
for d := dict; d != nil; d = d.parent {
for _, e := range d.list {
r = append(r, e.val)
}
}
return r
}