shuntcalc

Crates.ioshuntcalc
lib.rsshuntcalc
version2.0.0
created_at2025-10-06 15:34:24.992679+00
updated_at2025-10-06 15:34:24.992679+00
descriptionA smart CLI calculator with variables, REPL, and step-by-step RPN evaluation using the Shunting Yard algorithm
homepage
repositoryhttps://github.com/ArshErgon/shuntcalc
max_upload_size
id1870337
size50,322
Mohd Arsh Ali (ArshErgon)

documentation

README

shuntcalc ๐Ÿงฎ

A smart terminal calculator with variables, step-by-step RPN evaluation, and Shunting Yard algorithm โ€” all in pure Rust.

Crates.io License: MIT

shuntcalc demo

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 rustyline for 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

Commit count: 0

cargo fmt