/
evaluator.go
77 lines (68 loc) · 1.15 KB
/
evaluator.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 lookup
import (
"reflect"
)
type Runner interface {
Run(scope *Scope) Pathor
}
type Scope struct {
Current Pathor
Parent *Scope
v reflect.Value
path *string
Position Pathor
}
func NewScope(parent Pathor, position Pathor) *Scope {
var parentScope *Scope
if parent != nil {
parentScope = NewScope(nil, parent)
}
return &Scope{
Current: position,
Parent: parentScope,
Position: position,
}
}
func (s *Scope) Copy() *Scope {
return &Scope{
Current: s.Current,
Parent: s.Parent,
v: s.v,
path: s.path,
}
}
func (s *Scope) Nest(new Pathor) *Scope {
return &Scope{
Current: new,
Parent: s,
Position: new,
}
}
func (s *Scope) Value() reflect.Value {
if s.v.IsValid() {
return s.v
}
if s.Current != nil {
return s.Current.Value()
}
if s.Parent != nil {
return s.Parent.Value()
}
return reflect.Value{}
}
func (s *Scope) Path() string {
if s.path != nil {
return *s.path
}
if s.Parent != nil {
return s.Parent.Path()
}
return ExtractPath(s.Current)
}
func (s *Scope) Next(position Pathor) *Scope {
return &Scope{
Current: s.Current,
Parent: s,
Position: position,
}
}