summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-25 17:13:01 +0200
committerPaul Buetow <paul@buetow.org>2026-03-25 17:13:01 +0200
commit6d65520aea89e111398e2e7d3ca9b26c4c665c25 (patch)
tree9737d1eff927b53a211ba1260774e07c2e65031d
parentee989519753ba13f156a6763caa38305773fd042 (diff)
refactor: Update GetCurrentStack to return []Value instead of []float64
- Changed GetCurrentStack() to return []Value to preserve value types - Updated test to use Value.Number() method for comparison - Boolean values are now correctly preserved in stack inspection This ensures that boolean values returned from comparison operators (gt, lt, etc.) are preserved when inspecting the stack.
-rw-r--r--internal/rpn/rpn_state.go10
-rw-r--r--internal/rpn/rpn_test.go5
2 files changed, 6 insertions, 9 deletions
diff --git a/internal/rpn/rpn_state.go b/internal/rpn/rpn_state.go
index 30cf1cf..eb54add 100644
--- a/internal/rpn/rpn_state.go
+++ b/internal/rpn/rpn_state.go
@@ -39,16 +39,12 @@ func (r *RPN) SetMode(mode CalculationMode) {
}
// GetCurrentStack returns a copy of the current stack for inspection.
-func (r *RPN) GetCurrentStack() []float64 {
+// Returns []Value to preserve value types (numbers and booleans).
+func (r *RPN) GetCurrentStack() []Value {
if r.currentStack == nil {
return nil
}
- values := r.currentStack.Values()
- result := make([]float64, len(values))
- for i, v := range values {
- result[i] = v.Number()
- }
- return result
+ return r.currentStack.Values()
}
// SetCurrentStack sets the current stack from a slice of values.
diff --git a/internal/rpn/rpn_test.go b/internal/rpn/rpn_test.go
index e3bf43e..c529ffa 100644
--- a/internal/rpn/rpn_test.go
+++ b/internal/rpn/rpn_test.go
@@ -755,7 +755,8 @@ func TestRPNGetCurrentStack(t *testing.T) {
if len(stack) != 3 {
t.Errorf("Stack length = %d, want 3", len(stack))
}
- if stack[0] != 1 || stack[1] != 2 || stack[2] != 3 {
+ // Use Number() method to compare values
+ if stack[0].Number() != 1 || stack[1].Number() != 2 || stack[2].Number() != 3 {
t.Errorf("Stack = %v, want [1 2 3]", stack)
}
}
@@ -1210,7 +1211,7 @@ func TestRPNStackPreservation(t *testing.T) {
// Stack should preserve 3
stack := rpnCalc.GetCurrentStack()
- if len(stack) != 1 || stack[0] != 3.0 {
+ if len(stack) != 1 || stack[0].Number() != 3.0 {
t.Errorf("Stack should be [3], got %v", stack)
}