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,9 @@
# BACKLOG-eval
## Build backlog
(Builder-owned — read-only for Adversary)
All gates D1-D5 implemented and claimed. Awaiting Adversary PASS.
## Adversary findings
None yet.

View File

@ -0,0 +1,12 @@
# BACKLOG — Phase `lex`
## Build backlog
- [x] D1: integers and floats tokenize correctly
- [x] D2: operators and parens tokenize correctly
- [x] D3: whitespace skipped; invalid chars raise LexError
- [x] D4: test suite green (21 tests, 0 failures)
- [ ] Await Adversary PASS on D1D4
## Adversary findings
<!-- Adversary writes here -->

View File

@ -0,0 +1,13 @@
# BACKLOG — Phase `parse`
## Build backlog
- [x] D1 — Precedence: implement grammar with `*`/`/` tighter than `+`/`-`
- [x] D2 — Left associativity: iterative left-fold in `_expr` and `_term`
- [x] D3 — Parentheses: `_primary` handles `LPAREN expr RPAREN`
- [x] D4 — Unary minus: `_unary` handles leading `-` recursively
- [x] D5 — Errors: `ParseError` raised for all malformed inputs
- [x] D6 — Tests green: `calc/test_parser.py` with 23 structural assertions
## Adversary findings
(none yet)

View File

@ -0,0 +1,10 @@
# DECISIONS (append-only, shared)
## D-LEX-1: Token representation
Chose `__slots__`-based class over dataclass or namedtuple. Reason: explicit, zero-overhead, easy for downstream parser/evaluator to consume via `t.kind` / `t.value` without import coupling.
## D-LEX-2: Number regex
`r"\d+\.?\d*|\.\d+"` handles integers, trailing-dot floats (`10.`), leading-dot floats (`.5`), and standard floats (`3.14`). The alternation order matters: `\d+\.?\d*` before `\.\d+` so integers match first.
## D-LEX-3: int vs float distinction
`int(raw)` when no `.` in the matched string; `float(raw)` otherwise. Preserves the plan's requirement that `42` yields int value and `3.14` yields float value.

View File

@ -0,0 +1,45 @@
# JOURNAL-eval — Adversary notes
## 2026-06-15T04:08Z — Initialized
Adversary starting for eval phase. Parse phase completed (all D1-D6 PASS).
Waiting for Builder to create STATUS-eval.md and claim gates.
Planned verification approach:
- D1: Run exact arithmetic expressions from plan, check output values
- D2: Verify true division (7/2=3.5), EvalError on divide-by-zero (not bare ZeroDivisionError)
- D3: Check output format — whole numbers no .0, fractions as float
- D4: CLI exit codes, stderr for errors, no tracebacks
- D5: Full test suite green, no regressions in lex+parse tests
---
## 2026-06-15T04:10Z — Builder implementation notes
Built evaluator, CLI, and tests from scratch in one pass.
### evaluator.py design
- EvalError wraps division-by-zero so the bare ZeroDivisionError never escapes the API.
- evaluate() is a simple recursive dispatch on Num/BinOp/Unary node types.
- True division via Python's `/` operator naturally produces float.
### calc.py CLI design
- _format() prints int for whole-valued floats (value.is_integer()), str(float) otherwise.
- All LexError/ParseError/EvalError caught → stderr message + exit(1), no traceback.
### Test run (commit 14db736)
```
$ python -m unittest -q
Ran 66 tests in 0.147s
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" → stderr: error: division by zero, exit 1
$ python calc.py "1 +" → stderr: error: unexpected token 'EOF' (None), exit 1
```

View File

@ -0,0 +1,32 @@
# JOURNAL — Phase `lex` (Builder)
## Implementation
Built `calc/lexer.py` with:
- `Token` dataclass-style class with `__slots__ = ("kind", "value")` for efficiency
- `LexError(Exception)` for invalid characters
- `tokenize(src)` using `re.compile(r"\d+\.?\d*|\.\d+")` for number matching
- Integer if no `.` in raw string; float otherwise
- Single-char dispatch table `_SINGLE` for operators/parens
- Raises `LexError` with char + position for unknown characters
- Appends `EOF` token at end
## Test run
```
$ python -m unittest -q
Ran 21 tests in 0.000s
OK
```
## Cold-verify outputs
```
$ 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')"
Traceback (most recent call last):
...
calc.lexer.LexError: unexpected character '@' at position 2
```

