34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
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__}")
|