diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-24 00:17:22 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-24 00:17:22 +0300 |
| commit | 7e1c99cc79ba8338a05e37aaac498dd759f614f3 (patch) | |
| tree | a477b22042627af40708cd7fb4e3275360f01365 /internal | |
| parent | 11272232772fcfa20f15e12efe37aad580ab3d8c (diff) | |
rpn: eliminate RPN.mode duplication with Operations.mode
Remove the mode field from the RPN struct, which was duplicated from
Operations.mode. All reads of r.mode in rpn_parse.go and rpn_ops.go
now go through r.ops.GetMode().
- Add GetMode() to the Operator interface so RPN can access mode
through its Operator dependency
- Remove mode field from RPN struct in rpn_state.go
- Remove mode initialization from NewRPN
- Update SetMode to only set mode on Operations
- Update GetMode to delegate to Operations
- Replace r.mode with r.ops.GetMode() in rpn_parse.go (5 sites)
- Replace r.mode with r.ops.GetMode() in rpn_ops.go (1 site)
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/rpn/operations_interfaces.go | 2 | ||||
| -rw-r--r-- | internal/rpn/rpn_ops.go | 2 | ||||
| -rw-r--r-- | internal/rpn/rpn_parse.go | 10 | ||||
| -rw-r--r-- | internal/rpn/rpn_state.go | 5 |
4 files changed, 9 insertions, 10 deletions
diff --git a/internal/rpn/operations_interfaces.go b/internal/rpn/operations_interfaces.go index 6076ec0..002ec33 100644 --- a/internal/rpn/operations_interfaces.go +++ b/internal/rpn/operations_interfaces.go @@ -86,6 +86,8 @@ type Operator interface { PowerIntOperator // SetMode sets the calculation mode (e.g., FloatMode, RationalMode). SetMode(CalculationMode) + // GetMode returns the current calculation mode. + GetMode() CalculationMode // SetPrefixMode sets the prefix mode for data size calculations SetPrefixMode(PrefixMode) // GetPrefixMode returns the current prefix mode diff --git a/internal/rpn/rpn_ops.go b/internal/rpn/rpn_ops.go index 5f0863a..8620197 100644 --- a/internal/rpn/rpn_ops.go +++ b/internal/rpn/rpn_ops.go @@ -20,7 +20,7 @@ func Tokenize(input string) []string { // This is useful for commands that need to show the stack without consuming it. func (r *RPN) ResultStack(tokens []string) (string, error) { r.mu.RLock() - mode := r.mode + mode := r.ops.GetMode() r.mu.RUnlock() stack := NewStack() diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index 2eaab04..615e804 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -388,12 +388,12 @@ func (r *RPN) handleOperator(stack *Stack, token string, tokenIndex int) (string // Check if it's a variable reference first (before operators) if val, exists := r.vars.GetVariable(token); exists { - stack.Push(NewNumber(val, r.mode)) + stack.Push(NewNumber(val, r.ops.GetMode())) return "", nil } // Check if it's a constant reference (before operators) if val, exists := r.consts.GetConstant(token); exists { - stack.Push(NewNumber(val, r.mode)) + stack.Push(NewNumber(val, r.ops.GetMode())) return "", nil } @@ -486,7 +486,7 @@ func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { if stack.Len() >= r.maxStack { return false, fmt.Errorf("stack overflow") } - stack.Push(NewNumber(num, r.mode)) + stack.Push(NewNumber(num, r.ops.GetMode())) return true, nil } @@ -495,7 +495,7 @@ func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { if stack.Len() >= r.maxStack { return false, fmt.Errorf("stack overflow") } - stack.Push(NewNumberWithMetric(num, r.mode, metric)) + stack.Push(NewNumberWithMetric(num, r.ops.GetMode(), metric)) return true, nil } @@ -507,7 +507,7 @@ func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { if stack.Len() >= r.maxStack { return false, fmt.Errorf("stack overflow") } - stack.Push(NewNumberWithMetric(1, r.mode, metric)) + stack.Push(NewNumberWithMetric(1, r.ops.GetMode(), metric)) return true, nil } return false, fmt.Errorf("unknown metric %q in %q", metricName, token) diff --git a/internal/rpn/rpn_state.go b/internal/rpn/rpn_state.go index baf64de..647b5c3 100644 --- a/internal/rpn/rpn_state.go +++ b/internal/rpn/rpn_state.go @@ -19,7 +19,6 @@ type RPN struct { assignHandler *assignmentHandler maxStack int currentStack *Stack - mode CalculationMode metricRegistry *MetricRegistry } @@ -42,7 +41,6 @@ func NewRPN(vars VariableStore, reg ...*MetricRegistry) *RPN { assignHandler: newAssignmentHandler(), maxStack: 1000, // Reasonable limit for RPN expressions currentStack: NewStack(), - mode: FloatMode, // Default mode metricRegistry: r, } } @@ -58,7 +56,7 @@ func (r *RPN) GetConstants() ConstantsProvider { func (r *RPN) GetMode() CalculationMode { r.mu.RLock() defer r.mu.RUnlock() - return r.mode + return r.ops.GetMode() } // SetMode sets the calculation mode. @@ -66,7 +64,6 @@ func (r *RPN) GetMode() CalculationMode { func (r *RPN) SetMode(mode CalculationMode) { r.mu.Lock() defer r.mu.Unlock() - r.mode = mode r.ops.SetMode(mode) } |
