Skip to content

PrincetonAfeez/Calculator_v2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CLI Calculator

A hand-built Python command-line calculator with its own lexer, parser, AST, and evaluator. It does not use eval().

Quick Start

python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
python -m calc "2 + 2 * 3"

Run interactive mode:

python -m calc

Run tests:

pytest

CLI Usage

python -m calc "2 + 2"
python -m calc --precision 50 "1 / 7"
python -m calc --angle deg "sin(90)"
python -m calc --format scientific "123456789"
python -m calc -f script.calc
"sqrt(81) + max(2, 7)" | python -m calc

Supported flags:

  • -f, --file: evaluate expressions from a script file (one expression per line)
  • --precision N: Decimal precision (1 to 200)
  • --angle {rad,deg}: trigonometry angle mode
  • --format {auto,fixed,scientific}: output formatting mode
  • --version: print version
  • --help: show CLI help

Full Feature List

1) Arithmetic and Operators

  • Addition +, subtraction -, multiplication *, division /
  • Integer division //, modulo %, exponentiation ^ and **
  • Unary plus/minus (+x, -x, -(2+3))
  • Parentheses with precedence handling (PEMDAS)
  • Decimal-based numeric evaluation using decimal.Decimal

2) Number Literal Support

  • Integer literals: 42
  • Decimal literals: 3.1415
  • Scientific notation: 2.5e-3
  • Hex literals: 0xFF
  • Binary literals: 0b1010
  • Octal literals: 0o77

3) Variables, Constants, and Result Memory

  • Variable assignment: x = 5
  • Variable usage: x * 3
  • Auto-updated answer variable: ans
  • Built-in constants:
    • pi
    • e
    • tau
    • inf

4) User-Defined Functions

  • Define functions: f(x) = x^2 + 1
  • Call functions: f(10)
  • Multi-parameter functions: g(a, b) = a + b
  • Scoped argument binding per call
  • Validation for duplicate parameters and incorrect argument counts

5) Built-In Functions

  • Core helpers: abs, round, floor, ceil
  • Power/root: pow, sqrt
  • Aggregates: min, max
  • Integer math: factorial, gcd
  • Logarithms: log, log10, log2
  • Trigonometry: sin, cos, tan

Notes:

  • Log functions enforce valid domains (x > 0, base constraints).
  • Trigonometric functions support radians and degrees.
  • Precision-sensitive paths use Decimal-based computation.

6) REPL Commands

Start REPL with:

python -m calc

Commands:

  • help: show command help and examples
  • history: show command/result history
  • clear history or history clear: clear history
  • vars: list current variables (including ans)
  • clear: clear user-defined variables and functions
  • set precision N: update precision at runtime
  • set angle deg|rad: change angle mode
  • set format auto|fixed|scientific: change output mode
  • quit or exit: leave REPL

Input handling:

  • Empty lines are ignored
  • Ctrl+C cancels current input without exiting
  • Ctrl+D exits cleanly

7) Script and Pipe Modes

  • Script mode: python -m calc -f script.calc
  • Pipe mode: standard input is read line-by-line when stdin is not a TTY
  • Script/piped lines that are empty or start with # are skipped
  • Script errors are reported with line numbers and processing continues

8) Formatting and Precision Controls

  • Output format modes:
    • auto
    • fixed
    • scientific
  • Runtime and startup precision control
  • Session precision applies to Decimal evaluation and display behavior

9) Config and History Persistence

  • Config file: ~/.calcrc (JSON)
  • Supported config keys:
    • precision
    • angle_mode
    • output_format
    • save_history
  • History file: ~/.calc_history
  • History can be loaded/saved across sessions (up to recent entries)

10) Error Handling

Custom error hierarchy with human-readable messages:

  • CalcError
  • LexerError
  • ParseError
  • EvaluationError
  • DivisionByZeroError
  • UndefinedVariableError

Behavior:

  • Position-aware error messages
  • Caret-style rendering for source errors
  • Graceful REPL recovery after invalid input

Project Structure

  • calc/lexer.py: tokenizes source input
  • calc/parser.py: recursive-descent parser producing AST
  • calc/ast_nodes.py: AST node model
  • calc/evaluator.py: tree-walking evaluator
  • calc/environment.py: variables/constants/functions state
  • calc/formatter.py: output formatting strategies
  • calc/engine.py: session orchestration
  • calc/repl.py: interactive loop and commands
  • calc/cli.py: command-line entrypoint and execution modes
  • calc/config.py: config load/validate
  • calc/history.py: session and persisted history
  • tests/: unit and integration tests

Development

pip install -r requirements.txt
pytest
python -m calc

About

A command-line calculator with its own lexer, parser, AST, and evaluator

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages