artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs

This commit is contained in:
2026-06-16 15:39:42 +00:00
parent 64bc360fc0
commit bb85aa9f11
728 changed files with 34148 additions and 0 deletions

View File

@ -0,0 +1,40 @@
#!/usr/bin/env python3
"""calc.py — command-line calculator.
Usage: python calc.py "<expression>"
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 \"<expression>\"", 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()