From 3fb1a395ce147d364c811842ca87935caf75517a Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 20 Mar 2026 21:58:41 +0200 Subject: README.md: add REPL mode notes about independent command evaluation --- README.md | 16 ++++++++++++++++ internal/repl/repl.go | 17 +++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/README.md b/README.md index cb7d9a5..15571d9 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,22 @@ perc calc 1 2 3 show # Show stack without modifying # → 1 2 3 ``` +### REPL Mode Notes + +In REPL mode, each line is evaluated independently. To chain operations: +- Use the `rpn` subcommand for multi-step expressions: `rpn 2 3 + 4 -` +- Or use the `rpn` command to set variables: `rpn x 5 =` then `rpn x x +` +- The `show` command can display the final stack state + +Example REPL session: +``` +perc> rpn 2 3 4 + # Push 2, 3, 4; add last two +2 7 +perc> rpn show # Show full stack (but we start fresh per command) +``` + +Note: Each REPL command is evaluated independently, so you cannot chain operations like `2 3 4 +` then `-` on separate lines. Use `rpn 2 3 4 + -` for a single expression instead. + ## Building Using mage: diff --git a/internal/repl/repl.go b/internal/repl/repl.go index ed39755..0bcc4cc 100644 --- a/internal/repl/repl.go +++ b/internal/repl/repl.go @@ -18,6 +18,23 @@ import ( const historyFile = ".perc_history" +// RPNState holds the state for RPN operations in REPL +type RPNState struct { + vars *rpn.Variables +} + +var rpnState *RPNState + +// getRPNState returns or creates the RPN state +func getRPNState() *RPNState { + if rpnState == nil { + rpnState = &RPNState{ + vars: rpn.NewVariables().(*rpn.Variables), + } + } + return rpnState +} + // executor runs a calculation command and returns the result func executor(input string) { input = strings.TrimSpace(input) -- cgit v1.2.3