artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
33
calculators/builder-adversary/run-05/calc/evaluator.py
Normal file
33
calculators/builder-adversary/run-05/calc/evaluator.py
Normal file
@ -0,0 +1,33 @@
|
||||
from calc.parser import Num, BinOp, Unary
|
||||
|
||||
|
||||
class EvalError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def evaluate(node) -> "int | float":
|
||||
"""Walk the AST and return a numeric result.
|
||||
|
||||
Type rule: integer arithmetic stays int; division returns float, except when
|
||||
the quotient is whole-valued — then it is normalised to int so the CLI can
|
||||
print it without a trailing '.0'.
|
||||
"""
|
||||
if isinstance(node, Num):
|
||||
return node.value
|
||||
if isinstance(node, Unary):
|
||||
return -evaluate(node.operand)
|
||||
if isinstance(node, BinOp):
|
||||
left = evaluate(node.left)
|
||||
right = evaluate(node.right)
|
||||
if node.op == 'PLUS':
|
||||
return left + right
|
||||
if node.op == 'MINUS':
|
||||
return left - right
|
||||
if node.op == 'STAR':
|
||||
return left * right
|
||||
if node.op == 'SLASH':
|
||||
if right == 0:
|
||||
raise EvalError("division by zero")
|
||||
result = left / right
|
||||
return int(result) if result == int(result) else result
|
||||
raise EvalError(f"unknown node: {type(node).__name__}")
|
||||
Reference in New Issue
Block a user