42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
"""Evaluator for the arithmetic AST produced by calc.parser.
|
|
|
|
evaluate(node) -> int | float
|
|
|
|
Result-type rule: if the result is a whole number (no fractional part),
|
|
return an int; otherwise return a float. This means 4/2 → 2 (int) and
|
|
7/2 → 3.5 (float).
|
|
"""
|
|
|
|
from calc.parser import Num, BinOp, Unary, Node
|
|
|
|
|
|
class EvalError(Exception):
|
|
pass
|
|
|
|
|
|
def evaluate(node: Node) -> "int | float":
|
|
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)
|
|
op = node.op
|
|
if op == '+':
|
|
result = left + right
|
|
elif op == '-':
|
|
result = left - right
|
|
elif op == '*':
|
|
result = left * right
|
|
elif op == '/':
|
|
if right == 0:
|
|
raise EvalError("division by zero")
|
|
result = left / right
|
|
else:
|
|
raise EvalError(f"unknown operator: {op!r}")
|
|
if isinstance(result, float) and result.is_integer():
|
|
return int(result)
|
|
return result
|
|
raise EvalError(f"unknown node type: {type(node).__name__}")
|