-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
182 lines (159 loc) · 2.97 KB
/
context.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package djson
import "bytes"
type Context interface {
Assign(varName []byte, val Value)
ValueOf(name []byte) Value
PushScope()
PopScope()
Merge(ctx Context)
All() []Variable
Copy() Context
pushMe(val Value)
popMe()
}
type Variable struct {
Name []byte
Value Value
}
type scope struct {
p *scope
vars []Variable
}
type ctx struct {
scope *scope
}
var _ Context = &ctx{}
func NewContext(vars ...Variable) *ctx {
s := &scope{vars: vars}
return &ctx{
scope: s,
}
}
func (s *scope) assign(name []byte, val Value) {
if idx := s.indexOf(name); idx > -1 {
s.vars[idx] = Variable{Name: name, Value: val}
return
}
s.vars = append(s.vars, Variable{Name: name, Value: val})
}
func (s *scope) copy() *scope {
var p *scope
if s.p != nil {
p = s.p.copy()
}
r := &scope{
p: p,
vars: make([]Variable, len(s.vars)),
}
copy(r.vars, s.vars)
return s
}
func (s *scope) indexOf(name []byte) int {
for i := range s.vars {
if bytes.Equal(s.vars[i].Name, name) {
return i
}
}
return -1
}
func (s *scope) del(idx int) {
s.vars = append(s.vars[:idx], s.vars[idx+1:]...)
}
func (v *ctx) Assign(name []byte, val Value) {
scope := v.scope
for scope != nil {
if idx := scope.indexOf(name); idx > -1 {
scope.vars[idx] = Variable{Name: name, Value: val}
return
}
scope = scope.p
}
v.scope.assign(name, val)
}
func (ctx *ctx) Merge(lv Context) {
all := lv.All()
for _, v := range all {
ctx.Assign(v.Name, v.Value)
}
}
func (v *ctx) All() []Variable {
ret := []Variable{}
inRet := func(v *Variable) bool {
for _, lv := range ret {
if bytes.Equal(lv.Name, v.Name) {
return true
}
}
return false
}
scope := v.scope
for scope != nil {
for _, v := range scope.vars {
if inRet(&v) {
continue
}
ret = append(ret, v)
}
scope = scope.p
}
return ret
}
func (v *ctx) Copy() Context {
return &ctx{scope: v.scope.copy()}
}
func (v *ctx) PushScope() {
v.scope = &scope{p: v.scope}
}
func (v *ctx) PopScope() {
if v.scope != nil && v.scope.p != nil {
v.scope = v.scope.p
}
}
func (v *ctx) ValueOf(name []byte) Value {
scope := v.scope
for scope != nil {
if idx := scope.indexOf(name); idx > -1 {
return scope.vars[idx].Value
}
scope = scope.p
}
return NullValue()
}
func (v *ctx) pushMe(val Value) {
mk := []byte{'_', 'm', 'e'}
if idx := v.scope.indexOf(mk); idx > -1 {
val.p = &v.scope.vars[idx].Value
}
v.scope.assign(mk, val)
}
func (v *ctx) popMe() {
mk := []byte{'_', 'm', 'e'}
if idx := v.scope.indexOf(mk); idx > -1 {
if v.scope.vars[idx].Value.p != nil {
v.scope.assign(mk, *v.scope.vars[idx].Value.p)
return
}
v.scope.del(idx)
}
}
func path(p string) []byte {
return []byte(p)
}
func splitKeyAndRest(ik []byte) (k []byte, rest []byte) {
dot := bytes.Index(ik, []byte{'.'})
if dot < 0 {
k = ik
return
}
k = ik[0:dot]
rest = ik[dot+1:]
return
}
func (vs ctx) lookup(k []byte) Value {
i, r := splitKeyAndRest(k)
val := vs.ValueOf(i)
if len(r) == 0 {
return val
}
return val.lookup(r)
}