-
Notifications
You must be signed in to change notification settings - Fork 2
/
scope.go
46 lines (39 loc) · 1.08 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
package spiker
// VariableScope variable scope
type VariableScope struct {
scopeName string
scopeLevel int
vars map[string]interface{}
enclosingScope *VariableScope
}
// NewScopeTable return a new VariableScope
func NewScopeTable(scopeName string, scopeLevel int, scope *VariableScope) *VariableScope {
vs := &VariableScope{}
vs.vars = make(map[string]interface{})
vs.scopeName = scopeName
vs.scopeLevel = scopeLevel
vs.enclosingScope = scope
return vs
}
// Set store variable values
func (scope *VariableScope) Set(variable string, val interface{}) {
scope.vars[variable] = val
}
// Get fetch variable values
func (scope *VariableScope) Get(variable string) (interface{}, bool) {
if val, ok := scope.vars[variable]; ok {
return val, true
}
if scope.enclosingScope != nil {
return scope.enclosingScope.Get(variable)
}
return nil, false
}
// Del delete a variable
func (scope *VariableScope) Del(variable string) {
delete(scope.vars, variable)
}
// Clean clean all of the vars
func (scope *VariableScope) Clean() {
scope.vars = make(map[string]interface{})
}