diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-25 17:57:54 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-25 17:57:54 +0200 |
| commit | a3d3b676796f93f41f5b44b1d2b86b15f99080a0 (patch) | |
| tree | f8d6182cf95ca3816f9bbba251db0ce1ef77a802 /internal/rpn/stack.go | |
| parent | 31353ea7a3cb2f5ec5d14adcfaff840222185ae7 (diff) | |
Fix Ln operation and add comprehensive tests
- Fixed Ln operation to handle Value conversion before math.Log using Float64() which handles boolean conversion (true → 1, false → 0)
- Added TestLnWithBoolean and TestLnEdgeCases tests for comprehensive coverage
- Refactored operations.go into separate files (arithmetic.go, boolean_ops.go, hyper.go, stack.go, variable.go)
- Removed unused toNumber function from number.go
- Added Float64() method to Value struct for boolean conversion
Diffstat (limited to 'internal/rpn/stack.go')
| -rw-r--r-- | internal/rpn/stack.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/internal/rpn/stack.go b/internal/rpn/stack.go new file mode 100644 index 0000000..a956902 --- /dev/null +++ b/internal/rpn/stack.go @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 Paul Buetow + +package rpn + +import ( + "fmt" +) + +// StackOperations provides stack manipulation operator implementations. +type StackOperations struct { +} + +// NewStackOperations creates a new StackOperations instance. +func NewStackOperations() *StackOperations { + return &StackOperations{} +} + +// Dup duplicates the top stack value. +func (o *StackOperations) Dup(stack *Stack) error { + val, err := stack.Peek() + if err != nil { + return fmt.Errorf("insufficient operands for dup: %w", err) + } + stack.Push(val) + return nil +} + +// Swap swaps the top two stack values. +func (o *StackOperations) Swap(stack *Stack) error { + b, err := stack.Pop() + if err != nil { + return fmt.Errorf("insufficient operands for swap: %w", err) + } + + a, err := stack.Pop() + if err != nil { + return fmt.Errorf("insufficient operands for swap: %w", err) + } + + // Push in swapped order + stack.Push(b) + stack.Push(a) + return nil +} + +// Pop removes the top stack value. +func (o *StackOperations) Pop(stack *Stack) error { + _, err := stack.Pop() + if err != nil { + return fmt.Errorf("insufficient operands for pop: %w", err) + } + return nil +} + +// Show returns the current stack state as a string without modifying it. +func (o *StackOperations) Show(stack *Stack) (string, error) { + if stack.Len() == 0 { + return "", fmt.Errorf("empty stack") + } + // For now, just return the top value as a string + // In a full implementation, this would show the entire stack + val, err := stack.Peek() + if err != nil { + return "", err + } + return val.String(), nil +} |
