-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
105 lines (86 loc) · 2.03 KB
/
scope.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
package main
import "container/list"
type scope struct {
objs map[string]*obj
structdefs map[string]*structdef
blockscopes *list.List
}
func newscope() *scope {
return &scope{
objs: map[string]*obj{},
structdefs: map[string]*structdef{},
blockscopes: list.New(),
}
}
func (s *scope) addblockscope() {
s.blockscopes.PushBack(newblockscope())
}
func (s *scope) delblockscope() {
s.blockscopes.Remove(s.blockscopes.Back())
}
func (s *scope) setstruct(name string, sd *structdef) {
if s.blockscopes.Len() == 0 {
s.structdefs[name] = sd
return
}
for e := s.blockscopes.Back(); e != nil; e = e.Prev() {
bs := e.Value.(*blockscope)
if _, ok := bs.structdefs[name]; ok {
bs.structdefs[name] = sd
return
}
}
s.blockscopes.Back().Value.(*blockscope).structdefs[name] = sd
}
func (s *scope) setobj(name string, o *obj) {
if s.blockscopes.Len() == 0 {
s.objs[name] = o
return
}
for e := s.blockscopes.Back(); e != nil; e = e.Prev() {
bs := e.Value.(*blockscope)
if _, ok := bs.objs[name]; ok {
bs.objs[name] = o
return
}
}
s.blockscopes.Back().Value.(*blockscope).objs[name] = o
}
func (s *scope) getstruct(name string) (*structdef, bool) {
for e := s.blockscopes.Back(); e != nil; e = e.Prev() {
bs := e.Value.(*blockscope)
if sd, ok := bs.structdefs[name]; ok {
return sd, ok
}
}
sd, ok := s.structdefs[name]
return sd, ok
}
func (s *scope) getobj(name string) (*obj, bool) {
for e := s.blockscopes.Back(); e != nil; e = e.Prev() {
bs := e.Value.(*blockscope)
if o, ok := bs.objs[name]; ok {
return o, ok
}
}
o, ok := s.objs[name]
return o, ok
}
func (s *scope) getglobstruct(name string) (*structdef, bool) {
sd, ok := s.structdefs[name]
return sd, ok
}
func (s *scope) getglobobj(name string) (*obj, bool) {
o, ok := s.objs[name]
return o, ok
}
type blockscope struct {
objs map[string]*obj
structdefs map[string]*structdef
}
func newblockscope() *blockscope {
return &blockscope{
objs: map[string]*obj{},
structdefs: map[string]*structdef{},
}
}