summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-24 14:08:41 +0300
committerPaul Buetow <paul@buetow.org>2026-05-24 14:08:41 +0300
commitf02d0996bcaee49a4226ba678788e7550b06be29 (patch)
treef9c8eecdb36b95d4e28fc00c7aa1d913ebcd2387
parentf3a0760739049a42be7b446eab1d4d085ebe2702 (diff)
refactor(rpn): extract 'd' operator into Delete method (pj)
Move the inline logic for the 'd' (delete variable) operator into a proper Operations.Delete method, following the same registration pattern as all other operators. Adds Delete to the StackOperator interface.
-rw-r--r--internal/rpn/operations_interfaces.go1
-rw-r--r--internal/rpn/operations_variables.go20
-rw-r--r--internal/rpn/operator_registry.go17
3 files changed, 22 insertions, 16 deletions
diff --git a/internal/rpn/operations_interfaces.go b/internal/rpn/operations_interfaces.go
index cc60db2..39edcad 100644
--- a/internal/rpn/operations_interfaces.go
+++ b/internal/rpn/operations_interfaces.go
@@ -53,6 +53,7 @@ type StackOperator interface {
Dup(stack *Stack) error
Swap(stack *Stack) error
Pop(stack *Stack) error
+ Delete(stack *Stack) error
Show(stack *Stack) (string, error)
}
diff --git a/internal/rpn/operations_variables.go b/internal/rpn/operations_variables.go
index 49cab57..56bf9cd 100644
--- a/internal/rpn/operations_variables.go
+++ b/internal/rpn/operations_variables.go
@@ -65,6 +65,26 @@ func (o *Operations) DeleteVariable(name string) error {
return nil
}
+// Delete pops a variable name from the stack and deletes that variable.
+// Usage: `name d`
+func (o *Operations) Delete(stack *Stack) error {
+ val, err := popStack(stack, "d")
+ if err != nil {
+ return err
+ }
+
+ var name string
+ switch v := val.(type) {
+ case *Symbol:
+ name = v.Name()
+ case *StringNum:
+ name = v.String()
+ default:
+ return fmt.Errorf("delete expects a variable name, got %T", val)
+ }
+ return o.DeleteVariable(name)
+}
+
// ListVariables lists all variables.
// Usage: `vars`
func (o *Operations) ListVariables() (string, error) {
diff --git a/internal/rpn/operator_registry.go b/internal/rpn/operator_registry.go
index 605d1d7..80f5b05 100644
--- a/internal/rpn/operator_registry.go
+++ b/internal/rpn/operator_registry.go
@@ -68,22 +68,7 @@ func (r *OperatorRegistry) registerStackOperators(op OperatorProvider) {
r.registerStandardOperator("dup", func(stack *Stack) error { return op.Dup(stack) })
r.registerStandardOperator("swap", func(stack *Stack) error { return op.Swap(stack) })
r.registerStandardOperator("pop", func(stack *Stack) error { return op.Pop(stack) })
- r.registerStandardOperator("d", func(stack *Stack) error {
- val, err := popStack(stack, "d")
- if err != nil {
- return err
- }
- var name string
- switch v := val.(type) {
- case *Symbol:
- name = v.Name()
- case *StringNum:
- name = v.String()
- default:
- return fmt.Errorf("delete expects a variable name, got %T", val)
- }
- return op.DeleteVariable(name)
- })
+ r.registerStandardOperator("d", func(stack *Stack) error { return op.Delete(stack) })
}
// registerVariableOperators registers assignment and conversion operators.