934 B
934 B
JOURNAL — eval phase
Session 1
Implemented evaluator, CLI, and tests in one pass.
evaluator.py design
evaluate(node) recursively walks Num, BinOp, Unary nodes. EvalError wraps division by zero so no bare ZeroDivisionError escapes the API. _normalize(value) converts whole-valued floats to int (satisfying D3).
Test run
$ python -m unittest -q
Ran 69 tests in 0.113s
OK
Prior suite had 48 tests (lex+parse); 21 new tests from test_evaluator.py.
CLI manual check
$ 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 "8-3-2" → 3
$ python calc.py "-2+5" → 3
$ python calc.py "2*-3" → -6
$ python calc.py "1/0" → error: division by zero (exit 1)
$ python calc.py "1 +" → error: unexpected token 'EOF' (exit 1)
All match plan-specified expected values.