Exemplo n.º 1
0
func (self *Mod) Apply(args []value.Value) value.Value {
	if len(args) != 2 {
		panic(fmt.Sprint("argument mismatch for `%', expected 2, given: ", len(args)))
	}
	if v1, ok := args[0].(*value.IntValue); ok {
		if v2, ok := args[1].(*value.IntValue); ok {
			if v2.Value == 0 {
				panic(fmt.Sprint("remainder: undefined for 0"))
			}
			return value.NewIntValue(v1.Value % v2.Value)
		}
	}
	panic(fmt.Sprint("incorrect argument type for `%', expected integer?"))
}
Exemplo n.º 2
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.º 3
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.º 4
0
func (self *Int) Eval(env *scope.Scope) value.Value {
	return value.NewIntValue(self.Value)
}