diff options
Diffstat (limited to 'op.go')
| -rw-r--r-- | op.go | 30 |
1 files changed, 16 insertions, 14 deletions
@@ -5,39 +5,41 @@ import ( "math" ) -// parseOp returns a closure that performs the specified arithmetic operation, +// parseOperator returns a closure that performs the specified arithmetic operation, // or OpError if op is not a valid operator. -func parseOp(op byte) (func(lhs *float64, rhs float64), error) { +func parseOperator(op byte) (func(lhs float64, rhs float64) float64, error) { switch op { case '+': - return func(lhs *float64, rhs float64) { *lhs += rhs }, nil + return func(lhs, rhs float64) float64 { return lhs + rhs }, nil case '-': - return func(lhs *float64, rhs float64) { *lhs -= rhs }, nil + return func(lhs, rhs float64) float64 { return lhs - rhs }, nil case '*': - return func(lhs *float64, rhs float64) { *lhs *= rhs }, nil + return func(lhs, rhs float64) float64 { return lhs * rhs }, nil case '/': - return func(lhs *float64, rhs float64) { + return func(lhs, rhs float64) float64 { if rhs != 0 { - *lhs /= rhs + return lhs / rhs } + return lhs }, nil case '%': - return func(lhs *float64, rhs float64) { + return func(lhs, rhs float64) float64 { if rhs != 0 { - *lhs = float64(int64(*lhs) % int64(rhs)) + return float64(int64(lhs) % int64(rhs)) } + return lhs }, nil case '^': - return func(lhs *float64, rhs float64) { *lhs = math.Pow(*lhs, rhs) }, nil + return func(lhs, rhs float64) float64 { return math.Pow(lhs, rhs) }, nil } - return nil, OpError{op} + return nil, OperatorErr{op} } -// OpError records an invalid arithmetic operator. -type OpError struct { +// OperatorErr records an invalid arithmetic operator. +type OperatorErr struct { c byte } -func (e OpError) Error() string { +func (e OperatorErr) Error() string { return fmt.Sprintf("invalid operator: %c", e.c) } |