feat(query): split compile into two functions to allow Eval access

pull/10616/head
Lyon Hill 2018-07-17 16:03:29 -06:00
parent b665290f2c
commit ec2764f8d0
1 changed files with 25 additions and 16 deletions

View File

@ -42,29 +42,17 @@ func Compile(ctx context.Context, q string, now time.Time, opts ...Option) (*Spe
for _, opt := range opts {
opt(o)
}
s, _ := opentracing.StartSpanFromContext(ctx, "parse")
astProg, err := parser.NewAST(q)
if err != nil {
itrp := NewInterpreter()
itrp.SetOption(nowOption, nowFunc(now))
if err := Eval(itrp, q); err != nil {
return nil, err
}
s.Finish()
s, _ = opentracing.StartSpanFromContext(ctx, "compile")
defer s.Finish()
itrp := NewInterpreter()
itrp.SetOption(nowOption, nowFunc(now))
_, decls := builtIns(itrp)
// Convert AST program to a semantic program
semProg, err := semantic.New(astProg, decls)
if err != nil {
return nil, err
}
if err := itrp.Eval(semProg); err != nil {
return nil, err
}
spec := toSpec(itrp)
if o.verbose {
@ -73,6 +61,27 @@ func Compile(ctx context.Context, q string, now time.Time, opts ...Option) (*Spe
return spec, nil
}
func Eval(itrp *interpreter.Interpreter, q string) error {
astProg, err := parser.NewAST(q)
if err != nil {
return err
}
_, decls := builtIns(itrp)
// Convert AST program to a semantic program
semProg, err := semantic.New(astProg, decls)
if err != nil {
return err
}
if err := itrp.Eval(semProg); err != nil {
return err
}
return nil
}
// NewInterpreter returns an interpreter instance with
// pre-constructed options and global scopes.
func NewInterpreter() *interpreter.Interpreter {