1000 B
1000 B
JOURNAL — phase: eval (Builder)
2026-06-15 — implementation
Design
AST nodes from parse phase: Num(value), BinOp(op, left, right), Unary(op, operand).
Evaluator is a recursive tree walk. Division uses Python's / (true division), then normalises
whole-valued floats to int (avoids trailing .0 in CLI output without needing special fmt logic).
Runs
$ python -m unittest -q
----------------------------------------------------------------------
Ran 56 tests in 0.002s
OK
$ 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 (stderr, exit 1)
$ python calc.py "1 +"
error: unexpected token 'EOF' (stderr, exit 1)
Type normalisation choice
int(result) if result == int(result) else result — chose this over a fmt conversion in the CLI
so evaluate itself is honest about the value type and tests can assert isinstance(result, int).