1.5 KiB
1.5 KiB
JOURNAL — Phase parse
2026-06-15 — Implementation
Built calc/parser.py using a classic recursive-descent approach with three precedence levels:
_exprhandles+/-(lowest precedence, left-assoc via loop)_termhandles*//(medium precedence, left-assoc via loop)_unaryhandles leading-(right-assoc via recursion)_primaryhandles numbers and parenthesized expressions
Left-associativity falls out naturally from the while loops in _expr and _term — each iteration wraps the accumulated node as the left child of a new BinOp, building a left-leaning tree.
Test verification output:
D1: BinOp(op='+', left=Num(value=1), right=BinOp(op='*', left=Num(value=2), right=Num(value=3)))
D2: BinOp(op='-', left=BinOp(op='-', left=Num(value=8), right=Num(value=3)), right=Num(value=2))
D3: BinOp(op='*', left=BinOp(op='+', left=Num(value=1), right=Num(value=2)), right=Num(value=3))
D4a: Unary(op='-', operand=Num(value=5))
D4b: Unary(op='-', operand=BinOp(op='+', left=Num(value=1), right=Num(value=2)))
D4c: BinOp(op='*', left=Num(value=3), right=Unary(op='-', operand=Num(value=2)))
D5 errors:
OK ParseError for '1 +': unexpected token 'EOF' (None)
OK ParseError for '(1': unclosed parenthesis, got 'EOF'
OK ParseError for '1 2': unexpected token 'NUMBER' (2)
OK ParseError for ')(': unexpected token 'RPAREN' (')')
OK ParseError for '': empty input
D6: Ran 37 tests in 0.001s OK
All 6 gates claimed and pushed. Awaiting Adversary verification.