-
Notifications
You must be signed in to change notification settings - Fork 3
/
evaluate.go
66 lines (54 loc) · 1.47 KB
/
evaluate.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
package velty
import (
"github.com/viant/velty/est"
"github.com/viant/velty/est/op"
"github.com/viant/velty/parser"
"unsafe"
)
type evaluator struct {
x *op.Operand
cache *cache
control est.Control
parent *Planner
}
func (e *evaluator) compute(state *est.State) unsafe.Pointer {
varValue := *(*string)(e.x.Exec(state))
if cacheValue, ok := e.cache.expression(varValue); ok {
newState := e.newState(cacheValue.planner, state)
return cacheValue.compute(newState)
}
block, err := parser.Parse([]byte(varValue))
if err != nil {
return est.EmptyStringPtr
}
evaluatorPlanner := e.parent.New()
exec, err := evaluatorPlanner.newCompute(block)
if err != nil {
return est.EmptyStringPtr
}
newState := e.newState(evaluatorPlanner, state)
e.cache.put(varValue, evaluatorPlanner, exec)
return exec(newState)
}
func (e *evaluator) newState(planner *Planner, state *est.State) *est.State {
newState := planner.stateProvider()()
for _, valueAccessor := range e.parent.Type.ValueAccessors() {
valueAccessor.SetValue(newState.MemPtr, valueAccessor.Value(state.MemPtr))
}
newState.Buffer = state.Buffer
return newState
}
func evaluate(expr *op.Expression, cache *cache, parent *Planner) (est.New, error) {
return func(control est.Control) (est.Compute, error) {
x, err := expr.Operand(control)
if err != nil {
return nil, err
}
return (&evaluator{
x: x,
cache: cache,
control: control,
parent: parent,
}).compute, nil
}, nil
}