Exemplo n.º 1
0
func getTestEvalResult(src string, t *testing.T) []*ast.Stmt {
	var builtins = map[string]*ast.Stmt{
		// Date
		"gmtdate": ast.NewDate(stdtime.Now(), "YYYYMMDD"),
		"bizdate": ast.NewDate(stdtime.Now().AddDate(0, 0, -1), "YYYYMMDD"),
		// Duration
		"year":   ast.NewDurationExt(1, 0, 0),
		"month":  ast.NewDurationExt(0, 1, 0),
		"day":    ast.NewDurationExt(0, 0, 1),
		"hour":   ast.NewDuration(stdtime.Hour),
		"minute": ast.NewDuration(stdtime.Minute),
		"second": ast.NewDuration(stdtime.Second),
	}

	p := parser.NewParser()
	l := lexer.NewLexer([]byte(src))
	a, err := p.Parse(l)
	if err != nil {
		t.Error(err)
		return nil
	}
	e := eval.NewEval()
	res, err := e.Evaluate(a.([]*ast.Stmt), builtins)
	if err != nil {
		t.Error(err)
		return nil
	}
	return res
}
Exemplo n.º 2
0
func Resolve(src string, builtins map[string]*ast.Stmt) ([]*ast.Stmt, error) {
	e := eval.NewEval()
	if len(src) == 0 {
		return e.Evaluate([]*ast.Stmt{}, builtins)
	}
	p := parser.NewParser()
	l := lexer.NewLexer([]byte(src))
	a, err := p.Parse(l)
	if err != nil {
		return nil, err
	}
	return e.Evaluate(a.([]*ast.Stmt), builtins)
}