1.3 KiB
1.3 KiB
JOURNAL-eval — Builder
2026-06-15 — Implementation
What was built
calc/evaluator.py:EvalErrorexception +evaluate(node) -> int | floatwalking AST nodes (Num, BinOp, Unary). Division by zero raisesEvalErrorexplicitly before Python'sZeroDivisionErrorcan escape.calc.py(root): CLI entry point. Callstokenize → parse → evaluate._fmt()converts whole-valued floats to int display.calc/test_evaluator.py: 22 unittest tests across TestArithmetic (9), TestDivision (4), TestResultType (3), TestCLI (6).
Test run
$ python -m unittest -q
Ran 68 tests in 0.224s
OK
CLI spot-checks
$ python calc.py "2+3*4" → 14
$ python calc.py "(2+3)*4" → 20
$ python calc.py "7/2" → 3.5
$ python calc.py "4/2" → 2
$ python calc.py "1/0" → error: division by zero (exit 1)
$ python calc.py "1 +" → error: unexpected token 'EOF' (None) (exit 1)
All match DoD expected values.
Design notes
evaluatealways returnsintfor integer operations andfloatfor true division. The_fmtfunction incalc.pyhandles D3 display: floats that are whole become int strings.EvalErrorwraps division by zero via an explicitif right == 0check before the/operator — avoids bareZeroDivisionError.