aboutsummaryrefslogtreecommitdiffstats
path: root/op.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-10-01 20:02:22 -0400
committerSam Anthony <sam@samanthony.xyz>2023-10-01 20:02:22 -0400
commit4f9e220638ea62443db25d252c4e12ede2c7f9c5 (patch)
treeed40edf989f2907e58e3d5fd0fd0a85cde919386 /op.go
parent6e3073f5e835b19b39d0853dece386067218b1ce (diff)
downloadpfc-4f9e220638ea62443db25d252c4e12ede2c7f9c5.zip
error handling and tidying
Diffstat (limited to 'op.go')
-rw-r--r--op.go30
1 files changed, 16 insertions, 14 deletions
diff --git a/op.go b/op.go
index ab57a84..6ccc6a8 100644
--- a/op.go
+++ b/op.go
@@ -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)
}