-
Notifications
You must be signed in to change notification settings - Fork 4
/
scope.go
63 lines (51 loc) · 1.11 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
package eval
import (
"fmt"
"io"
)
func NewScope(p *Scope, w io.Writer) *Scope {
s := make(map[string]Object)
ret := &Scope{store: s, parentScope: p}
if p == nil {
ret.Writer = w
} else {
ret.Writer = p.Writer
}
return ret
}
type Scope struct {
store map[string]Object
parentScope *Scope
Writer io.Writer
}
func (s *Scope) Get(name string) (Object, bool) {
obj, ok := s.store[name]
if !ok && s.parentScope != nil {
obj, ok = s.parentScope.Get(name)
}
return obj, ok
}
// Get all the keys of the scope.
func (s *Scope) GetKeys() []string {
keys := make([]string, 0, len(s.store))
for k := range s.store {
keys = append(keys, k)
}
return keys
}
func (s *Scope) DebugPrint(indent string) {
for k, v := range s.store {
fmt.Fprintf(s.Writer, "%s<%s> = <%s> value.Type: %T\n", indent, k, v.Inspect(), v)
}
if s.parentScope != nil {
fmt.Fprintf(s.Writer, "\n%sParentScope:\n", indent)
s.parentScope.DebugPrint(indent + " ")
}
}
func (s *Scope) Set(name string, val Object) Object {
s.store[name] = val
return val
}
func (s *Scope) Del(name string) {
delete(s.store, name)
}