diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-25 17:57:54 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-25 17:57:54 +0200 |
| commit | a3d3b676796f93f41f5b44b1d2b86b15f99080a0 (patch) | |
| tree | f8d6182cf95ca3816f9bbba251db0ce1ef77a802 /internal/rpn/variable.go | |
| parent | 31353ea7a3cb2f5ec5d14adcfaff840222185ae7 (diff) | |
Fix Ln operation and add comprehensive tests
- Fixed Ln operation to handle Value conversion before math.Log using Float64() which handles boolean conversion (true → 1, false → 0)
- Added TestLnWithBoolean and TestLnEdgeCases tests for comprehensive coverage
- Refactored operations.go into separate files (arithmetic.go, boolean_ops.go, hyper.go, stack.go, variable.go)
- Removed unused toNumber function from number.go
- Added Float64() method to Value struct for boolean conversion
Diffstat (limited to 'internal/rpn/variable.go')
| -rw-r--r-- | internal/rpn/variable.go | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/internal/rpn/variable.go b/internal/rpn/variable.go new file mode 100644 index 0000000..f3302df --- /dev/null +++ b/internal/rpn/variable.go @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 Paul Buetow + +package rpn + +import ( + "fmt" +) + +// VariableOperations provides variable management operator implementations. +type VariableOperations struct { + vars VariableStore +} + +// NewVariableOperations creates a new VariableOperations instance. +func NewVariableOperations(vars VariableStore) *VariableOperations { + return &VariableOperations{vars: vars} +} + +// AssignVariable assigns a value from the stack to a variable. +// Usage: `name value =` +func (o *VariableOperations) AssignVariable(stack *Stack, name string) error { + val, err := stack.Pop() + if err != nil { + return err + } + + // Convert Number to float64 for variable storage + return o.vars.SetVariable(name, val.Float64()) +} + +// UseVariable pushes a variable's value onto the stack. +// Usage: `varname` (pushes stored value) +func (o *VariableOperations) UseVariable(stack *Stack, name string) error { + if name == "" { + return fmt.Errorf("variable name cannot be empty") + } + + val, exists := o.vars.GetVariable(name) + if !exists { + return fmt.Errorf("%w: %s", ErrVariableNotFound, name) + } + + stack.Push(NewNumber(val, FloatMode)) + return nil +} + +// DeleteVariable removes a variable. +// Usage: `name d` +func (o *VariableOperations) DeleteVariable(name string) error { + if name == "" { + return fmt.Errorf("variable name cannot be empty") + } + + deleted := o.vars.DeleteVariable(name) + if !deleted { + return fmt.Errorf("%w: %s", ErrVariableNotFound, name) + } + return nil +} + +// ListVariables returns a string listing all variables. +// Usage: `vars` +func (o *VariableOperations) ListVariables() (string, error) { + return o.vars.FormatVariables(), nil +} + +// ClearVariables removes all variables. +// Usage: `clear` +func (o *VariableOperations) ClearVariables() { + o.vars.ClearVariables() +} |
