diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-24 14:11:54 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-24 14:11:54 +0300 |
| commit | c38e792622bd392b09e6ed95a1242015b26f85a2 (patch) | |
| tree | 441da038b3e728ddb867777878cd2ef75abacbc8 /internal | |
| parent | 0710597ccd639c37f4afe6aa3c9081b7229b6066 (diff) | |
refactor(repl): extract RPN prefixes to data-driven slice (task uj)
Replace the hardcoded strings.HasPrefix check for "rpn " and "calc "
with a loop over a package-level rpnPrefixes slice. Adding a new prefix
now only requires appending to the slice, satisfying the Open/Closed
Principle.
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/repl/handlers.go | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/internal/repl/handlers.go b/internal/repl/handlers.go index 069e945..a04a76c 100644 --- a/internal/repl/handlers.go +++ b/internal/repl/handlers.go @@ -12,6 +12,10 @@ import ( "codeberg.org/snonux/gt/internal/rpn" ) +// rpnPrefixes lists the command prefixes that trigger RPN evaluation. +// Adding a new prefix (e.g., "expr") requires only appending to this slice. +var rpnPrefixes = []string{"rpn", "calc"} + // RPNCalculator defines the methods needed by REPL handlers to interact with // the RPN engine. By depending on this interface instead of the concrete *rpn.RPN, // handlers obey DIP and the Law of Demeter. @@ -174,17 +178,17 @@ func (h *RPNHandler) evalWithStackRestore(repl *REPL, input string) (string, err // input: the command string to process // Returns: (output string, handled bool, err error) func (h *RPNHandler) Handle(repl *REPL, input string) (output string, handled bool, err error) { - // Check for rpn/calc prefix + // Check for rpn/calc prefix (data-driven; see rpnPrefixes) lowerInput := strings.ToLower(input) - if strings.HasPrefix(lowerInput, "rpn ") || strings.HasPrefix(lowerInput, "calc ") { - // Extract the expression after rpn/calc - rest := strings.TrimSpace(strings.TrimPrefix(input, strings.SplitN(input, " ", 2)[0])) - - result, err := h.evalWithStackRestore(repl, rest) - if err != nil { - return "", true, err + for _, prefix := range rpnPrefixes { + if strings.HasPrefix(lowerInput, prefix+" ") { + rest := strings.TrimSpace(strings.TrimPrefix(input, strings.SplitN(input, " ", 2)[0])) + result, err := h.evalWithStackRestore(repl, rest) + if err != nil { + return "", true, err + } + return result, true, nil } - return result, true, nil } // Try RPN parsing first (for bare RPN expressions like "3 4 +") |
