1 stable release
| 2.0.0 | Oct 6, 2025 |
|---|
#2146 in Command line utilities
25KB
490 lines
shuntcalc ๐งฎ
A smart terminal calculator with variables, step-by-step RPN evaluation, and Shunting Yard algorithm โ all in pure Rust.

Ever wondered how (8 - 3) * 5 becomes 25 inside a calculator?
shuntcalc shows you every step โ while supporting variables, error recovery, and an interactive REPL.
Built with โค๏ธ in Rust. Zero unsafe code. Zero bloat.
โจ Features
- Variables:
x = 10; y = x * 2; x + y - Step-by-step RPN tracing: See how expressions are evaluated using a stack
- Interactive REPL: Like Pythonโs shell โ type, assign, compute
- Shunting Yard Algorithm: Converts infix โ RPN correctly with operator precedence
- Clear error messages: Know exactly where your expression went wrong
- Cross-platform: Works on Windows, macOS, and Linux
- Lightweight: No external dependencies (except
rustylinefor REPL)
๐ Install
From Crates.io (recommended)
$ cargo install shuntcalc
From source
$ git clone https://github.com/yourusername/shuntcalc
$ cd shuntcalc
$ cargo install --path .
๐ก Make sure you have Rust installed.
๐ฎ Usage
One-off calculation
$ calc "(8 - 3) * 5"
# Output: 25
Interactive REPL (like Python!)
$ calc
Then:
>>> x = 5
5
>>> (x + 2) * 3
Step 1: Push 5 โ Stack: [5]
Step 2: Push 2 โ Stack: [5, 2]
Step 3: Apply '+' โ Result: 7 โ Stack: [7]
Step 4: Push 3 โ Stack: [7, 3]
Step 5: Apply '*' โ Result: 21 โ Stack: [21]
21
>>> exit
๐ How It Works
- Tokenization: Splits input into numbers, operators, parentheses, and variables.
- Shunting Yard: Converts infix notation (e.g., 2 + 3 * 4) to Reverse Polish Notation (2 3 4 * +).
- RPN Evaluation: Uses a stack to evaluate the expression โ and explains each step.
- Error Handling: Detects unmatched parentheses, invalid tokens, division by zero, and undefined variables.
Examples
Input: (5 + 1 - 1
Shunting Yard: 5 1 + 1 - (
โ Error: Invalid token '(' at end of expression
๐ฆ Built With
- Rust ๐ฆ โ for memory safety, performance, and correctness
- Shunting Yard Algorithm โ Dijkstraโs classic parsing technique
- Reverse Polish Notation (RPN) โ stack-based evaluation
- rustyline โ for a polished REPL experience (history, arrow keys, line editing)
๐ Why This Project?
Most calculators hide the magic. shuntcalc reveals it.
Itโs not just a tool โ itโs a teaching aid for:
- Computer science students learning parsing
- Developers exploring Rustโs expressiveness
- Anyone curious about how math expressions are evaluated
And itโs built to production standards โ with error recovery, clean architecture, and user-friendly UX.
๐ License
MIT ยฉ ArshErgon
Dependencies
~8โ21MB
~272K SLoC