View File

@ -0,0 +1,43 @@
# JOURNAL — Phase `parse` (Builder)
## 2026-06-15 — Implementation
### Design decisions
- Grammar: `expr → term ((+|-) term)*`, `term → unary ((*|/) unary)*`, `unary → - unary | primary`, `primary → NUMBER | ( expr )`. Standard Pratt/recursive-descent, iterative left-fold for left associativity.
- Node types: `Num`, `BinOp`, `Unary` with `__repr__` and `__eq__` for structural test assertions.
- `ParseError` defined in `parser.py`.
### Test run output
```
$ python -m unittest -q
Ran 44 tests in 0.001s
OK
```
### Cold-verify commands verified locally
```
$ python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('1+2*3')))"
BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))
$ python -c "from calc.lexer import tokenize; from calc.parser import parse; parse(tokenize('1 +'))"
calc.parser.ParseError: unexpected token 'EOF' (None)
(exit 1)
$ python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('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/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('(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('-5')))"
Unary('-', Num(5))
$ python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-(1+2)')))"
Unary('-', BinOp('+', Num(1), Num(2)))
$ python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('3 * -2')))"
BinOp('*', Num(3), Unary('-', Num(2)))
```

View File

@ -0,0 +1,73 @@
# REVIEW-eval — Adversary verdicts
## Phase: eval
Adversary started: 2026-06-15T04:08Z
---
## eval/D1: PASS @2026-06-15T04:12Z
Cold re-run: all 5 plan expressions correct.
- `2+3*4``14`
- `(2+3)*4``20`
- `8-3-2``3`
- `-2+5``3`
- `2*-3``-6`
Additional adversarial: `((2+3)*4-10)/2``5`, `-(-3)``3`, `1-2*3+4``-1` — all correct.
D1Arithmetic suite: 8 tests, OK.
---
## eval/D2: PASS @2026-06-15T04:12Z
Cold re-run:
- `7/2``3.5` (true division) ✓
- `1/0``EvalError("division by zero")` raised, not bare `ZeroDivisionError`
- `5/(3-3)``EvalError` (expression-based zero denominator) ✓
- `EvalError.__bases__` = `(Exception,)` — not a subclass of `ZeroDivisionError`
D2Division suite: 5 tests, OK.
---
## eval/D3: PASS @2026-06-15T04:12Z
Cold re-run:
- `python calc.py "4/2"``2` (no trailing `.0`) ✓
- `python calc.py "7/2"``3.5`
- `_format(5)``'5'`, `_format(5.0)``'5'`, `_format(3.5)``'3.5'`
- Integer inputs (e.g. `2+3`) return `int` from `evaluate()`, format correctly ✓
D3ResultType suite: 3 tests, OK.
---
## eval/D4: PASS @2026-06-15T04:12Z
Cold re-run:
- `python calc.py "2+3*4"` → stdout `14`, exit 0 ✓
- `python calc.py "(2+3)*4"` → stdout `20`, exit 0 ✓
- `python calc.py "1/0"` → stderr `error: division by zero`, exit 1, stdout empty ✓
- `python calc.py "1 +"` → stderr `error: unexpected token 'EOF' (None)`, exit 1, stdout empty ✓
- No traceback in stderr on error ✓
- No args / too many args → usage to stderr, exit 1 ✓
D4CLI suite: 6 tests, OK.
---
## eval/D5: PASS @2026-06-15T04:12Z
Cold re-run: `python -m unittest -q``Ran 66 tests in 0.153s` / `OK`
- Lex tests (21) + parse tests (23) + evaluator/CLI tests (22) all green ✓
- No regressions in prior phases ✓
- Full suite run twice; consistent result ✓
---
## Summary
All gates D1D5: **PASS**. No defects. No VETO.
Verified cold from commit `14db736` / claimed at `47f3478`.

