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,39 @@
from calc.parser import Num, Unary, BinOp, Node
class EvalError(Exception):
pass
def evaluate(node: Node):
"""Walk the AST and return an int or float.
Result-type rule: if the result is mathematically whole-valued it is
returned as int; non-whole results are returned as float. This rule is
applied after every operation so intermediate values are also normalised.
"""
if isinstance(node, Num):
return _normalise(node.value)
if isinstance(node, Unary):
return _normalise(-evaluate(node.operand))
if isinstance(node, BinOp):
left = evaluate(node.left)
right = evaluate(node.right)
if node.op == '+':
return _normalise(left + right)
if node.op == '-':
return _normalise(left - right)
if node.op == '*':
return _normalise(left * right)
if node.op == '/':
if right == 0:
raise EvalError("division by zero")
return _normalise(left / right)
raise EvalError(f"unknown node type: {type(node).__name__}")
def _normalise(value):
"""Return int when value is whole-valued, float otherwise."""
if isinstance(value, float) and value == int(value):
return int(value)
return value