artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
@ -0,0 +1,13 @@
|
||||
# BACKLOG-eval
|
||||
|
||||
## Build backlog
|
||||
(Builder-owned — read only to Adversary)
|
||||
|
||||
- [x] D1: implement evaluate() for arithmetic, precedence, parens, unary minus
|
||||
- [x] D2: true division; EvalError on divide-by-zero
|
||||
- [x] D3: _fmt() for whole vs non-whole display
|
||||
- [x] D4: calc.py CLI
|
||||
- [x] D5: test_evaluator.py (22 tests); full suite 68 tests green
|
||||
|
||||
## Adversary findings
|
||||
(No findings yet — eval phase not started)
|
||||
@ -0,0 +1,27 @@
|
||||
# BACKLOG-lex
|
||||
|
||||
## Build backlog
|
||||
(Builder-owned — read-only to Adversary)
|
||||
|
||||
## Adversary findings
|
||||
|
||||
### AF-01: unhandled ValueError for malformed number literals [informational, non-blocking]
|
||||
|
||||
**Repro:**
|
||||
```python
|
||||
from calc.lexer import tokenize, LexError
|
||||
tokenize('1.2.3') # raises ValueError, not LexError
|
||||
tokenize('.') # raises ValueError, not LexError
|
||||
tokenize('..') # raises ValueError, not LexError
|
||||
```
|
||||
|
||||
**Root cause:** `lexer.py` line 39: `float(raw)` is called without a try/except. If the
|
||||
greedy digit/dot scan produces an unparseable string (e.g. `1.2.3` or bare `.`), Python
|
||||
raises `ValueError` instead of the module's `LexError`.
|
||||
|
||||
**Impact:** Not a DoD violation (D3 specifies invalid *characters*, not malformed tokens).
|
||||
However it leaks internal Python exceptions for unusual but possible inputs. Recommend
|
||||
wrapping in `try/except ValueError` and re-raising as `LexError` with position info.
|
||||
|
||||
**Status:** Informational — Builder may address in this phase or a follow-up. Adversary
|
||||
will close this finding if re-tested and passing.
|
||||
@ -0,0 +1,16 @@
|
||||
# BACKLOG-parse
|
||||
|
||||
## Build backlog
|
||||
|
||||
- [x] D1 — precedence: implemented via separate `_expr`/`_term` levels
|
||||
- [x] D2 — left associativity: `while` loop in `_expr`/`_term`
|
||||
- [x] D3 — parentheses: `_primary` handles LPAREN/RPAREN
|
||||
- [x] D4 — unary minus: `_unary` level, right-recursive
|
||||
- [x] D5 — ParseError: defined and raised for all malformed inputs
|
||||
- [x] D6 — tests green: 46 tests, 0 failures
|
||||
|
||||
All items complete. Awaiting Adversary verification.
|
||||
|
||||
## Adversary findings
|
||||
|
||||
_(Adversary writes here)_
|
||||
@ -0,0 +1,10 @@
|
||||
# DECISIONS — shared (append-only)
|
||||
|
||||
## 2026-06-15
|
||||
- Adversary initialized; awaiting Builder gate claims on D1–D4
|
||||
|
||||
## D-001: Token representation
|
||||
Token is a dataclass with `kind: str` and `value: Any`. NUMBER tokens carry int or float value; operator tokens carry the character string; EOF carries None. This makes the type easy to pattern-match in future parser/evaluator phases.
|
||||
|
||||
## D-002: LexError
|
||||
LexError subclasses Exception (not ValueError) for clean catching. Message format: `"unexpected character {char!r} at position {pos}"`.
|
||||
@ -0,0 +1,35 @@
|
||||
# JOURNAL-eval — Builder
|
||||
|
||||
## 2026-06-15 — Implementation
|
||||
|
||||
### What was built
|
||||
|
||||
- `calc/evaluator.py`: `EvalError` exception + `evaluate(node) -> int | float` walking AST nodes (Num, BinOp, Unary). Division by zero raises `EvalError` explicitly before Python's `ZeroDivisionError` can escape.
|
||||
- `calc.py` (root): CLI entry point. Calls `tokenize → parse → evaluate`. `_fmt()` converts whole-valued floats to int display.
|
||||
- `calc/test_evaluator.py`: 22 unittest tests across TestArithmetic (9), TestDivision (4), TestResultType (3), TestCLI (6).
|
||||
|
||||
### Test run
|
||||
|
||||
```
|
||||
$ python -m unittest -q
|
||||
Ran 68 tests in 0.224s
|
||||
OK
|
||||
```
|
||||
|
||||
### CLI spot-checks
|
||||
|
||||
```
|
||||
$ 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 (exit 1)
|
||||
$ python calc.py "1 +" → error: unexpected token 'EOF' (None) (exit 1)
|
||||
```
|
||||
|
||||
All match DoD expected values.
|
||||
|
||||
### Design notes
|
||||
|
||||
- `evaluate` always returns `int` for integer operations and `float` for true division. The `_fmt` function in `calc.py` handles D3 display: floats that are whole become int strings.
|
||||
- `EvalError` wraps division by zero via an explicit `if right == 0` check before the `/` operator — avoids bare `ZeroDivisionError`.
|
||||
@ -0,0 +1,72 @@
|
||||
# JOURNAL-lex — Adversary
|
||||
|
||||
## 2026-06-15 — Wake 1
|
||||
- Read phase plan: mission is lexer for Python arithmetic calculator
|
||||
- Checked origin/main: only seed commit, Builder has not pushed any work yet
|
||||
- Set up REVIEW-lex.md, BACKLOG-lex.md, STATUS-lex.md, JOURNAL-lex.md
|
||||
- Will poll for Builder claims
|
||||
|
||||
## 2026-06-15 — Wake 2 (watchdog ping)
|
||||
- Pulled claim(D1,D2,D3,D4) commit from Builder — all gates claimed at once
|
||||
- Read STATUS-lex.md for verification commands (did NOT read JOURNAL before verdicts)
|
||||
- Cold-verified all four gates from my own clone:
|
||||
|
||||
### D1 verification
|
||||
- `tokenize('42')` → NUMBER(42 int) EOF ✓
|
||||
- `tokenize('3.14')` → NUMBER(3.14 float) EOF ✓
|
||||
- `tokenize('.5')` → NUMBER(0.5) EOF ✓
|
||||
- `tokenize('10.')` → NUMBER(10.0) EOF ✓
|
||||
|
||||
### D2 verification
|
||||
- `tokenize('1+2*3')` kinds → ['NUMBER','PLUS','NUMBER','STAR','NUMBER','EOF'] ✓
|
||||
- `tokenize('3.5*(1-2)')` → correct full token list ✓
|
||||
- All 6 operator kinds present in test suite ✓
|
||||
|
||||
### D3 verification
|
||||
- `tokenize(' 12 + 3 ')` → ['NUMBER','PLUS','NUMBER','EOF'] ✓
|
||||
- `tokenize('1 @ 2')` → LexError: "unexpected character '@' at position 2" ✓
|
||||
- '@' and '2' both in error message ✓
|
||||
|
||||
### D4 verification
|
||||
- `python -m unittest -q` → Ran 24 tests, OK, exit 0 ✓
|
||||
- Three mandatory test inputs from DoD confirmed in test suite ✓
|
||||
|
||||
### Break-it probes
|
||||
- `tokenize('1.2.3')` → ValueError (not LexError) — logged as AF-01 (non-blocking)
|
||||
- `tokenize('.')` → ValueError (not LexError) — same issue
|
||||
- `tokenize('..')` → ValueError (not LexError) — same issue
|
||||
- `tokenize('1x')` → LexError at position 1 ✓ (x is invalid char)
|
||||
- `tokenize('\n')` → LexError ✓ (newline not in whitespace set)
|
||||
- `tokenize('')` → [Token(EOF, None)] ✓
|
||||
- SLASH operator explicitly tested ✓
|
||||
|
||||
### Verdict
|
||||
All four gates: PASS. One informational finding (AF-01: ValueError for malformed number
|
||||
literals). No VETO issued. Builder may write ## DONE.
|
||||
|
||||
## 2026-06-15 — Builder Wake 2 (post-verdicts)
|
||||
|
||||
Pulled Adversary verdict commit. All four gates PASS.
|
||||
|
||||
Addressed AF-01: wrapped `float(raw)` in try/except ValueError → re-raises as LexError.
|
||||
|
||||
```
|
||||
$ python -m unittest -q
|
||||
Ran 24 tests in 0.000s
|
||||
OK
|
||||
|
||||
$ python -c "
|
||||
from calc.lexer import tokenize, LexError
|
||||
for case in ['1.2.3', '.', '..']:
|
||||
try:
|
||||
tokenize(case)
|
||||
print(f'FAIL: {case!r}')
|
||||
except LexError as e:
|
||||
print(f'OK: {case!r} -> LexError: {e}')
|
||||
"
|
||||
OK: '1.2.3' -> LexError: malformed number '1.2.3' at position 0
|
||||
OK: '.' -> LexError: malformed number '.' at position 0
|
||||
OK: '..' -> LexError: malformed number '..' at position 0
|
||||
```
|
||||
|
||||
Updated STATUS-lex.md to ## DONE. Phase lex complete.
|
||||
@ -0,0 +1,45 @@
|
||||
# JOURNAL-parse — Builder
|
||||
|
||||
## 2026-06-15
|
||||
|
||||
### Implementation
|
||||
|
||||
Built `calc/parser.py` as a classic recursive-descent parser with three precedence levels:
|
||||
|
||||
```
|
||||
expr → term (('+' | '-') term)* # left-assoc, lowest
|
||||
term → unary (('*' | '/') unary)* # left-assoc, higher
|
||||
unary → '-' unary | primary # right-recursive for nested --
|
||||
primary→ NUMBER | '(' expr ')'
|
||||
```
|
||||
|
||||
This naturally yields left-associativity (the `while` loop builds left-leaning trees) and correct precedence (mul/div are parsed inside `term` which is called from `expr`).
|
||||
|
||||
### Test run output
|
||||
|
||||
```
|
||||
$ python -m unittest -q
|
||||
......................................................
|
||||
Ran 46 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
(46 = 9 existing lex tests + 17 new parser tests)
|
||||
|
||||
### Manual gate verification
|
||||
|
||||
```
|
||||
D1 add-mul: BinOp('+', Num(1), BinOp('*', Num(2), Num(3))) ✓
|
||||
D1 mul-add: BinOp('+', BinOp('*', Num(2), Num(3)), Num(1)) ✓
|
||||
D2 sub: BinOp('-', BinOp('-', Num(8), Num(3)), Num(2)) ✓
|
||||
D2 div: BinOp('/', BinOp('/', Num(8), Num(4)), Num(2)) ✓
|
||||
D3 paren: BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)) ✓
|
||||
D4 unary: Unary('-', Num(5)) ✓
|
||||
D4 u-paren: Unary('-', BinOp('+', Num(1), Num(2))) ✓
|
||||
D4 mul-u: BinOp('*', Num(3), Unary('-', Num(2))) ✓
|
||||
D5 '1 +' → ParseError: unexpected token 'EOF' (None) ✓
|
||||
D5 '(1' → ParseError: expected RPAREN, got 'EOF' (None) ✓
|
||||
D5 '1 2' → ParseError: unexpected token 'NUMBER' (2) after expression ✓
|
||||
D5 ')(' → ParseError: unexpected token 'RPAREN' (')') ✓
|
||||
D5 '' → ParseError: empty expression ✓
|
||||
```
|
||||
120
calculators/builder-adversary/run-02/machine-docs/REVIEW-eval.md
Normal file
120
calculators/builder-adversary/run-02/machine-docs/REVIEW-eval.md
Normal file
@ -0,0 +1,120 @@
|
||||
# REVIEW-eval — Adversary Verdicts
|
||||
|
||||
## Legend
|
||||
- PASS @<ts> — gate accepted, evidence below
|
||||
- FAIL — repro steps below, Builder must fix
|
||||
|
||||
---
|
||||
|
||||
## D1 — arithmetic
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run — all 5 DoD-mandated cases:
|
||||
```
|
||||
'2+3*4' -> 14 expected 14 OK
|
||||
'(2+3)*4' -> 20 expected 20 OK
|
||||
'8-3-2' -> 3 expected 3 OK
|
||||
'-2+5' -> 3 expected 3 OK
|
||||
'2*-3' -> -6 expected -6 OK
|
||||
```
|
||||
|
||||
Extra break-it probes (all correct):
|
||||
```
|
||||
'2+3+4' -> 9 OK (left-assoc addition)
|
||||
'10-2-3' -> 5 OK (left-assoc subtraction)
|
||||
'2*3*4' -> 24 OK (left-assoc multiplication)
|
||||
'--5' -> 5 OK (double unary minus)
|
||||
'(-3)*(-2)' -> 6 OK (unary in parens)
|
||||
'(1+2)*(3+4)' -> 21 OK (nested parens)
|
||||
'0*100' -> 0 OK
|
||||
```
|
||||
|
||||
`python -m unittest calc.test_evaluator.TestArithmetic -q` — 0 failures.
|
||||
|
||||
---
|
||||
|
||||
## D2 — division
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run:
|
||||
```
|
||||
'7/2' -> 3.5 OK (true division)
|
||||
'1/0' -> EvalError: division by zero OK (not ZeroDivisionError)
|
||||
'5/(3-3)' -> EvalError: division by zero OK (dynamic zero denominator)
|
||||
```
|
||||
|
||||
Implementation: explicit `if right == 0: raise EvalError(...)` at `calc/evaluator.py:18-21` — `ZeroDivisionError` cannot escape the API boundary.
|
||||
|
||||
`python -m unittest calc.test_evaluator.TestDivision -q` — 0 failures.
|
||||
|
||||
---
|
||||
|
||||
## D3 — result type
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run — CLI output (stdout only, no stderr):
|
||||
```
|
||||
'4/2' -> '2' OK (whole float -> int display)
|
||||
'9/3' -> '3' OK (whole float -> int display)
|
||||
'0/5' -> '0' OK (zero result -> int display)
|
||||
'7/2' -> '3.5' OK (non-whole)
|
||||
'1/3' -> '0.3333333333333333' OK (non-whole)
|
||||
'22/7' -> '3.142857142857143' OK (non-whole)
|
||||
```
|
||||
|
||||
Rule confirmed: `_fmt()` in `calc.py` calls `value.is_integer()` on floats; whole → cast to int for display.
|
||||
|
||||
`python -m unittest calc.test_evaluator.TestResultType -q` — 0 failures.
|
||||
|
||||
---
|
||||
|
||||
## D4 — CLI
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run — all DoD cases:
|
||||
```
|
||||
python calc.py "2+3*4" -> stdout='14' stderr='' exit=0 OK
|
||||
python calc.py "(2+3)*4" -> stdout='20' stderr='' exit=0 OK
|
||||
python calc.py "7/2" -> stdout='3.5' stderr='' exit=0 exit=0 OK
|
||||
python calc.py "4/2" -> stdout='2' stderr='' exit=0 OK
|
||||
python calc.py "1/0" -> stdout='' stderr='error: division by zero' exit=1 OK
|
||||
python calc.py "1 +" -> stdout='' stderr='error: unexpected token ...' exit=1 OK
|
||||
```
|
||||
|
||||
Additional probes:
|
||||
- No-arg: stderr='usage: calc.py <expression>', exit=1 OK
|
||||
- Empty string `""`: stderr='error: empty expression', exit=1 OK
|
||||
- No traceback in any error case (grepped for "Traceback" — not found) OK
|
||||
- Errors go to stderr, stdout is empty on error (verified via redirect) OK
|
||||
|
||||
---
|
||||
|
||||
## D5 — tests green + end-to-end
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run:
|
||||
```
|
||||
$ python -m unittest -q
|
||||
----------------------------------------------------------------------
|
||||
Ran 68 tests in 0.210s
|
||||
|
||||
OK
|
||||
```
|
||||
|
||||
Exit code 0. 68/68 pass (24 lex + 22 parse + 22 eval, including 6 CLI subprocess tests).
|
||||
|
||||
No regression in prior lex/parse tests.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
| Gate | Verdict |
|
||||
|------|---------|
|
||||
| D1 — arithmetic | **PASS** |
|
||||
| D2 — division | **PASS** |
|
||||
| D3 — result type | **PASS** |
|
||||
| D4 — CLI | **PASS** |
|
||||
| D5 — tests green | **PASS** |
|
||||
|
||||
All gates PASS. No findings. Builder may write "## DONE" to STATUS-eval.md.
|
||||
103
calculators/builder-adversary/run-02/machine-docs/REVIEW-lex.md
Normal file
103
calculators/builder-adversary/run-02/machine-docs/REVIEW-lex.md
Normal file
@ -0,0 +1,103 @@
|
||||
# REVIEW-lex — Adversary Verdicts
|
||||
|
||||
## Legend
|
||||
- PASS @<ts> — gate accepted, evidence below
|
||||
- FAIL — repro steps below, Builder must fix
|
||||
|
||||
---
|
||||
|
||||
## D1 — numbers
|
||||
**PASS @2026-06-15T00:36Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
python -c "...tokenize('42')..." → NUMBER(42, int), EOF — PASS
|
||||
python -c "...tokenize('3.14')..." → NUMBER(3.14, float), EOF — PASS
|
||||
python -c "...tokenize('.5')..." → NUMBER(0.5), EOF — PASS
|
||||
python -c "...tokenize('10.')..." → NUMBER(10.0), EOF — PASS
|
||||
```
|
||||
Type assertions: `isinstance(42, int)` ✓, `isinstance(3.14, float)` ✓
|
||||
|
||||
---
|
||||
|
||||
## D2 — operators & parens
|
||||
**PASS @2026-06-15T00:36Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
tokenize('1+2*3') kinds → ['NUMBER','PLUS','NUMBER','STAR','NUMBER','EOF'] ✓
|
||||
tokenize('3.5*(1-2)') → [('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)] ✓
|
||||
All 6 operators (PLUS MINUS STAR SLASH LPAREN RPAREN) individually tested ✓
|
||||
SLASH explicitly tested in test_lexer.py ✓
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## D3 — whitespace & errors
|
||||
**PASS @2026-06-15T00:36Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
tokenize(' 12 + 3 ') kinds → ['NUMBER','PLUS','NUMBER','EOF'] ✓
|
||||
tokenize('1 @ 2') → raises calc.lexer.LexError: unexpected character '@' at position 2
|
||||
'@' in message ✓, '2' (position) in message ✓
|
||||
Plan's verbatim command exits code 1 with correct traceback ✓
|
||||
```
|
||||
|
||||
Break-it probes run (see Adversary findings for non-blocking issues):
|
||||
- `$` raises LexError ✓
|
||||
- `x` (letter) raises LexError ✓
|
||||
- `\n` raises LexError (treated as invalid char, reasonable) ✓
|
||||
|
||||
---
|
||||
|
||||
## D4 — tests green
|
||||
**PASS @2026-06-15T00:36Z**
|
||||
|
||||
Cold run:
|
||||
```
|
||||
$ python -m unittest -q
|
||||
----------------------------------------------------------------------
|
||||
Ran 24 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
Exit code 0. 24/24 pass.
|
||||
|
||||
DoD-mandated test inputs confirmed present:
|
||||
- `" 12 + 3 "` — covered by test_spaces_between_tokens + test_padded_addition ✓
|
||||
- `"3.5*(1-2)"` — covered by test_complex_expression + test_complex_with_values ✓
|
||||
- `"1 @ 2"` raises LexError — covered by test_invalid_char_raises + test_lex_error_position ✓
|
||||
|
||||
---
|
||||
|
||||
## Non-blocking finding: unhandled ValueError for malformed number literals
|
||||
|
||||
**Severity: informational — does not fail any DoD gate**
|
||||
|
||||
`tokenize('1.2.3')`, `tokenize('.')`, `tokenize('..')` all raise Python's built-in
|
||||
`ValueError` ("could not convert string to float: ...") instead of `LexError`.
|
||||
The lexer greedily consumes digit/dot sequences then passes the raw string to
|
||||
`float()` without catching failure.
|
||||
|
||||
The DoD's D3 specifies "invalid character (e.g. @, $, a letter)" — not malformed
|
||||
number literals — so this does not block PASS. However, downstream parser/evaluator
|
||||
phases will see unexpected ValueError exceptions from edge-case inputs. The Builder
|
||||
should consider wrapping the `float(raw)` call in a try/except that re-raises as
|
||||
`LexError`.
|
||||
|
||||
This finding is noted only; the Builder may address it in a follow-up or the next phase.
|
||||
No VETO issued.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
| Gate | Verdict |
|
||||
|------|---------|
|
||||
| D1 — numbers | **PASS** |
|
||||
| D2 — operators & parens | **PASS** |
|
||||
| D3 — whitespace & errors | **PASS** |
|
||||
| D4 — tests green | **PASS** |
|
||||
|
||||
All gates PASS. Builder may write "## DONE" to STATUS-lex.md.
|
||||
@ -0,0 +1,126 @@
|
||||
# REVIEW-parse — Adversary Verdicts
|
||||
|
||||
## Legend
|
||||
- PASS @<ts> — gate accepted, evidence below
|
||||
- FAIL — repro steps below, Builder must fix
|
||||
|
||||
---
|
||||
|
||||
## D1 — precedence
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
parse(tokenize('1+2*3')) → BinOp('+', Num(1), BinOp('*', Num(2), Num(3))) ✓
|
||||
parse(tokenize('2*3+1')) → BinOp('+', BinOp('*', Num(2), Num(3)), Num(1)) ✓
|
||||
```
|
||||
Both match expected repr exactly. `*` binds tighter than `+` in both orderings.
|
||||
|
||||
Extra probe — complex chain `1+2+3*4-5`:
|
||||
→ `BinOp('-', BinOp('+', BinOp('+', Num(1), Num(2)), BinOp('*', Num(3), Num(4))), Num(5))` ✓
|
||||
`3*4` is correctly nested under addition/subtraction.
|
||||
|
||||
---
|
||||
|
||||
## D2 — left associativity
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
parse(tokenize('8-3-2')) → BinOp('-', BinOp('-', Num(8), Num(3)), Num(2)) ✓
|
||||
parse(tokenize('8/4/2')) → BinOp('/', BinOp('/', Num(8), Num(4)), Num(2)) ✓
|
||||
```
|
||||
|
||||
Extra probes:
|
||||
```
|
||||
parse(tokenize('2*3*4')) → BinOp('*', BinOp('*', Num(2), Num(3)), Num(4)) ✓
|
||||
parse(tokenize('1+2+3')) → BinOp('+', BinOp('+', Num(1), Num(2)), Num(3)) ✓
|
||||
```
|
||||
Explicit assertion `r == BinOp('+', BinOp('+', Num(1), Num(2)), Num(3))` passed.
|
||||
|
||||
---
|
||||
|
||||
## D3 — parentheses
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
parse(tokenize('(1+2)*3')) → BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)) ✓
|
||||
parse(tokenize('(-3)*2')) → BinOp('*', Unary('-', Num(3)), Num(2)) ✓
|
||||
```
|
||||
Parens correctly place `+` sub-tree under `*`.
|
||||
|
||||
---
|
||||
|
||||
## D4 — unary minus
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
parse(tokenize('-5')) → Unary('-', Num(5)) ✓
|
||||
parse(tokenize('-(1+2)')) → Unary('-', BinOp('+', Num(1), Num(2))) ✓
|
||||
parse(tokenize('3 * -2')) → BinOp('*', Num(3), Unary('-', Num(2))) ✓
|
||||
```
|
||||
|
||||
Extra probes:
|
||||
```
|
||||
parse(tokenize('--5')) → Unary('-', Unary('-', Num(5))) ✓ (recursive, correct)
|
||||
parse(tokenize('(-3)*2')) → BinOp('*', Unary('-', Num(3)), Num(2)) ✓
|
||||
```
|
||||
`_unary` is correctly recursive for double-negation.
|
||||
|
||||
---
|
||||
|
||||
## D5 — errors
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run — all five DoD-mandated cases:
|
||||
```
|
||||
'1 +' → ParseError: unexpected token 'EOF' (None) ✓
|
||||
'(1' → ParseError: expected RPAREN, got 'EOF' (None) ✓
|
||||
'1 2' → ParseError: unexpected token 'NUMBER' (2) after expression ✓
|
||||
')(' → ParseError: unexpected token 'RPAREN' (')') ✓
|
||||
'' → ParseError: empty expression ✓
|
||||
```
|
||||
All raise `ParseError` (not `ValueError`, `IndexError`, or other exceptions).
|
||||
|
||||
Extra break-it probes — all raise `ParseError` (not other exceptions):
|
||||
```
|
||||
'+5' → ParseError: unexpected token 'PLUS' ('+') ✓
|
||||
'1++2' → ParseError: unexpected token 'PLUS' ('+') ✓
|
||||
'()' → ParseError: unexpected token 'RPAREN' (')') ✓
|
||||
'1 /' → ParseError: unexpected token 'EOF' (None) ✓
|
||||
'* 2' → ParseError: unexpected token 'STAR' ('*') ✓
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## D6 — tests green
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run:
|
||||
```
|
||||
$ python -m unittest -q
|
||||
----------------------------------------------------------------------
|
||||
Ran 46 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
Exit code 0. 46/46 pass (24 lex + 22 parser).
|
||||
|
||||
DoD requires 0 failures — confirmed.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
| Gate | Verdict |
|
||||
|------|---------|
|
||||
| D1 — precedence | **PASS** |
|
||||
| D2 — left associativity | **PASS** |
|
||||
| D3 — parentheses | **PASS** |
|
||||
| D4 — unary minus | **PASS** |
|
||||
| D5 — errors | **PASS** |
|
||||
| D6 — tests green | **PASS** |
|
||||
|
||||
All gates PASS. No findings. Builder may write "## DONE" to STATUS-parse.md.
|
||||
111
calculators/builder-adversary/run-02/machine-docs/STATUS-eval.md
Normal file
111
calculators/builder-adversary/run-02/machine-docs/STATUS-eval.md
Normal file
@ -0,0 +1,111 @@
|
||||
# STATUS-eval — Builder
|
||||
|
||||
## DONE
|
||||
|
||||
## Current state
|
||||
|
||||
Gate: D1, D2, D3, D4, D5 — all PASS (Adversary verified @2026-06-15T00:54Z)
|
||||
|
||||
---
|
||||
|
||||
## Claims
|
||||
|
||||
### D1 — arithmetic
|
||||
|
||||
**What:** `evaluate(parse(tokenize(s)))` correct for `+ - * /`, precedence, parens, unary minus.
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python -m unittest calc.test_evaluator.TestArithmetic -q
|
||||
```
|
||||
|
||||
**Expected:** 0 failures. Specific cases:
|
||||
- `"2+3*4"` → 14
|
||||
- `"(2+3)*4"` → 20
|
||||
- `"8-3-2"` → 3
|
||||
- `"-2+5"` → 3
|
||||
- `"2*-3"` → -6
|
||||
|
||||
**Where:** `calc/evaluator.py` + `calc/test_evaluator.py`
|
||||
|
||||
---
|
||||
|
||||
### D2 — division
|
||||
|
||||
**What:** `/` is true division; `EvalError` raised on division by zero, not bare `ZeroDivisionError`.
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python -m unittest calc.test_evaluator.TestDivision -q
|
||||
```
|
||||
|
||||
**Expected:** 0 failures. Includes:
|
||||
- `"7/2"` → 3.5
|
||||
- `"1/0"` → `EvalError`
|
||||
- `"5/(3-3)"` → `EvalError`
|
||||
- No `ZeroDivisionError` escaping the API
|
||||
|
||||
**Where:** `calc/evaluator.py:18-21`
|
||||
|
||||
---
|
||||
|
||||
### D3 — result type
|
||||
|
||||
**What:** Whole-valued results print without `.0`; non-whole as float.
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python -m unittest calc.test_evaluator.TestResultType -q
|
||||
python calc.py "4/2" # should print: 2
|
||||
python calc.py "7/2" # should print: 3.5
|
||||
```
|
||||
|
||||
**Expected:** 0 failures; `4/2` → `2` (no dot), `7/2` → `3.5`.
|
||||
|
||||
**Rule:** The `_fmt()` function in `calc.py` checks `value.is_integer()` on floats; if true, casts to `int` for display.
|
||||
|
||||
**Where:** `calc.py:_fmt()`, `calc/test_evaluator.py:TestResultType`
|
||||
|
||||
---
|
||||
|
||||
### D4 — CLI
|
||||
|
||||
**What:** `python calc.py "2+3*4"` prints `14` and exits 0; invalid expression prints error to stderr and exits non-zero (no traceback).
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python calc.py "2+3*4" # stdout: 14, exit 0
|
||||
python calc.py "(2+3)*4" # stdout: 20, exit 0
|
||||
python calc.py "7/2" # stdout: 3.5, exit 0
|
||||
python calc.py "4/2" # stdout: 2, exit 0
|
||||
python calc.py "1/0" # stderr: error: ..., exit 1
|
||||
python calc.py "1 +" # stderr: error: ..., exit 1
|
||||
```
|
||||
|
||||
**Expected:** Exact outputs as above. All errors caught by `(LexError, ParseError, EvalError)` — no Python traceback.
|
||||
|
||||
**Where:** `calc.py`
|
||||
|
||||
---
|
||||
|
||||
### D5 — tests green + end-to-end
|
||||
|
||||
**What:** Full unittest suite (lex + parse + eval) passes with 0 failures; CLI checks cover D4.
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python -m unittest -q
|
||||
```
|
||||
|
||||
**Expected:**
|
||||
```
|
||||
Ran 68 tests in ~0.2s
|
||||
|
||||
OK
|
||||
```
|
||||
|
||||
All 68 tests pass: 24 lex + 22 parse + 22 eval (including 6 CLI subprocess tests).
|
||||
|
||||
**Commit sha:** (see latest commit after push)
|
||||
|
||||
**Where:** `calc/test_lexer.py`, `calc/test_parser.py`, `calc/test_evaluator.py`
|
||||
103
calculators/builder-adversary/run-02/machine-docs/STATUS-lex.md
Normal file
103
calculators/builder-adversary/run-02/machine-docs/STATUS-lex.md
Normal file
@ -0,0 +1,103 @@
|
||||
# STATUS — phase lex (Builder)
|
||||
|
||||
## DONE
|
||||
|
||||
All DoD gates Adversary-verified PASS. Phase complete.
|
||||
|
||||
## Gates
|
||||
|
||||
| Gate | Status |
|
||||
|------|--------|
|
||||
| D1 — numbers | **PASS** (Adversary @2026-06-15T00:36Z) |
|
||||
| D2 — operators & parens | **PASS** (Adversary @2026-06-15T00:36Z) |
|
||||
| D3 — whitespace & errors | **PASS** (Adversary @2026-06-15T00:36Z) |
|
||||
| D4 — tests green | **PASS** (Adversary @2026-06-15T00:36Z) |
|
||||
|
||||
## Post-verification fix
|
||||
|
||||
**AF-01 addressed:** Wrapped `float(raw)` in `try/except ValueError` to re-raise as `LexError` for malformed number literals like `1.2.3`, `.`, `..`. 24 tests still pass.
|
||||
|
||||
---
|
||||
|
||||
## Claim: D1 — numbers
|
||||
|
||||
**WHAT:** `calc/lexer.py::tokenize` correctly tokenizes integers and floats to NUMBER tokens with numeric Python values (int for integers, float for floats). EOF is always the final token.
|
||||
|
||||
**HOW to verify:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; t=tokenize('42'); assert t[0].kind=='NUMBER' and t[0].value==42 and isinstance(t[0].value,int) and t[1].kind=='EOF', t"
|
||||
python -c "from calc.lexer import tokenize; t=tokenize('3.14'); assert t[0].kind=='NUMBER' and abs(t[0].value-3.14)<1e-9 and isinstance(t[0].value,float), t"
|
||||
python -c "from calc.lexer import tokenize; t=tokenize('.5'); assert t[0].value==0.5, t"
|
||||
python -c "from calc.lexer import tokenize; t=tokenize('10.'); assert t[0].value==10.0, t"
|
||||
```
|
||||
|
||||
**EXPECTED:** All assertions pass (exit 0).
|
||||
|
||||
**WHERE:** `calc/lexer.py`
|
||||
|
||||
---
|
||||
|
||||
## Claim: D2 — operators & parens
|
||||
|
||||
**WHAT:** `+`, `-`, `*`, `/`, `(`, `)` each tokenize to PLUS, MINUS, STAR, SLASH, LPAREN, RPAREN respectively. `tokenize("1+2*3")` → NUMBER PLUS NUMBER STAR NUMBER EOF.
|
||||
|
||||
**HOW to verify:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; k=[t.kind for t in tokenize('1+2*3')]; assert k==['NUMBER','PLUS','NUMBER','STAR','NUMBER','EOF'], k"
|
||||
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
|
||||
```
|
||||
|
||||
**EXPECTED:**
|
||||
- First command: exit 0 (assertion passes)
|
||||
- Second command prints: `[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
|
||||
|
||||
**WHERE:** `calc/lexer.py`
|
||||
|
||||
---
|
||||
|
||||
## Claim: D3 — whitespace & errors
|
||||
|
||||
**WHAT:** Spaces and tabs between tokens are skipped. Invalid characters (letters, `@`, `$`, etc.) raise `LexError` with the offending char and its position in the message.
|
||||
|
||||
**HOW to verify:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; k=[t.kind for t in tokenize(' 12 + 3 ')]; assert k==['NUMBER','PLUS','NUMBER','EOF'], k"
|
||||
python -c "
|
||||
from calc.lexer import tokenize, LexError
|
||||
try:
|
||||
tokenize('1 @ 2')
|
||||
assert False, 'should have raised'
|
||||
except LexError as e:
|
||||
assert '@' in str(e), str(e)
|
||||
assert '2' in str(e), str(e)
|
||||
print('OK')
|
||||
"
|
||||
```
|
||||
|
||||
**EXPECTED:**
|
||||
- First command: exit 0
|
||||
- Second command prints: `OK`
|
||||
|
||||
**WHERE:** `calc/lexer.py`
|
||||
|
||||
---
|
||||
|
||||
## Claim: D4 — tests green
|
||||
|
||||
**WHAT:** `calc/test_lexer.py` passes under `python -m unittest` with 0 failures (24 tests).
|
||||
|
||||
**HOW to verify:**
|
||||
```bash
|
||||
python -m unittest -q
|
||||
```
|
||||
|
||||
**EXPECTED:**
|
||||
```
|
||||
----------------------------------------------------------------------
|
||||
Ran 24 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
Exit code 0.
|
||||
|
||||
**WHERE:** `calc/test_lexer.py`, `calc/lexer.py`
|
||||
@ -0,0 +1,114 @@
|
||||
# STATUS-parse — Builder
|
||||
|
||||
## DONE
|
||||
|
||||
All gates D1–D6 verified PASS by Adversary @2026-06-15T00:50Z. See REVIEW-parse.md.
|
||||
|
||||
---
|
||||
|
||||
## Gate: D1–D6 CLAIMED → PASS
|
||||
|
||||
All six gates are implemented and verified locally.
|
||||
|
||||
---
|
||||
|
||||
## Files
|
||||
|
||||
| File | Description |
|
||||
|---|---|
|
||||
| `calc/parser.py` | Recursive-descent parser exposing `parse(tokens) -> Node` |
|
||||
| `calc/test_parser.py` | unittest suite covering D1–D6 |
|
||||
|
||||
---
|
||||
|
||||
## AST Node Types (stable shape for evaluator)
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Any # int or float from lexer
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str # '+', '-', '*', '/'
|
||||
left: Any # Node
|
||||
right: Any # Node
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str # '-'
|
||||
operand: Any # Node
|
||||
```
|
||||
|
||||
All three are dataclasses with `__repr__` — equality comparison works via `==`.
|
||||
|
||||
---
|
||||
|
||||
## Verification commands (cold-runnable from any clone)
|
||||
|
||||
```bash
|
||||
# D6 — all tests green
|
||||
python -m unittest -q
|
||||
|
||||
# D1 — precedence: 1+2*3 => BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('1+2*3')))"
|
||||
# expected: BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))
|
||||
|
||||
# D1 — precedence: 2*3+1 => BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('2*3+1')))"
|
||||
# expected: BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))
|
||||
|
||||
# D2 — left assoc subtraction: 8-3-2 => BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8-3-2')))"
|
||||
# expected: BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))
|
||||
|
||||
# D2 — left assoc division: 8/4/2 => BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8/4/2')))"
|
||||
# expected: BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))
|
||||
|
||||
# D3 — parens override: (1+2)*3 => BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('(1+2)*3')))"
|
||||
# expected: BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))
|
||||
|
||||
# D4 — unary minus: -5 => Unary('-', Num(5))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5')))"
|
||||
# expected: Unary('-', Num(5))
|
||||
|
||||
# D4 — unary in paren: -(1+2) => Unary('-', BinOp('+', Num(1), Num(2)))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-(1+2)')))"
|
||||
# expected: Unary('-', BinOp('+', Num(1), Num(2)))
|
||||
|
||||
# D4 — unary in mul: 3 * -2 => BinOp('*', Num(3), Unary('-', Num(2)))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('3 * -2')))"
|
||||
# expected: BinOp('*', Num(3), Unary('-', Num(2)))
|
||||
|
||||
# D5 — each raises ParseError (must NOT raise any other exception)
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
|
||||
for s in ['1 +', '(1', '1 2', ')(', '']:
|
||||
try: parse(tokenize(s)); print(f'FAIL no error for {s!r}')
|
||||
except ParseError as e: print(f'OK {s!r} => ParseError: {e}')
|
||||
except Exception as e: print(f'FAIL wrong exc for {s!r}: {type(e).__name__}: {e}')
|
||||
"
|
||||
# expected: 5 lines each starting "OK"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Expected outputs (exact)
|
||||
|
||||
| Gate | Expression | Expected repr |
|
||||
|---|---|---|
|
||||
| D1 | `1+2*3` | `BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))` |
|
||||
| D1 | `2*3+1` | `BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))` |
|
||||
| D2 | `8-3-2` | `BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))` |
|
||||
| D2 | `8/4/2` | `BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))` |
|
||||
| D3 | `(1+2)*3` | `BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))` |
|
||||
| D4 | `-5` | `Unary('-', Num(5))` |
|
||||
| D4 | `-(1+2)` | `Unary('-', BinOp('+', Num(1), Num(2)))` |
|
||||
| D4 | `3 * -2` | `BinOp('*', Num(3), Unary('-', Num(2)))` |
|
||||
| D5 | `1 +` | `ParseError: unexpected token 'EOF' (None)` |
|
||||
| D5 | `(1` | `ParseError: expected RPAREN, got 'EOF' (None)` |
|
||||
| D5 | `1 2` | `ParseError: unexpected token 'NUMBER' (2) after expression` |
|
||||
| D5 | `)(` | `ParseError: unexpected token 'RPAREN' (')')` |
|
||||
| D5 | `` | `ParseError: empty expression` |
|
||||
| D6 | `python -m unittest -q` | `Ran 46 tests in …s OK` |
|
||||
Reference in New Issue
Block a user