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,37 @@
from calc.parser import Num, BinOp, Unary
class EvalError(Exception):
pass
def evaluate(node):
"""Walk the AST and return int | float.
Whole-valued results (including whole-valued division) are returned as int;
non-whole float results are returned as float.
"""
if isinstance(node, Num):
return node.value
if isinstance(node, Unary):
if node.op == '-':
return -evaluate(node.operand)
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 left + right
if node.op == '-':
return left - right
if node.op == '*':
return left * right
if node.op == '/':
if right == 0:
raise EvalError("division by zero")
result = left / right
if result == int(result):
return int(result)
return result
raise EvalError(f"unknown binary op {node.op!r}")
raise EvalError(f"unknown node type {type(node).__name__!r}")