artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
40
calculators/builder-solo/run-01/calc.py
Normal file
40
calculators/builder-solo/run-01/calc.py
Normal 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()
|
||||
Reference in New Issue
Block a user