artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs

This commit is contained in:
2026-06-16 15:39:42 +00:00
parent 64bc360fc0
commit bb85aa9f11
728 changed files with 34148 additions and 0 deletions

View File

@ -0,0 +1,7 @@
# BACKLOG — phase `eval`
## Build backlog
(Builder's section — read-only for Adversary)
## Adversary findings
(None yet — phase not started)

View File

@ -0,0 +1,14 @@
# BACKLOG — phase lex
## Build backlog
- [x] Create calc/__init__.py
- [ ] Create calc/lexer.py (Token, LexError, tokenize)
- [ ] Create calc/test_lexer.py (unittest covering D1-D3)
- [ ] Run tests green
- [ ] Claim D1
- [ ] Claim D2
- [ ] Claim D3
- [ ] Claim D4
## Adversary findings
<!-- Adversary writes here -->

View File

@ -0,0 +1,10 @@
# BACKLOG — phase `parse`
## Build backlog
- [x] Write calc/parser.py (AST nodes + recursive descent parser)
- [x] Write calc/test_parser.py (unittest suite covering D1-D5)
- [ ] Claim D1-D6 after tests green
## Adversary findings
(Adversary writes here)

View File

@ -0,0 +1,17 @@
# JOURNAL — phase `lex`
## Builder — Wake 1 — 2026-06-15
- Implemented calc/lexer.py: Token dataclass, LexError, tokenize().
- Implemented calc/test_lexer.py: 15 tests covering D1-D3.
- Ran `python -m unittest -q`: 15 tests, 0 failures.
- Verified:
- `python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"``[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
- `python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"` → raises `LexError: unexpected character '@' at position 2`
- Claiming D1, D2, D3, D4.
## Adversary — Wake 1 — 2026-06-15
- Initialized adversary tracking files.
- No Builder claims yet; machine-docs/ empty, only seed commit exists.
- Entering idle loop, will check every 10 min for Builder progress.

View File

@ -0,0 +1,45 @@
# JOURNAL — phase `parse`
## 2026-06-15 — Initial implementation
Plan: recursive-descent parser with two precedence levels.
- `expr` → additive level (+ -)
- `term` → multiplicative level (* /)
- `unary` → handle leading -
- `primary` → NUMBER or parenthesized expr
Grammar:
```
expr ::= term (('+' | '-') term)*
term ::= unary (('*' | '/') unary)*
unary ::= '-' unary | primary
primary ::= NUMBER | '(' expr ')'
```
Left-associativity achieved naturally by the while loop in `expr` and `term`.
## Test run output
```
Ran 36 tests in 0.001s
OK
```
## AST shape verification
```
1+2*3: BinOp(op='+', left=Num(value=1), right=BinOp(op='*', left=Num(value=2), right=Num(value=3)))
8-3-2: BinOp(op='-', left=BinOp(op='-', left=Num(value=8), right=Num(value=3)), right=Num(value=2))
8/4/2: BinOp(op='/', left=BinOp(op='/', left=Num(value=8), right=Num(value=4)), right=Num(value=2))
(1+2)*3: BinOp(op='*', left=BinOp(op='+', left=Num(value=1), right=Num(value=2)), right=Num(value=3))
-5: Unary(op='-', operand=Num(value=5))
-(1+2): Unary(op='-', operand=BinOp(op='+', left=Num(value=1), right=Num(value=2)))
3*-2: BinOp(op='*', left=Num(value=3), right=Unary(op='-', operand=Num(value=2)))
```
## Error cases
```
'1 +': ParseError: unexpected end of input
'(1': ParseError: expected RPAREN, got 'EOF' (None)
'1 2': ParseError: unexpected token 'NUMBER' (2) after expression
')(': ParseError: unexpected token 'RPAREN' (')')
'': ParseError: empty expression
```

View File

@ -0,0 +1,37 @@
# REVIEW — phase `eval`
Adversary verification log. Gates: D1 (arithmetic), D2 (division/EvalError), D3 (result type), D4 (CLI), D5 (tests green + end-to-end).
## Verdicts
### D1 — arithmetic: PASS @2026-06-15T03:53Z
Cold run of all five plan expressions:
- `2+3*4` → 14 ✓
- `(2+3)*4` → 20 ✓
- `8-3-2` → 3 ✓
- `-2+5` → 3 ✓
- `2*-3` → -6 ✓
Extra probes: `--5`→5 (double unary), `-0`→0, `100/4`→25. All correct.
### D2 — division / EvalError: PASS @2026-06-15T03:53Z
- `7/2` → 3.5 ✓
- `1/0``EvalError("division by zero")` raised (not bare `ZeroDivisionError`); CLI prints `error: division by zero` to stderr, exits 1 ✓
### D3 — result type: PASS @2026-06-15T03:53Z
- `4/2``2` (int, no trailing .0) ✓
- `7/2``3.5` (float) ✓
- Extended: `6/3`→int, `9/3`→int, `-2+5`→int; `1/3`→float, `5/4`→float. Rule consistent.
### D4 — CLI: PASS @2026-06-15T03:53Z
- `python calc.py "2+3*4"` → stdout `14`, exit 0 ✓
- `python calc.py "1 +"` → stderr `error: unexpected end of input`, exit 1, no traceback ✓
- `python calc.py "1/0"` → stderr `error: division by zero`, exit 1, no traceback ✓
- Error is to stderr only (stdout clean on error) ✓
### D5 — tests green + end-to-end: PASS @2026-06-15T03:53Z
`python -m unittest -q``Ran 47 tests in 0.001s` / `OK` — 0 failures.
Covers lex (16 tests) + parse (20 tests) + eval (11+ tests). No regression.
## Summary
All five gates PASS. No defects found. No veto.

View File

@ -0,0 +1,38 @@
# REVIEW — phase `lex` (Adversary)
## Gates
### lex/D1: PASS @2026-06-15T03:33:50Z
Cold run from Adversary clone (commit 328d25f).
- `tokenize("42")``[NUMBER(42), EOF(None)]`, value is `int`
- `tokenize("3.14")``[NUMBER(3.14), EOF(None)]`, value is `float`
- `tokenize(".5")``[NUMBER(0.5), EOF(None)]`, value is `float`
- `tokenize("10.")``[NUMBER(10.0), EOF(None)]`, value is `float`
### lex/D2: PASS @2026-06-15T03:33:50Z
Cold run from Adversary clone.
- `tokenize("1+2*3")``[NUMBER(1), PLUS('+'), NUMBER(2), STAR('*'), NUMBER(3), EOF(None)]`
- `tokenize("+-*/()") ``['PLUS', 'MINUS', 'STAR', 'SLASH', 'LPAREN', 'RPAREN', 'EOF']`
### lex/D3: PASS @2026-06-15T03:33:50Z
Cold run from Adversary clone.
- `tokenize(" 12 + 3 ")``[('NUMBER', 12), ('PLUS', '+'), ('NUMBER', 3), ('EOF', None)]`
- `tokenize("1 @ 2")` raises `LexError: unexpected character '@' at position 2` ✓ (char and position in message)
- `tokenize("$5")` raises `LexError: unexpected character '$' at position 0`
- `tokenize("x")` raises `LexError`
### lex/D4: PASS @2026-06-15T03:33:50Z
Cold run from Adversary clone.
- `python -m unittest -q` → Ran 15 tests, OK (0 failures) ✓
- `tokenize('3.5*(1-2)')``[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
- `tokenize('1 @ 2')` → raises `LexError`
## Adversary findings
### AF-1 — bare dot raises `ValueError` not `LexError` [non-blocking]
`tokenize(".")` raises `ValueError: could not convert string to float: '.'` (from `float(".")` inside
the number branch) instead of `LexError`. Not a DoD FAIL — none of D1D3 test a lone `.` — but
the module leaks an internal Python exception for this invalid input. Recommend the Builder guard
with `try/except ValueError` in the number branch and re-raise as `LexError`.
Status: open (non-blocking — Builder may fix in a later phase or now at discretion)

View File

@ -0,0 +1,50 @@
# REVIEW — parse phase (Adversary)
## Status
All gates verified. Awaiting Builder to write ## DONE to STATUS-parse.md.
## Gate verdicts
**D1 (precedence): PASS** @2026-06-15T03:40Z
- `parse(tokenize('1+2*3'))``BinOp(op='+', left=Num(value=1), right=BinOp(op='*', left=Num(value=2), right=Num(value=3)))`
- `parse(tokenize('2*3+1'))``BinOp(op='+', left=BinOp(op='*', ...), right=Num(1))`
- `parse(tokenize('2+3*4*5'))``+` at root, nested `*` tree under right ✓
**D2 (left assoc): PASS** @2026-06-15T03:40Z
- `8-3-2``BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))`
- `8/4/2``BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))`
- `12/6*2``BinOp('*', BinOp('/', Num(12), Num(6)), Num(2))` ✓ (left-assoc across same-level ops)
- `1-2+3``BinOp('+', BinOp('-', Num(1), Num(2)), Num(3))`
**D3 (parentheses): PASS** @2026-06-15T03:40Z
- `(1+2)*3``BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))`
- `((3))``Num(3)`
**D4 (unary minus): PASS** @2026-06-15T03:40Z
- `-5``Unary('-', Num(5))`
- `-(1+2)``Unary('-', BinOp('+', Num(1), Num(2)))`
- `3 * -2``BinOp('*', Num(3), Unary('-', Num(2)))`
- `--5``Unary('-', Unary('-', Num(5)))` ✓ (right-recursive _unary)
- `-(-5)``Unary('-', Unary('-', Num(5)))`
**D5 (errors): PASS** @2026-06-15T03:40Z
All 5 plan-mandated cases raise `ParseError` (not a Python built-in):
- `'1 +'` → ParseError ✓
- `'(1'` → ParseError ✓
- `'1 2'` → ParseError ✓
- `')('` → ParseError ✓
- `''` → ParseError ✓
Additional probes also raise `ParseError`: `*5`, `()`, `1+2)`, `+-5`, `+` alone.
**D6 (tests green): PASS** @2026-06-15T03:40Z
- `python -m unittest -q``Ran 36 tests in 0.001s` / `OK`
- Tests assert on tree structure via dataclass equality (not evaluation) ✓
- 7 error-case tests, 20 structural tests across D1D4
## Adversary findings
None. No defects found.
## Implementation notes (post-verdict)
Recursive-descent parser is structurally sound: `_expr → _term → _unary → _primary`.
`ParseError(Exception)` is a proper subclass. EOF sentinel token prevents index OOB.

View File

@ -0,0 +1,49 @@
# STATUS — phase `eval`
## DONE
## Gates: D1D5 — all PASS (Adversary-verified 2026-06-15T03:53Z)
Commit: `6d4fbb6f1c15402148e5e06e2f99e2b1154f4dd6`
---
## Verify commands + expected outputs
Run from repo root (clean clone):
```bash
# D5 — whole suite (lex + parse + eval), must be 0 failures
python -m unittest -q
# Expected: Ran 47 tests in <t>s / OK
# D1 — arithmetic
python calc.py "2+3*4" # → 14
python calc.py "(2+3)*4" # → 20
python calc.py "8-3-2" # → 3
python calc.py "-2+5" # → 3
python calc.py "2*-3" # → -6
# D2 — true division + EvalError
python calc.py "7/2" # → 3.5
python calc.py "1/0" # → stderr: "error: division by zero", exit 1
# D3 — result type
python calc.py "4/2" # → 2 (int, no trailing .0)
python calc.py "7/2" # → 3.5 (float)
# D4 — CLI error handling
python calc.py "1 +" # → stderr: error message, exit 1
```
---
## Files
- `calc/evaluator.py``evaluate(node)` walking Num/BinOp/Unary, `EvalError`
- `calc.py` — CLI: tokenize→parse→evaluate, errors to stderr, exit 1
- `calc/test_evaluator.py` — 13 unittest tests covering D1D3
## Result-type rule (D3)
`evaluate` returns `int` when the float result equals its integer value (`result == int(result)`), `float` otherwise. This is applied inside `evaluate` for BinOp division results.

View File

@ -0,0 +1,46 @@
# STATUS — phase lex
## DONE
All gates D1D4 verified PASS by Adversary @2026-06-15T03:33:50Z.
AF-1 fixed: bare dot now raises LexError instead of leaking ValueError.
## Current state
Gate: D1 D2 D3 D4 — all PASS (Adversary verified)
## Gates
### D1 — numbers
**WHAT:** Integers and floats tokenize to NUMBER tokens with correct Python numeric values.
**HOW:** `python -m unittest -q calc.test_lexer.TestNumbers` (or full suite)
**EXPECTED:** 4 tests pass; `tokenize("42")``[NUMBER(42), EOF]`; `tokenize("3.14")``[NUMBER(3.14), EOF]`; `tokenize(".5")``[NUMBER(0.5), EOF]`; `tokenize("10.")``[NUMBER(10.0), EOF]`
**WHERE:** calc/lexer.py, calc/test_lexer.py
### D2 — operators & parens
**WHAT:** `+ - * / ( )` each tokenize to the correct kind.
**HOW:** `python -m unittest -q calc.test_lexer.TestOperatorsAndParens`
**EXPECTED:** 4 tests pass; `tokenize("1+2*3")``[NUMBER(1), PLUS('+'), NUMBER(2), STAR('*'), NUMBER(3), EOF(None)]`
**WHERE:** calc/lexer.py, calc/test_lexer.py
### D3 — whitespace & errors
**WHAT:** Spaces/tabs skipped; invalid chars raise LexError with the char and position.
**HOW:** `python -m unittest -q calc.test_lexer.TestWhitespaceAndErrors`
**EXPECTED:** 7 tests pass; `tokenize(" 12 + 3 ")``[NUMBER(12), PLUS, NUMBER(3), EOF]`; `tokenize("1 @ 2")` raises `LexError`
**WHERE:** calc/lexer.py, calc/test_lexer.py
### D4 — tests green
**WHAT:** Full test suite 0 failures.
**HOW:**
```bash
python -m unittest -q
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
```
**EXPECTED:**
- `python -m unittest -q` → 15 tests, 0 failures
- Second command → `[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
- Third command → raises `LexError: unexpected character '@' at position 2`
**WHERE:** calc/test_lexer.py (commit sha: see below)
## Commit SHA
328d25f — claim(D1,D2,D3,D4): lexer implementation + tests green

View File

@ -0,0 +1,105 @@
# STATUS — phase `parse`
## DONE
## Gates
- D1 (precedence): PASS @2026-06-15T03:40Z
- D2 (left assoc): PASS @2026-06-15T03:40Z
- D3 (parentheses): PASS @2026-06-15T03:40Z
- D4 (unary minus): PASS @2026-06-15T03:40Z
- D5 (errors): PASS @2026-06-15T03:40Z
- D6 (tests green): PASS @2026-06-15T03:40Z
## Source files
- `calc/parser.py` — AST node definitions + recursive-descent parser
- `calc/test_parser.py` — 20 unittest cases covering D1D5
## AST shape
Nodes are dataclasses defined in `calc/parser.py`:
```python
@dataclass
class Num:
value: Union[int, float]
@dataclass
class BinOp:
op: str # '+', '-', '*', '/'
left: Node
right: Node
@dataclass
class Unary:
op: str # '-'
operand: Node
```
## Verify commands (cold)
```bash
python -m unittest -q
```
Expected: `Ran 36 tests in Xs` / `OK` (0 failures)
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('1+2*3')))"
```
Expected: `BinOp(op='+', left=Num(value=1), right=BinOp(op='*', left=Num(value=2), right=Num(value=3)))`
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8-3-2')))"
```
Expected: `BinOp(op='-', left=BinOp(op='-', left=Num(value=8), right=Num(value=3)), right=Num(value=2))`
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8/4/2')))"
```
Expected: `BinOp(op='/', left=BinOp(op='/', left=Num(value=8), right=Num(value=4)), right=Num(value=2))`
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('(1+2)*3')))"
```
Expected: `BinOp(op='*', left=BinOp(op='+', left=Num(value=1), right=Num(value=2)), right=Num(value=3))`
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5')))"
```
Expected: `Unary(op='-', operand=Num(value=5))`
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-(1+2)')))"
```
Expected: `Unary(op='-', operand=BinOp(op='+', left=Num(value=1), right=Num(value=2)))`
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('3 * -2')))"
```
Expected: `BinOp(op='*', left=Num(value=3), right=Unary(op='-', operand=Num(value=2)))`
```bash
python -c "
from calc.lexer import tokenize; from calc.parser import parse, ParseError
for expr in ['1 +', '(1', '1 2', ')(', '']:
try:
parse(tokenize(expr))
print(f'{expr!r}: NO ERROR (FAIL)')
except ParseError as e:
print(f'{expr!r}: ParseError OK')
"
```
Expected: all 5 lines print `ParseError OK`
## Gate-specific DoD mapping
**D1 — precedence:** `1+2*3` parses as `1+(2*3)``*` is under the right child of `+`, not the other way.
**D2 — left assoc:** `8-3-2` → left-leaning tree; `8/4/2` → left-leaning tree. The while-loop in `_expr`/`_term` naturally accumulates left.
**D3 — parens:** `(1+2)*3``BinOp('*', BinOp('+', ...), Num(3))``+` is under `*`'s left child.
**D4 — unary:** `-5``Unary`, `-(1+2)``Unary(BinOp(...))`, `3*-2``BinOp('*', Num(3), Unary(...))`.
**D5 — errors:** `"1 +"`, `"(1"`, `"1 2"`, `")("`, `""` all raise `ParseError` (not Python built-ins).
**D6 — tests:** `python -m unittest -q` → 36 tests, 0 failures.