From 33aa907994ff4cb7bdd83e005806d8301d829978 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 24 May 2026 11:24:52 +0300 Subject: rpn: remove duplicate metricRegistry, use ops.MetricRegistry() --- internal/rpn/operations_interfaces.go | 2 ++ internal/rpn/rpn_parse.go | 4 ++-- internal/rpn/rpn_state.go | 22 ++++++++-------------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/internal/rpn/operations_interfaces.go b/internal/rpn/operations_interfaces.go index 6af0973..b254a26 100644 --- a/internal/rpn/operations_interfaces.go +++ b/internal/rpn/operations_interfaces.go @@ -102,4 +102,6 @@ type Operator interface { CustomList(stack *Stack) (string, error) CustomDefine(name string, factor float64, category string) error CustomUndefine(name string) error + // MetricRegistry returns the metric registry used by this Operations instance. + MetricRegistry() *MetricRegistry } diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index 8775f93..007ec00 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -483,7 +483,7 @@ func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { } // Check if it's a number with a metric suffix (e.g., 100Mbps, 5.5GB, 2hr) - if num, metric, ok := parseNumberWithMetric(token, r.metricRegistry); ok { + if num, metric, ok := parseNumberWithMetric(token, r.ops.MetricRegistry()); ok { if stack.Len() >= r.maxStack { return false, fmt.Errorf("stack overflow") } @@ -495,7 +495,7 @@ func (r *RPN) pushLiteral(stack *Stack, token string) (bool, error) { // Pushes a Number with value 1 and the looked-up metric if len(token) > 1 && token[0] == '@' { metricName := token[1:] - if metric, ok := r.metricRegistry.FindWithAliases(metricName); ok { + if metric, ok := r.ops.MetricRegistry().FindWithAliases(metricName); ok { if stack.Len() >= r.maxStack { return false, fmt.Errorf("stack overflow") } diff --git a/internal/rpn/rpn_state.go b/internal/rpn/rpn_state.go index 647b5c3..474f164 100644 --- a/internal/rpn/rpn_state.go +++ b/internal/rpn/rpn_state.go @@ -19,29 +19,23 @@ type RPN struct { assignHandler *assignmentHandler maxStack int currentStack *Stack - metricRegistry *MetricRegistry } // NewRPN creates a new RPN parser and evaluator with the given variable store. // If no registry is provided, defaults to the global MetricRegistry. func NewRPN(vars VariableStore, reg ...*MetricRegistry) *RPN { consts := NewConstants() - r := GetMetricRegistry() - if len(reg) > 0 && reg[0] != nil { - r = reg[0] - } - ops := NewOperations(vars, r) + ops := NewOperations(vars, reg...) ops.SetMode(FloatMode) // Set default mode ops.SetConstants(consts) // Share the same ConstantsProvider return &RPN{ - vars: vars, - consts: consts, - ops: ops, - opRegistry: NewOperatorRegistry(ops), - assignHandler: newAssignmentHandler(), - maxStack: 1000, // Reasonable limit for RPN expressions - currentStack: NewStack(), - metricRegistry: r, + vars: vars, + consts: consts, + ops: ops, + opRegistry: NewOperatorRegistry(ops), + assignHandler: newAssignmentHandler(), + maxStack: 1000, // Reasonable limit for RPN expressions + currentStack: NewStack(), } } -- cgit v1.2.3