#!/usr/bin/env python3 """calc.py — command-line calculator. Usage: python calc.py "" Prints the result to stdout and exits 0. Prints an error message to stderr and exits 1 on bad input. """ import sys from calc.evaluator import EvalError, evaluate from calc.lexer import LexError, tokenize from calc.parser import ParseError, parse def _fmt(val) -> str: """Format a numeric result: whole-valued floats print without '.0'.""" if isinstance(val, float) and val.is_integer(): return str(int(val)) return str(val) def main() -> None: if len(sys.argv) != 2: print(f"usage: python calc.py \"\"", file=sys.stderr) sys.exit(1) expr = sys.argv[1] try: tokens = tokenize(expr) ast = parse(tokens) result = evaluate(ast) print(_fmt(result)) except (LexError, ParseError, EvalError) as exc: print(f"error: {exc}", file=sys.stderr) sys.exit(1) if __name__ == "__main__": main()