1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-06-17 00:07:55 +02:00

Hello world

This commit is contained in:
Tim Voronov
2018-09-18 16:42:38 -04:00
parent c872f4565b
commit e02e861240
132 changed files with 22163 additions and 2 deletions

68
pkg/compiler/compiler.go Normal file
View File

@ -0,0 +1,68 @@
package compiler
import (
"github.com/MontFerret/ferret/pkg/parser"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/stdlib"
"github.com/pkg/errors"
)
type FqlCompiler struct {
funcs map[string]core.Function
}
func New() *FqlCompiler {
return &FqlCompiler{
stdlib.NewLib(),
}
}
func (c *FqlCompiler) RegisterFunction(name string, fun core.Function) error {
_, exists := c.funcs[name]
if exists {
return errors.Errorf("function already exists: %s", name)
}
c.funcs[name] = fun
return nil
}
func (c *FqlCompiler) Compile(query string) (program *runtime.Program, err error) {
if query == "" {
return nil, ErrEmptyQuery
}
p := parser.New(query)
p.AddErrorListener(&errorListener{})
defer func() {
if r := recover(); r != nil {
// find out exactly what the error was and set err
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("unknown panic")
}
program = nil
}
}()
l := newVisitor(c.funcs)
res := p.Visit(l).(*result)
if res.Ok() {
program = res.Data().(*runtime.Program)
} else {
err = res.Error()
}
return program, err
}