diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-23 22:29:18 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-23 22:29:18 +0200 |
| commit | 0998f4f76fb670c741eb0951630f4f5d18dcc1e5 (patch) | |
| tree | d4499fce20a45df53bea435c8f6181d40302cc31 | |
| parent | c1de7b44437997e4695b5554a96ad253cf44584a (diff) | |
Refactor Operator interface to reduce bloat and fix errcheck issue
| -rw-r--r-- | internal/rpn/operations.go | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/internal/rpn/operations.go b/internal/rpn/operations.go index cab77db..4bd1e4b 100644 --- a/internal/rpn/operations.go +++ b/internal/rpn/operations.go @@ -5,36 +5,49 @@ import ( "math" ) -// Operator defines the interface for operator implementations and stack manipulation. -// This allows RPN to depend on an abstraction instead of the concrete Operations type. -type Operator interface { - // Arithmetic operators +// ArithmeticOperator defines the interface for basic arithmetic operators. +type ArithmeticOperator interface { Add(stack *Stack) error Subtract(stack *Stack) error Multiply(stack *Stack) error Divide(stack *Stack) error Power(stack *Stack) error Modulo(stack *Stack) error +} - // Hyper operators +// HyperOperator defines the interface for hyper operators. +type HyperOperator interface { HyperAdd(stack *Stack) error HyperSubtract(stack *Stack) error HyperMultiply(stack *Stack) error HyperDivide(stack *Stack) error HyperPower(stack *Stack) error HyperModulo(stack *Stack) error +} - // Stack manipulation operators +// StackOperator defines the interface for stack manipulation operators. +type StackOperator interface { Dup(stack *Stack) error Swap(stack *Stack) error Pop(stack *Stack) error Show(stack *Stack) (string, error) +} - // Variable operations +// VariableOperator defines the interface for variable operations. +type VariableOperator interface { ListVariables() (string, error) ClearVariables() } +// Operator is the combined interface for all operator implementations. +// This allows RPN to depend on an abstraction instead of the concrete Operations type. +type Operator interface { + ArithmeticOperator + HyperOperator + StackOperator + VariableOperator +} + // Operations provides operator implementations and stack manipulation. type Operations struct { vars VariableStore @@ -365,8 +378,7 @@ func (o *Operations) Swap(stack *Stack) error { // Pop removes and discards the top stack value. func (o *Operations) Pop(stack *Stack) error { - _, err := stack.Pop() - if err != nil { + if _, err := stack.Pop(); err != nil { return fmt.Errorf("insufficient operands for pop: %w", err) } return nil |
