コード例 #1
0
ファイル: eval_test.go プロジェクト: yonglehou/hpipe
func TestEvalAddFloat(t *testing.T) {
	src := "$res=1.1+2.2"
	//fmt.Printf("src: %s\n", src)
	ret := getTestEvalResult(src, t)
	if ret == nil {
		return
	}
	res := ret[0]
	check := ast.NewLeftID("res", ast.NewFloat(3.3))
	if !res.Equals(check) {
		t.Error(fmt.Errorf("%s => %v", src, res.Value))
		return
	}
	//fmt.Printf("res: %v\n", res)
}
コード例 #2
0
ファイル: eval.go プロジェクト: yonglehou/hpipe
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)
}