summaryrefslogtreecommitdiff
path: root/internal/repl/repl.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-20 21:39:32 +0200
committerPaul Buetow <paul@buetow.org>2026-03-20 21:39:32 +0200
commit92c9b9c1cf5f6b1d71d9f0178ad262068b6d5684 (patch)
tree6cd199e1b696dfc2b40a5fed0fbcdb277d5ef018 /internal/repl/repl.go
parent8701f048229e0bbb2d97ba23a9ba38397eb70286 (diff)
internal/repl: add RPN support to REPL
- Import rpn package in repl.go - Add 'rpn' and 'calc' as built-in commands - Update executor to handle 'rpn <expr>' and 'calc <expr>' commands - Add rpn/calc descriptions to help text - All tests pass, go vet passes
Diffstat (limited to 'internal/repl/repl.go')
-rw-r--r--internal/repl/repl.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/internal/repl/repl.go b/internal/repl/repl.go
index 0f690e9..79a94f4 100644
--- a/internal/repl/repl.go
+++ b/internal/repl/repl.go
@@ -10,6 +10,7 @@ import (
"syscall"
"codeberg.org/snonux/perc/internal/calculator"
+ "codeberg.org/snonux/perc/internal/rpn"
"github.com/mattn/go-isatty"
"github.com/c-bata/go-prompt"
@@ -37,6 +38,19 @@ func executor(input string) {
return
}
+ // Check for rpn command prefix
+ if strings.HasPrefix(strings.ToLower(input), "rpn ") || strings.HasPrefix(strings.ToLower(input), "calc ") {
+ // Extract the expression after rpn/calc
+ rest := strings.TrimSpace(strings.TrimPrefix(input, strings.SplitN(input, " ", 2)[0]))
+ result, err := runRPN(rest)
+ if err != nil {
+ fmt.Printf("Error: %v\n", err)
+ return
+ }
+ fmt.Println(result)
+ return
+ }
+
// Run the calculation
result, err := calculator.Parse(input)
if err != nil {
@@ -46,6 +60,13 @@ func executor(input string) {
fmt.Println(result)
}
+// runRPN parses and evaluates an RPN expression
+func runRPN(input string) (string, error) {
+ vars := rpn.NewVariables().(*rpn.Variables)
+ rpnCalc := rpn.NewRPN(vars)
+ return rpnCalc.ParseAndEvaluate(input)
+}
+
// isBuiltinCommand checks if input starts with a built-in command
func isBuiltinCommand(input string) (string, bool) {
args := strings.Fields(input)
@@ -180,6 +201,8 @@ func getCommandDescription(cmd string) string {
"clear": "Clear the screen",
"quit": "Exit the REPL",
"exit": "Exit the REPL",
+ "rpn": "Evaluate an RPN (postfix notation) expression",
+ "calc": "Same as rpn - evaluate an RPN expression",
}
return descriptions[cmd]
}