-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
62 lines (52 loc) · 1.02 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
package main
type (
Scope struct {
parent *Scope
vars ScopeTable
funcs ScopeTable
}
ScopeTable map[string]ScopeEntry
ScopeEntry struct {
DataType
Value interface{}
}
)
func NewScope() *Scope {
s := &Scope{
vars: make(ScopeTable, 0),
funcs: make(ScopeTable, 0),
}
return s
}
func NewScopeWithParent(p *Scope) *Scope {
s := NewScope()
s.parent = p
return s
}
// EmptyVarCopy returns a copy of the scope with a empty var table but funcs
// still defined.
func (s *Scope) EmptyVarCopy() *Scope {
s2 := NewScope()
s2.parent = s.parent
s2.funcs = s.funcs
return s2
}
func (s *Scope) SetVar(key string, entry ScopeEntry) {
s.vars[key] = entry
}
func (s *Scope) GetVar(key string) (ScopeEntry, bool) {
entry, ok := s.vars[key]
return entry, ok
}
func (s *Scope) SetFunc(key string, entry ScopeEntry) {
s.funcs[key] = entry
}
func (s *Scope) GetFunc(key string) (ScopeEntry, bool) {
cur := s
for {
if entry, ok := cur.funcs[key]; ok || cur.parent == nil {
return entry, ok
}
cur = cur.parent
}
}