aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-07-29 10:37:40 -0230
committerSam Anthony <sam@samanthony.xyz>2023-07-29 10:37:40 -0230
commit0e1cd2b6a328887c1a630eae0b34366a607bb1b8 (patch)
tree70825addc206c7b7e7fae33bbc0f86ab24534ee8
parent5b81647c27af77be3d35e10cb53cad1897a7f392 (diff)
downloadpfc-0e1cd2b6a328887c1a630eae0b34366a607bb1b8.zip
refactor
-rw-r--r--calculator.go54
-rw-r--r--op.go43
-rw-r--r--stack.go16
3 files changed, 59 insertions, 54 deletions
diff --git a/calculator.go b/calculator.go
index 3493382..b5dae10 100644
--- a/calculator.go
+++ b/calculator.go
@@ -1,27 +1,10 @@
package main
import (
- "fmt"
- "math"
"strconv"
"strings"
)
-type Stack []float64
-
-func (s *Stack) push(v float64) {
- *s = append(*s, v)
-}
-
-func (s *Stack) pop() *float64 {
- if len(*s) > 0 {
- v := (*s)[len(*s)-1]
- *s = (*s)[:len(*s)-1]
- return &v
- }
- return nil
-}
-
type Calculator struct {
stack Stack
buf string
@@ -79,40 +62,3 @@ func (c *Calculator) performOp(op byte) error {
c.buf = ""
return nil
}
-
-// parseOp 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) {
- switch op {
- case '+':
- return func(lhs *float64, rhs float64) { *lhs += rhs }, nil
- case '-':
- return func(lhs *float64, rhs float64) { *lhs -= rhs }, nil
- case '*':
- return func(lhs *float64, rhs float64) { *lhs *= rhs }, nil
- case '/':
- return func(lhs *float64, rhs float64) {
- if rhs != 0 {
- *lhs /= rhs
- }
- }, nil
- case '%':
- return func(lhs *float64, rhs float64) {
- if rhs != 0 {
- *lhs = float64(int64(*lhs) % int64(rhs))
- }
- }, nil
- case '^':
- return func(lhs *float64, rhs float64) { *lhs = math.Pow(*lhs, rhs) }, nil
- }
- return nil, OpError{op}
-}
-
-// OpError records an invalid arithmetic operator.
-type OpError struct {
- c byte
-}
-
-func (e OpError) Error() string {
- return fmt.Sprintf("invalid operator: %c", e.c)
-}
diff --git a/op.go b/op.go
new file mode 100644
index 0000000..ab57a84
--- /dev/null
+++ b/op.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+// parseOp 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) {
+ switch op {
+ case '+':
+ return func(lhs *float64, rhs float64) { *lhs += rhs }, nil
+ case '-':
+ return func(lhs *float64, rhs float64) { *lhs -= rhs }, nil
+ case '*':
+ return func(lhs *float64, rhs float64) { *lhs *= rhs }, nil
+ case '/':
+ return func(lhs *float64, rhs float64) {
+ if rhs != 0 {
+ *lhs /= rhs
+ }
+ }, nil
+ case '%':
+ return func(lhs *float64, rhs float64) {
+ if rhs != 0 {
+ *lhs = float64(int64(*lhs) % int64(rhs))
+ }
+ }, nil
+ case '^':
+ return func(lhs *float64, rhs float64) { *lhs = math.Pow(*lhs, rhs) }, nil
+ }
+ return nil, OpError{op}
+}
+
+// OpError records an invalid arithmetic operator.
+type OpError struct {
+ c byte
+}
+
+func (e OpError) Error() string {
+ return fmt.Sprintf("invalid operator: %c", e.c)
+}
diff --git a/stack.go b/stack.go
new file mode 100644
index 0000000..182d418
--- /dev/null
+++ b/stack.go
@@ -0,0 +1,16 @@
+package main
+
+type Stack []float64
+
+func (s *Stack) push(v float64) {
+ *s = append(*s, v)
+}
+
+func (s *Stack) pop() *float64 {
+ if len(*s) > 0 {
+ v := (*s)[len(*s)-1]
+ *s = (*s)[:len(*s)-1]
+ return &v
+ }
+ return nil
+}