diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-24 10:11:42 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-24 10:11:42 +0300 |
| commit | a963fd6f854d476ec838fbde7ae3c28cd4270c97 (patch) | |
| tree | 600ecaebfc57e9b9ea0ecc7f07cf7dfe8f78d141 | |
| parent | 1c8c056ab7ce8c0ccd36ba0d954110789713204e (diff) | |
refactor(rpn): have ResultStack reuse pushLiteral
ResultStack reimplemented boolean and number parsing that already
existed in pushLiteral. This duplication also meant it missed
metric-suffixed numbers (e.g. 100Mbps) and @metric prefixes (e.g.
@GB).
Replace the duplicated logic with a call to pushLiteral, which
handles booleans, plain numbers, metric-suffixed numbers, and
@metric prefixes in one place.
| -rw-r--r-- | internal/rpn/rpn_ops.go | 21 |
1 files changed, 4 insertions, 17 deletions
diff --git a/internal/rpn/rpn_ops.go b/internal/rpn/rpn_ops.go index 8620197..caf1e87 100644 --- a/internal/rpn/rpn_ops.go +++ b/internal/rpn/rpn_ops.go @@ -5,7 +5,6 @@ package rpn import ( "fmt" - "strconv" "strings" ) @@ -26,22 +25,10 @@ func (r *RPN) ResultStack(tokens []string) (string, error) { stack := NewStack() for _, token := range tokens { - // Check if it's a boolean literal - if token == "true" { - stack.Push(NewFloatFromBool(true)) - continue - } - if token == "false" { - stack.Push(NewFloatFromBool(false)) - continue - } - - // Check if it's a number - if num, err := strconv.ParseFloat(token, 64); err == nil { - if stack.Len() >= r.maxStack { - return "", fmt.Errorf("stack overflow") - } - stack.Push(NewNumber(num, mode)) + // Push literal values (numbers, booleans, metric values, @metric prefix) + if pushed, err := r.pushLiteral(stack, token); err != nil { + return "", err + } else if pushed { continue } |
