summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-24 11:24:52 +0300
committerPaul Buetow <paul@buetow.org>2026-05-24 11:24:52 +0300
commit33aa907994ff4cb7bdd83e005806d8301d829978 (patch)
treef2e7ee1dc07bcd2ba4e1f83333fcb4236c1fbf33
parent871f7fa2b9a0fd5653adadfc9efb90fcbd2da203 (diff)
rpn: remove duplicate metricRegistry, use ops.MetricRegistry()
-rw-r--r--internal/rpn/operations_interfaces.go2
-rw-r--r--internal/rpn/rpn_parse.go4
-rw-r--r--internal/rpn/rpn_state.go22
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(),
}
}