|
|
@ -4,9 +4,29 @@ import (
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type builtin func(...interface{}) (interface{}, error)
|
|
|
|
type builtin func([]interface{}) (interface{}, error)
|
|
|
|
|
|
|
|
|
|
|
|
func addition(vals ...interface{}) (interface{}, error) {
|
|
|
|
// evaluates all of the arguments, and then calls the function with the results
|
|
|
|
|
|
|
|
// of the evaluations
|
|
|
|
|
|
|
|
func (b *builtin) call(env *environment, rawArgs []interface{}) (interface{}, error) {
|
|
|
|
|
|
|
|
if rawArgs == nil {
|
|
|
|
|
|
|
|
return (*b)(nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// eval all arguments first
|
|
|
|
|
|
|
|
args := make([]interface{}, 0, len(rawArgs))
|
|
|
|
|
|
|
|
for _, raw := range rawArgs {
|
|
|
|
|
|
|
|
v, err := eval(raw, env)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, v)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (*b)(args)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func addition(vals []interface{}) (interface{}, error) {
|
|
|
|
a := accumulator{
|
|
|
|
a := accumulator{
|
|
|
|
name: "addition",
|
|
|
|
name: "addition",
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
@ -16,10 +36,10 @@ func addition(vals ...interface{}) (interface{}, error) {
|
|
|
|
return left + right, nil
|
|
|
|
return left + right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.total(vals...)
|
|
|
|
return a.total(vals)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func subtraction(vals ...interface{}) (interface{}, error) {
|
|
|
|
func subtraction(vals []interface{}) (interface{}, error) {
|
|
|
|
a := accumulator{
|
|
|
|
a := accumulator{
|
|
|
|
name: "subtraction",
|
|
|
|
name: "subtraction",
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
@ -29,10 +49,10 @@ func subtraction(vals ...interface{}) (interface{}, error) {
|
|
|
|
return left - right, nil
|
|
|
|
return left - right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.total(vals...)
|
|
|
|
return a.total(vals)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func multiplication(vals ...interface{}) (interface{}, error) {
|
|
|
|
func multiplication(vals []interface{}) (interface{}, error) {
|
|
|
|
a := accumulator{
|
|
|
|
a := accumulator{
|
|
|
|
name: "multiplication",
|
|
|
|
name: "multiplication",
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
@ -42,10 +62,10 @@ func multiplication(vals ...interface{}) (interface{}, error) {
|
|
|
|
return left * right, nil
|
|
|
|
return left * right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.total(vals...)
|
|
|
|
return a.total(vals)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func division(vals ...interface{}) (interface{}, error) {
|
|
|
|
func division(vals []interface{}) (interface{}, error) {
|
|
|
|
a := accumulator{
|
|
|
|
a := accumulator{
|
|
|
|
name: "division",
|
|
|
|
name: "division",
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
|
floatFn: func(left, right float64) (float64, error) {
|
|
|
@ -61,5 +81,5 @@ func division(vals ...interface{}) (interface{}, error) {
|
|
|
|
return left / right, nil
|
|
|
|
return left / right, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a.total(vals...)
|
|
|
|
return a.total(vals)
|
|
|
|
}
|
|
|
|
}
|
|
|
|