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
@@ -0,0 +1,43 @@
"""Evaluator for the calc AST.
Result type rule: if the result is a whole number, return int; otherwise return float.
Division is always true division. Division by zero raises EvalError.
"""
from calc.parser import Num, BinOp, Unary
class EvalError(Exception):
pass
def evaluate(node) -> int | float:
if isinstance(node, Num):
return node.value
if isinstance(node, Unary):
val = evaluate(node.operand)
if node.op == '-':
return _coerce(-val)
raise EvalError(f"Unknown unary op: {node.op!r}")
if isinstance(node, BinOp):
left = evaluate(node.left)
right = evaluate(node.right)
if node.op == '+':
return _coerce(left + right)
if node.op == '-':
return _coerce(left - right)
if node.op == '*':
return _coerce(left * right)
if node.op == '/':
if right == 0:
raise EvalError("Division by zero")
return _coerce(left / right)
raise EvalError(f"Unknown binary op: {node.op!r}")
raise EvalError(f"Unknown node type: {type(node)!r}")
def _coerce(val: int | float) -> int | float:
"""Return int if val is whole-valued, otherwise float."""
if isinstance(val, float) and val.is_integer():
return int(val)
return val