Exemplo n.º 1
0
func (self *Mult) Apply(args []value.Value) value.Value {
	var val1 int64 = 1
	var val2 float64 = 1
	isfloat := false

	for _, arg := range args {
		switch arg.(type) {
		case *value.IntValue:
			val1 *= arg.(*value.IntValue).Value
		case *value.FloatValue:
			isfloat = true
			val2 *= arg.(*value.FloatValue).Value
		default:
			panic(fmt.Sprint("incorrect argument type for '*' : ", arg))
		}
	}
	if !isfloat {
		return value.NewIntValue(val1)
	} else {
		return value.NewFloatValue(float64(val1) * val2)
	}
}
Exemplo n.º 2
0
func (self *Sub) Apply(args []value.Value) value.Value {
	var val float64
	isfloat := false

	if len(args) == 0 {
		panic(fmt.Sprint("`-' argument unmatch: expected at least 1"))
	} else if len(args) > 1 {
		switch args[0].(type) {
		case *value.IntValue:
			val = float64(args[0].(*value.IntValue).Value)
		case *value.FloatValue:
			val = args[0].(*value.FloatValue).Value
		default:
			panic(fmt.Sprint("incorrect argument type for `-' : ", args[0]))
		}
		args = args[1:]
	}

	for _, arg := range args {
		switch arg.(type) {
		case *value.IntValue:
			val -= float64(arg.(*value.IntValue).Value)
		case *value.FloatValue:
			isfloat = true
			val -= arg.(*value.FloatValue).Value
		default:
			panic(fmt.Sprint("incorrect argument type for `-' : ", arg))
		}

	}
	if isfloat {
		return value.NewFloatValue(val)
	} else {
		return value.NewIntValue(int64(val))
	}
}
Exemplo n.º 3
0
func (self *Int) Eval(env *scope.Scope) value.Value {
	return value.NewIntValue(self.Value)
}