-
Notifications
You must be signed in to change notification settings - Fork 659
/
scope.go
77 lines (67 loc) · 1.47 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
package main
//-------------------------------------------------------------------------
// scope
//-------------------------------------------------------------------------
type scope struct {
// the package name that this scope resides in
pkgname string
parent *scope // nil for universe scope
entities map[string]*decl
}
func new_named_scope(outer *scope, name string) *scope {
s := new_scope(outer)
s.pkgname = name
return s
}
func new_scope(outer *scope) *scope {
s := new(scope)
if outer != nil {
s.pkgname = outer.pkgname
}
s.parent = outer
s.entities = make(map[string]*decl)
return s
}
// returns: new, prev
func advance_scope(s *scope) (*scope, *scope) {
if len(s.entities) == 0 {
return s, s.parent
}
return new_scope(s), s
}
// adds declaration or returns an existing one
func (s *scope) add_named_decl(d *decl) *decl {
return s.add_decl(d.name, d)
}
func (s *scope) add_decl(name string, d *decl) *decl {
decl, ok := s.entities[name]
if !ok {
s.entities[name] = d
return d
}
return decl
}
func (s *scope) replace_decl(name string, d *decl) {
s.entities[name] = d
}
func (s *scope) merge_decl(d *decl) {
decl, ok := s.entities[d.name]
if !ok {
s.entities[d.name] = d
} else {
decl := decl.deep_copy()
decl.expand_or_replace(d)
s.entities[d.name] = decl
}
}
func (s *scope) lookup(name string) *decl {
decl, ok := s.entities[name]
if !ok {
if s.parent != nil {
return s.parent.lookup(name)
} else {
return nil
}
}
return decl
}