View File

@ -0,0 +1,44 @@
# REVIEW — Phase `lex` (Adversary)
## Verdicts
### D1: PASS @2026-06-15T04:02Z
Cold-run evidence:
- `tokenize("42")``[('NUMBER', 42), ('EOF', None)]`, value is `<class 'int'>`
- `tokenize("3.14")``NUMBER(3.14)` as float ✓
- `tokenize(".5")``NUMBER(0.5)` as float ✓
- `tokenize("10.")``NUMBER(10.0)` as float ✓
Regex `\d+\.?\d*|\.\d+` correctly handles all three float forms.
### D2: PASS @2026-06-15T04:02Z
Cold-run evidence:
- `tokenize("1+2*3")``['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']`
- All six operators (`+ - * / ( )`) mapped to correct kinds ✓
- Plan cold-verify: `tokenize('3.5*(1-2)')``[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
### D3: PASS @2026-06-15T04:02Z
Cold-run evidence:
- `tokenize(" 12 + 3 ")``['NUMBER', 'PLUS', 'NUMBER', 'EOF']`
- `tokenize("1 @ 2")` raises `LexError: unexpected character '@' at position 2`
- Error message contains offending char and position ✓
- Letters and `$` also raise LexError with position ✓
- Tabs skipped correctly ✓
### D4: PASS @2026-06-15T04:02Z
Cold-run evidence:
```
Ran 21 tests in 0.000s
OK
```
All 21 tests pass, 0 failures. Test suite covers D1D3 including `" 12 + 3 "`, `"3.5*(1-2)"`, and `"1 @ 2"` raising `LexError`.
## Adversarial probes (no failures found)
- Empty string `""``[EOF]` (correct)
- Whitespace-only `" "``[EOF]` (correct)
- `.5+10.` → two floats with operator between (correct)
- Newline raises LexError — consistent with plan (plan specifies only spaces/tabs are skipped)
- Position reporting is 0-indexed and accurate
## Conclusion
All four DoD gates PASS. No defects found. Builder may mark STATUS as DONE.

View File

@ -0,0 +1,44 @@
# REVIEW — Phase `parse` (Adversary)
## Status
All gates D1D6: PASS. Phase complete — no defects found.
## Gate verdicts
### D1 — Precedence: PASS @2026-06-15T04:08Z
Cold run: `parse(tokenize('1+2*3'))``BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))`
Matches expected. Also verified `2*3+1`, `10-4/2`, `1+2*3-4/2` all produce correct precedence trees.
### D2 — Left Associativity: PASS @2026-06-15T04:08Z
Cold run:
- `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))`
Also verified `1+2+3` and `2*3*4` fold left correctly.
### D3 — Parentheses: PASS @2026-06-15T04:08Z
Cold run: `parse(tokenize('(1+2)*3'))``BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))`
Also verified nested parens `((2+3))`, `8-(3-2)`, and `(42)`.
### D4 — Unary Minus: PASS @2026-06-15T04:08Z
Cold run:
- `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)))`
Also verified `--5``Unary('-', Unary('-', Num(5)))` and `1 + -(2 * 3)`.
### D5 — Errors: PASS @2026-06-15T04:08Z
All five plan-specified malformed inputs raise `ParseError` (not any other exception):
- `"1 +"` → ParseError: unexpected token 'EOF' ✓
- `"(1"` → ParseError: expected RPAREN, got 'EOF' ✓
- `"1 2"` → ParseError: unexpected token 'NUMBER' ✓
- `")("` → ParseError: unexpected token 'RPAREN' ✓
- `""` → ParseError: empty input ✓
Extra adversarial cases also raise ParseError: `*5`, `/5`, `()`, `1++2`, `*` alone.
### D6 — Tests Green: PASS @2026-06-15T04:08Z
`python -m unittest -q``Ran 44 tests in 0.001s OK`
23 parser tests (TestPrecedence×4, TestLeftAssociativity×4, TestParentheses×4, TestUnaryMinus×5, TestErrors×6) + 21 lexer tests.
All assertions are structural (equality via `__eq__` on `Num`/`BinOp`/`Unary` nodes), not evaluation.
## Adversary findings
None. No defects found.

View File

@ -0,0 +1,49 @@
# STATUS-eval — Builder
## Phase: eval
Builder claim commit: 14db7363662f9d74fcee7344c41f1bca04e31488
## Gates Claimed
### Gate D1 — arithmetic
WHAT: evaluate(parse(tokenize(s))) correct for +, -, *, /, precedence, parens, unary minus.
HOW: `python -m unittest calc.test_evaluator.D1Arithmetic -q`
EXPECTED: 8 tests, 0 failures, OK
WHERE: calc/test_evaluator.py::D1Arithmetic, commit 14db736
### Gate D2 — division
WHAT: / is true division; division by zero raises EvalError (not ZeroDivisionError).
HOW: `python -m unittest calc.test_evaluator.D2Division -q`
EXPECTED: 5 tests, 0 failures, OK
WHERE: calc/test_evaluator.py::D2Division, commit 14db736
### Gate D3 — result type
WHAT: Whole-valued results print without .0; non-whole as float.
HOW: `python -m unittest calc.test_evaluator.D3ResultType -q` AND:
`python calc.py "4/2"` → stdout: `2`
`python calc.py "7/2"` → stdout: `3.5`
EXPECTED: 3 tests OK; CLI prints "2" and "3.5"
WHERE: calc/evaluator.py, calc.py::_format(), commit 14db736
### Gate D4 — CLI
WHAT: `python calc.py "2+3*4"` prints 14 exits 0; invalid input to stderr exits non-zero.
HOW: `python -m unittest calc.test_evaluator.D4CLI -q`
EXPECTED: 6 tests, 0 failures, OK
Manual spot-checks:
`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: division by zero`, exit 1
`python calc.py "1 +"` → stderr: `error: unexpected token 'EOF' (None)`, exit 1
WHERE: calc.py, commit 14db736
### Gate D5 — tests green + end-to-end
WHAT: Full suite (lex + parse + eval) passes, 0 failures.
HOW: `python -m unittest -q`
EXPECTED: 66 tests, 0 failures, OK
WHERE: calc/test_lexer.py, calc/test_parser.py, calc/test_evaluator.py, commit 14db736
## DONE
All gates D1D5 Adversary-verified PASS (REVIEW-eval.md @2026-06-15T04:12Z). No VETO. Phase eval complete.

View File

@ -0,0 +1,49 @@
# STATUS — Phase `lex` (Builder)
## DONE
All gates D1, D2, D3, D4 verified PASS by Adversary @2026-06-15T04:02Z. Phase `lex` complete.
## Current State
Gates D1, D2, D3, D4: Adversary-verified PASS.
## Gate Claims
### D1 — Numbers
**WHAT:** `tokenize("42")``[NUMBER(42), EOF]`; integers return int, floats return float.
**HOW:** `python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('42')]); print(type(tokenize('42')[0].value))"`
**EXPECTED:** `[('NUMBER', 42), ('EOF', None)]` then `<class 'int'>`
**WHERE:** `calc/lexer.py` commit to be pushed; `calc/test_lexer.py` class `TestNumbers`
### D2 — Operators & Parens
**WHAT:** `+ - * / ( )` each tokenize to the right kind; `tokenize("1+2*3")` yields NUMBER PLUS NUMBER STAR NUMBER EOF.
**HOW:** `python -c "from calc.lexer import tokenize; print([t.kind for t in tokenize('1+2*3')])"`
**EXPECTED:** `['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']`
**WHERE:** `calc/lexer.py`; `calc/test_lexer.py` class `TestOperatorsAndParens`
### D3 — Whitespace & Errors
**WHAT:** Spaces/tabs skipped; invalid chars raise `LexError` with offending char and position.
**HOW:**
```bash
python -c "from calc.lexer import tokenize; print([t.kind for t in tokenize(' 12 + 3 ')])"
python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
```
**EXPECTED:** First: `['NUMBER', 'PLUS', 'NUMBER', 'EOF']`. Second: raises `calc.lexer.LexError: unexpected character '@' at position 2`
**WHERE:** `calc/lexer.py`; `calc/test_lexer.py` class `TestWhitespaceAndErrors`
### D4 — Tests Green
**WHAT:** `python -m unittest -q` passes 21 tests, 0 failures.
**HOW:** `python -m unittest -q` (run from repo root)
**EXPECTED:** `Ran 21 tests in ...s\n\nOK`
**WHERE:** `calc/test_lexer.py`
## Cold-verify commands (from plan)
```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 outputs:**
1. `Ran 21 tests in ...s\n\nOK`
2. `[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
3. `calc.lexer.LexError: unexpected character '@' at position 2` (raises, exits non-zero)

View File

@ -0,0 +1,87 @@
# STATUS — Phase `parse` (Builder)
## DONE
All gates D1D6: Adversary-verified PASS (see REVIEW-parse.md @2026-06-15T04:08Z). Phase complete.
## AST Node Shapes (stable API for evaluator)
```
Num(value) # numeric literal; value is int or float
BinOp(op, left, right) # op in {'+', '-', '*', '/'}; left/right are nodes
Unary(op, operand) # op is '-'; operand is a node
```
All nodes implement `__repr__` and `__eq__`.
## Gate Claims
### D1 — Precedence
**WHAT:** `*` and `/` bind tighter than `+` and `-`; `1+2*3` parses as `1+(2*3)`.
**HOW:**
```bash
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)))`
**WHERE:** `calc/parser.py``_expr` iterates `+/-`, `_term` iterates `*//`, so `*` folds first.
### D2 — Left Associativity
**WHAT:** `8-3-2` parses as `(8-3)-2`; `8/4/2` as `(8/4)/2`.
**HOW:**
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8-3-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(3)), Num(2))`
- `BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))`
**WHERE:** `calc/parser.py``_expr` and `_term` use `while` loops (iterative left fold).
### D3 — Parentheses
**WHAT:** `(1+2)*3` parses with `+` under `*`.
**HOW:**
```bash
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))`
**WHERE:** `calc/parser.py``_primary` handles `LPAREN expr RPAREN`, returning inner node to be used in `_term`.
### D4 — Unary Minus
**WHAT:** `-5`, `-(1+2)`, `3 * -2` each parse correctly.
**HOW:**
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5')))"
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-(1+2)')))"
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('3 * -2')))"
```
**EXPECTED:**
- `Unary('-', Num(5))`
- `Unary('-', BinOp('+', Num(1), Num(2)))`
- `BinOp('*', Num(3), Unary('-', Num(2)))`
**WHERE:** `calc/parser.py``_unary` handles `MINUS` recursively before `_primary`.
### D5 — Errors
**WHAT:** Malformed inputs raise `ParseError` (not any other exception).
**HOW:**
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
try:
parse(tokenize('1 +'))
print('NO ERROR')
except ParseError:
print('ParseError OK')
"
# Repeat for each case: '(1', '1 2', ')(', ''
```
**EXPECTED:** `ParseError` raised for all five inputs: `"1 +"`, `"(1"`, `"1 2"`, `")("`, `""`.
Shortcut — test suite already covers all five (TestErrors class).
**WHERE:** `calc/parser.py``_primary` raises on bad token; `parse()` raises on trailing token or empty input; `_expect` raises on mismatched RPAREN.
### D6 — Tests Green
**WHAT:** `python -m unittest -q` passes, 0 failures; covers D1D5 with structural assertions.
**HOW:**
```bash
python -m unittest -q
```
**EXPECTED:** `Ran 44 tests in ...s\n\nOK` (21 lexer + 23 parser)
**WHERE:** `calc/test_parser.py` — classes TestPrecedence, TestLeftAssociativity, TestParentheses, TestUnaryMinus, TestErrors.