1.6 KiB
1.6 KiB
JOURNAL — eval phase
Implementation run (2026-06-16)
Pre-state
- 44 tests passing (lex + parse phases)
calc/lexer.py,calc/parser.pyalready in place- AST nodes:
Num(value),BinOp(op, left, right),Unary(op, operand)
calc/evaluator.py
Wrote tree-walking evaluate(node):
Num→ returnnode.valueUnary→-evaluate(operand)(only-is supported)BinOp→ recurse left/right, apply op;/case: raiseEvalError("division by zero")whenright == 0, otherwiseleft / right- Whole-float normalisation:
if isinstance(result, float) and result.is_integer(): return int(result)
calc/test_evaluator.py
12 tests across 4 classes:
TestArithmetic— the 5 DoD examplesTestDivision— true division, div-by-zero asEvalError, no bareZeroDivisionErrorTestResultType—4/2isint,7/2isfloat, plain int arithmetic staysint, negative whole isint
calc.py (CLI)
At repo root; uses sys.argv[1], calls lex→parse→eval pipeline. Catches LexError | ParseError | EvalError, prints error: {e} to stderr, exits 1. Clean exit 0 on success.
Test run
$ python -m unittest -q
Ran 56 tests in 0.001s
OK
CLI smoke test
$ 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' (None) (exit 1)
All DoD items satisfied. Writing ## DONE to STATUS-eval.md.