aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--calc.go10
-rw-r--r--op.go9
-rw-r--r--ui.go24
3 files changed, 30 insertions, 13 deletions
diff --git a/calc.go b/calc.go
index 67794aa..1e0e598 100644
--- a/calc.go
+++ b/calc.go
@@ -61,17 +61,13 @@ func (c *Calculator) negate() {
}
}
-// performOp performs the specified arithmetic operation.
-func (c *Calculator) performOperation(operator byte) {
- fn, err := parseOperator(operator)
- if err != nil {
- return
- }
+// operate performs a binary arithmetic operation on the stored values.
+func (c *Calculator) operate(op Op) {
lhs, rhs, err := c.operands()
if err != nil {
return
}
- c.stack.push(fn(lhs, rhs))
+ c.stack.push(op(lhs, rhs))
c.buf = ""
}
diff --git a/op.go b/op.go
index 6ccc6a8..3a193b6 100644
--- a/op.go
+++ b/op.go
@@ -5,9 +5,12 @@ import (
"math"
)
-// parseOperator returns a closure that performs the specified arithmetic operation,
-// or OpError if op is not a valid operator.
-func parseOperator(op byte) (func(lhs float64, rhs float64) float64, error) {
+// Op is a binary operator.
+type Op func(lhs, rhs float64) float64
+
+// parseOp parses a binary operator, returning a function that performs the operation, or
+// OpError if op is not a valid operator.
+func parseOperator(op byte) (Op, error) {
switch op {
case '+':
return func(lhs, rhs float64) float64 { return lhs + rhs }, nil
diff --git a/ui.go b/ui.go
index cea51e7..02f2bb1 100644
--- a/ui.go
+++ b/ui.go
@@ -2,6 +2,7 @@ package main
import (
"fmt"
+ "strings"
"github.com/charmbracelet/bubbletea"
)
@@ -52,9 +53,22 @@ func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
ui.calc.angleMode = !ui.calc.angleMode
case "N":
ui.calc.negate()
- case "+", "-", "*", "/", "%", "^":
- operator := msg.String()[0]
- ui.calc.performOperation(operator)
+ case "+", "-":
+ if isENotation(ui.calc.buf) {
+ ui.calc.buf += msg.String()
+ } else {
+ op, err := parseOperator(msg.String()[0])
+ if err != nil {
+ panic(err)
+ }
+ ui.calc.operate(op)
+ }
+ case "*", "/", "%", "^":
+ op, err := parseOperator(msg.String()[0])
+ if err != nil {
+ panic(err)
+ }
+ ui.calc.operate(op)
case "backspace":
if len(ui.calc.buf) > 0 {
ui.calc.buf = ui.calc.buf[:len(ui.calc.buf)-1]
@@ -75,6 +89,10 @@ func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return ui, nil
}
+func isENotation(s string) bool {
+ return strings.ContainsRune(s, 'e')
+}
+
func (ui UI) View() string {
s := padding(ui)