From f02d0996bcaee49a4226ba678788e7550b06be29 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 24 May 2026 14:08:41 +0300 Subject: 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. --- internal/rpn/operations_interfaces.go | 1 + internal/rpn/operations_variables.go | 20 ++++++++++++++++++++ internal/rpn/operator_registry.go | 17 +---------------- 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. -- cgit v1.2.3