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 }
func TestSymbolResolveAll(t *testing.T) { 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), } src := "$res=$gmtdate-1*$day" ret, err := symbol.Resolve(src, builtins) if err != nil { t.Error(err) return } res := ret[0] check := ast.NewLeftID("res", ast.NewDate(stdtime.Now().AddDate(0, 0, -1), "YYYYMMDD")) if !res.Equals(check) { t.Error(fmt.Errorf("%s=%d", src, res.Value.(string))) return } //fmt.Println(res) }
func NewBuiltins() *Builtins { return &Builtins{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), // System "job_report": ast.NewString("{}"), }} }
func (this *Eval) evalOperator(op1 *ast.Stmt, op2 *ast.Stmt, opstr string) (*ast.Stmt, error) { if op1.Type == ast.Int && op2.Type == ast.Int { var res int a := op1.Prop["value"].(int) b := op2.Prop["value"].(int) switch opstr { case "+": res = a + b case "-": res = a - b case "*": res = a * b case "/": res = a / b default: return nil, fmt.Errorf("invalid operator between Integers: %s", opstr) } return ast.NewInt(res), nil } if (op1.Type == ast.Float || op2.Type == ast.Int) && (op1.Type == ast.Int || op2.Type == ast.Float) { var res float64 var a float64 var b float64 var err error a, err = strconv.ParseFloat(op1.Value.(string), 64) if err != nil { return nil, fmt.Errorf("invalid float: %s", op1.Value) } b, err = strconv.ParseFloat(op2.Value.(string), 64) if err != nil { return nil, fmt.Errorf("invalid float: %s", op2.Value) } switch opstr { case "+": res = a + b case "-": res = a - b case "*": res = a * b case "/": res = a / b default: return nil, fmt.Errorf("invalid operator between Integers: %s", opstr) } return ast.NewFloat(res), nil } if op1.Type == ast.LeftID { if opstr != "=" { return nil, fmt.Errorf("invalid operator for LeftID: %s", opstr) } return ast.NewLeftID(op1.Value.(string), op2), nil } if op1.Type == ast.Date && op2.Type == ast.Duration { var ret stdtime.Time old := op1.Prop["time"].(stdtime.Time) duration := op2.Prop["time"].(stdtime.Duration) switch opstr { case "+": ret = old.Add(duration) case "-": ret = old.Add(-1 * duration) default: return nil, fmt.Errorf("invalid operator between Date and Duration: %s", opstr) } return ast.NewDate(ret, op1.Prop["format"].(string)), nil } if op1.Type == ast.Date && op2.Type == ast.DurationExt { var ret stdtime.Time old := op1.Prop["time"].(stdtime.Time) year := op2.Prop["year"].(int) month := op2.Prop["month"].(int) day := op2.Prop["day"].(int) switch opstr { case "+": ret = old.AddDate(year, month, day) case "-": ret = old.AddDate(-1*year, -1*month, -1*day) default: return nil, fmt.Errorf("invalid operator between Date and Duration: %s", opstr) } return ast.NewDate(ret, op1.Prop["format"].(string)), nil } if op1.Type == ast.Int && op2.Type == ast.Duration { if opstr != "*" { return nil, fmt.Errorf("invalid operator between Int and Duration: %s", opstr) } n := op1.Prop["value"].(int) return ast.NewDuration(stdtime.Duration(n) * op2.Prop["time"].(stdtime.Duration)), nil } if op1.Type == ast.Int && op2.Type == ast.DurationExt { if opstr != "*" { return nil, fmt.Errorf("invalid operator between Int and Duration: %s", opstr) } n := op1.Prop["value"].(int) return ast.NewDurationExt( n*op2.Prop["year"].(int), n*op2.Prop["month"].(int), n*op2.Prop["day"].(int)), nil } return nil, fmt.Errorf("invalid operation pair type: %s and %s", op1.Type, op2.Type) }