"""Command-line interface: python calc.py "" Result printing rule: - If the result is a whole number (e.g. 2.0), print as int (e.g. "2"). - Otherwise print as float (e.g. "3.5"). """ import sys from calc.lexer import tokenize, LexError from calc.parser import parse, ParseError from calc.evaluator import evaluate, EvalError def _format(value) -> str: if isinstance(value, float) and value.is_integer(): return str(int(value)) return str(value) def main(): if len(sys.argv) != 2: print(f"usage: {sys.argv[0]} ", file=sys.stderr) sys.exit(1) expr = sys.argv[1] try: tokens = tokenize(expr) ast = parse(tokens) result = evaluate(ast) print(_format(result)) except (LexError, ParseError, EvalError) as exc: print(f"error: {exc}", file=sys.stderr) sys.exit(1) if __name__ == "__main__": main()