-
Notifications
You must be signed in to change notification settings - Fork 3
/
compile.go
63 lines (54 loc) · 1.37 KB
/
compile.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 velty
import (
"github.com/viant/velty/ast/stmt"
"github.com/viant/velty/est"
"github.com/viant/velty/parser"
"github.com/viant/xunsafe"
"reflect"
)
//Compile create Execution Plan and State provider for the Execution Plan.
func (p *Planner) Compile(template []byte) (*est.Execution, func() *est.State, error) {
root, err := parser.Parse(template)
if err != nil {
return nil, nil, err
}
exec, err := p.newExecution(root)
if err != nil {
return nil, nil, err
}
newState := p.stateProvider()
return exec, newState, err
}
func (p *Planner) stateProvider() func() *est.State {
return func() *est.State {
mem := reflect.New(p.Type.Type).Interface()
state := &est.State{
Mem: mem,
MemPtr: xunsafe.AsPointer(mem),
Buffer: est.NewBuffer(p.bufferSize, p.escapeHTML),
StateType: p.Type,
PanicOnError: p.panicOnError,
}
return state
}
}
func (p *Planner) newExecution(root *stmt.Block) (*est.Execution, error) {
compute, err := p.newCompute(root)
if err != nil {
return nil, err
}
exec := est.NewExecution(compute)
exec.PanicOnError = p.panicOnError
return exec, nil
}
func (p *Planner) newCompute(root *stmt.Block) (est.Compute, error) {
computeNew, err := p.compileBlock(root)
if err != nil {
return nil, err
}
compute, err := computeNew(*p.Control)
if err != nil {
return nil, err
}
return compute, nil
}