diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-25 17:25:00 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-25 17:25:00 +0200 |
| commit | 6b5bee78c239b221188eea2bebe0e87714970866 (patch) | |
| tree | c35622e61aa629c0912621b21655fdb8c04cfa1c /internal/rpn/rpn_parse.go | |
| parent | f692d72dc840be8eb17857b2ef85ee42dde5fd3f (diff) | |
refactor: Refactor RPN to use Number interface uniformly for stack values
This commit refactors the internal/rpn package to use the Number interface
instead of the old Value struct for stack values. Key changes:
1. Updated Number interface to include IsBool() and Bool() methods for
boolean value support
2. Modified Float and Rat types to support boolean mode with:
- isBool and boolVal fields
- Float64() returns 1 for true, 0 for false
- String() returns 'true' or 'false' for boolean values
3. Updated Stack to use []Number instead of []Value
4. Updated all operations to use the Number interface methods directly
- Add, Sub, Mul, Div, Pow, Mod now use Float64() for values
5. Updated tests to use NewNumber() with mode parameter instead of
NewNumberValue(), and use Float64() instead of Number()
Benefits:
- Simplified code - no need for toNumber() and NewNumberValue() wrappers
- Better type safety - stack values are Number interface instances
- Boolean-to-number coercion works correctly in all operations
Diffstat (limited to 'internal/rpn/rpn_parse.go')
| -rw-r--r-- | internal/rpn/rpn_parse.go | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index e03426d..4686122 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -54,11 +54,11 @@ func (r *RPN) evaluate(tokens []string) (string, error) { // Check if it's a boolean literal if token == "true" { - stack.Push(NewBoolValue(true)) + stack.Push(NewFloatFromBool(true)) continue } if token == "false" { - stack.Push(NewBoolValue(false)) + stack.Push(NewFloatFromBool(false)) continue } @@ -67,7 +67,7 @@ func (r *RPN) evaluate(tokens []string) (string, error) { if stack.Len() >= r.maxStack { return "", fmt.Errorf("stack overflow") } - stack.Push(NewNumberValue(num)) + stack.Push(NewNumber(num, r.mode)) continue } @@ -115,7 +115,7 @@ 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(NewNumberValue(val)) + stack.Push(NewNumber(val, r.mode)) return "", nil } |
