diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-11 20:58:46 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-11 20:58:46 +0300 |
| commit | 4efc8d2c03e91ea330cbb9366a60e7405ebca8de (patch) | |
| tree | 538259a91f7c87971cda78cd881e6c74343238ee /internal/rpn/rpn_parse.go | |
| parent | 4465f7abdc72887a422b2ddd9afc43ee811b2e9b (diff) | |
Refactor number.go Float64() to return errors
- Float64() now returns (float64, error) instead of just float64
- All arithmetic methods (Add, Sub, Mul, Div, Pow, Mod, Compare) return errors
- IsString() and IsSymbol() added to Number interface
- Float and Rat now implement IsString() and IsSymbol()
- StringNum and Symbol updated to implement complete Number interface
- IsZero() updated to use val, _ := Float64() pattern
- ToFloat() updated to ignore error from Float64()}
Diffstat (limited to 'internal/rpn/rpn_parse.go')
| -rw-r--r-- | internal/rpn/rpn_parse.go | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index bf9a613..19e35fa 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -345,13 +345,17 @@ func (r *RPN) evaluate(input string, tokens []string) (string, error) { if err != nil { return "", fmt.Errorf("insufficient operands for %s: stack is empty", nextToken) } - if err := r.vars.SetVariable(token, val.Float64()); err != nil { + valF, err := val.Float64() + if err != nil { + return "", fmt.Errorf("failed to get float64 value for variable %q: %w", token, err) + } + if err := r.vars.SetVariable(token, valF); err != nil { return "", fmt.Errorf("failed to set variable %q: %w", token, err) } // Skip the operator token (next one) since we handled it inline // We've consumed both tokens, so we're done // Return confirmation message showing the assignment - return fmt.Sprintf("%s = %.10g", token, val.Float64()), nil + return fmt.Sprintf("%s = %.10g", token, valF), nil } else if _, err := strconv.ParseFloat(token, 64); err != nil && isValidIdentifier(token) { // This token is a variable name (not a number) shouldPushName = true |
