Example #1
0
func (self *Div) Call(args ...value.Value) value.Value {
	var result float64

	for i, item := range args {
		switch operand := item.(type) {
		case *value.Int:
			if operand.Value == 0 {
				panic(fmt.Errorf("%v: division by zero", self.String()))
			}
			if i == 0 {
				result = float64(operand.Value)
			} else {
				result /= float64(operand.Value)
			}

		case *value.Float:
			if operand.Value == 0 {
				panic(fmt.Errorf("%v: division by zero", self.String()))
			}
			if i == 0 {
				result = operand.Value
			} else {
				result /= operand.Value
			}

		default:
			panic(fmt.Errorf("%v: unexpected number", self.String()))
		}
	}
	return value.NewFloat(result)
}
Example #2
0
func (self *Mul) Call(args ...value.Value) value.Value {
	var result float64 = 1
	isfloat := false

	for _, item := range args {
		switch operand := item.(type) {
		case *value.Int:
			result *= float64(operand.Value)
		case *value.Float:
			isfloat = true
			result *= operand.Value
		default:
			panic(fmt.Errorf("%v: unexpected number", self.String()))
		}
	}
	if isfloat {
		return value.NewFloat(result)
	}
	return value.NewInt(int64(result))
}
Example #3
0
File: float.go Project: ctliu3/Gosp
func (self *Float) Eval(env *scope.Scope) value.Value {
	return value.NewFloat(self.value)
}