diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-22 12:33:34 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-22 12:33:34 +0300 |
| commit | 912c8effc82cd185d7489e21482a216289288480 (patch) | |
| tree | dfd9a946c6118a0831e8af4d3bcd2f7a7f748e68 | |
| parent | e7b97833bfb1415d147d30d119052a56bb39967b (diff) | |
| parent | 9a68d3658e9d4f71121f90a1e0489898fea849f5 (diff) | |
Merge branch 'develop': eliminate duplicated prefixMode state
Task be: prefixMode now lives in Operations only. RPN delegates
GetPrefixMode/SetPrefixMode through the Operator interface.
Removed direct r.prefixMode writes from evaluate().
| -rw-r--r-- | internal/rpn/operations.go | 2 | ||||
| -rw-r--r-- | internal/rpn/rpn_parse.go | 5 | ||||
| -rw-r--r-- | internal/rpn/rpn_state.go | 8 |
3 files changed, 5 insertions, 10 deletions
diff --git a/internal/rpn/operations.go b/internal/rpn/operations.go index 04c2014..d45ffd1 100644 --- a/internal/rpn/operations.go +++ b/internal/rpn/operations.go @@ -158,6 +158,8 @@ type Operator interface { SetMode(CalculationMode) // SetPrefixMode sets the prefix mode for data size calculations SetPrefixMode(PrefixMode) + // GetPrefixMode returns the current prefix mode + GetPrefixMode() PrefixMode // Metric command handlers MetricShow(stack *Stack) (string, error) MetricList(stack *Stack) (string, error) diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index 7abd41c..ab64ec7 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -367,17 +367,12 @@ func (r *RPN) evaluate(input string, tokens []string) (string, error) { return result, nil case "binary": if i+2 < len(tokens) && tokens[i+2] == "set" { - // Lock order: r.mu → o.mu (same as RPN.SetPrefixMode) - // No deadlock risk: both paths acquire in the same direction. - // r.mu is held by evaluate(); o.mu is a separate mutex on Operations. - r.prefixMode = IEC r.ops.SetPrefixMode(IEC) return "prefix mode: IEC", nil } return "", fmt.Errorf("rpn: metric binary: use 'metric binary set'") case "decimal": if i+2 < len(tokens) && tokens[i+2] == "set" { - r.prefixMode = SI r.ops.SetPrefixMode(SI) return "prefix mode: SI", nil } diff --git a/internal/rpn/rpn_state.go b/internal/rpn/rpn_state.go index 3b63984..a146409 100644 --- a/internal/rpn/rpn_state.go +++ b/internal/rpn/rpn_state.go @@ -20,7 +20,6 @@ type RPN struct { maxStack int currentStack *Stack mode CalculationMode - prefixMode PrefixMode } // NewRPN creates a new RPN parser and evaluator with the given variable store. @@ -37,7 +36,6 @@ func NewRPN(vars VariableStore) *RPN { maxStack: 1000, // Reasonable limit for RPN expressions currentStack: NewStack(), mode: FloatMode, // Default mode - prefixMode: SI, // Default prefix mode } } @@ -96,19 +94,19 @@ func (r *RPN) Stack() []Number { } // SetPrefixMode sets the prefix mode (SI or IEC). -// Propagates to the Operations instance so arithmetic uses the correct mode. +// Delegates to the Operations instance. // This method is thread-safe for writes. func (r *RPN) SetPrefixMode(mode PrefixMode) { r.mu.Lock() defer r.mu.Unlock() - r.prefixMode = mode r.ops.SetPrefixMode(mode) } // GetPrefixMode returns the current prefix mode. +// Delegates to the Operations instance. // This method is thread-safe for concurrent reads. func (r *RPN) GetPrefixMode() PrefixMode { r.mu.RLock() defer r.mu.RUnlock() - return r.prefixMode + return r.ops.GetPrefixMode() } |
