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,10 @@
# JOURNAL — phase `lex`
## 2026-06-15
- Read plan, set up `calc/` package with `lexer.py` and `test_lexer.py`.
- `Token` uses `__eq__` for easy test assertions; `__repr__` for readable output.
- Number parsing: handles integers, `3.14`, `.5`, `10.`; lone `.` raises LexError.
- Operator dispatch via `_SINGLE` dict — clean and extensible.
- 12 tests pass, all plan cold-verify commands confirmed locally before claiming gates.
- Pulled Adversary's REVIEW-lex.md (all PENDING) before commit — no conflicts.

View File

@ -0,0 +1,24 @@
# JOURNAL — phase `parse`
## Approach
Recursive-descent parser with standard precedence climbing:
- `expr` handles `+` and `-` (lowest precedence), left-associative via iteration
- `_term` handles `*` and `/` (higher precedence), left-associative via iteration
- `_unary` handles unary `-` recursively (right-recursive so `--5` works)
- `_primary` handles numbers and parenthesized subexpressions
Empty input detected early (before `_Parser` is constructed) by checking if the token list
is empty or contains only EOF. This avoids a confusing error path through `_primary`.
## Gate verification results (local)
- D1: `1+2*3``BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))`
- 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: all 5 error cases raise `ParseError`
- D6: 30 tests, 0 failures ✓

View File

@ -0,0 +1,50 @@
# REVIEW — phase `eval`
Cold-verified at commit `6e385c92a1bb145bc97183dfed8016a33f86f3ca` (pulled via `b136909`).
## D1: PASS @2026-06-15T00:11Z
All five arithmetic cases correct:
- `2+3*4` → 14 ✓
- `(2+3)*4` → 20 ✓
- `8-3-2` → 3 ✓
- `-2+5` → 3 ✓
- `2*-3` → -6 ✓
Adversarial extras also pass: `(-3)*(-3)=9`, `10-4-3=3`, left-assoc chain `2+3+4+5=14`.
## D2: PASS @2026-06-15T00:11Z
- `7/2``3.5` (true division, returns float) ✓
- `1/0` raises `EvalError("division by zero")` — not bare `ZeroDivisionError`
- `0/1` does not crash; returns `0`
## D3: PASS @2026-06-15T00:11Z
- `format_result(evaluate(parse(tokenize("4/2"))))``'2'`
- `format_result(evaluate(parse(tokenize("7/2"))))``'3.5'`
- `0/1``'0'` (not `'0.0'`) ✓
- Rule documented in `evaluator.format_result` docstring ✓
## D4: PASS @2026-06-15T00:11Z
```
python calc.py "2+3*4" → 14, exit 0 ✓
python calc.py "(2+3)*4" → 20, exit 0 ✓
python calc.py "7/2" → 3.5, exit 0 ✓
python calc.py "4/2" → 2, exit 0 ✓
python calc.py "1/0" → "error: division by zero" on stderr, exit 1 ✓
python calc.py "1 +" → "error: unexpected token EOF(None)" on stderr, exit 1 ✓
```
Extra adversarial CLI checks: `""`, `"2+"`, `"*3"` all print clean error lines to stderr, exit 1, no traceback ✓
## D5: PASS @2026-06-15T00:11Z
```
python -m unittest -q
Ran 44 tests in 0.049s
OK
```
0 failures, no regressions in lex/parse suites ✓

View File

@ -0,0 +1,68 @@
# REVIEW — phase `lex`
Adversary cold-verification log. Updated each time a DoD gate is checked.
## Gates
| Gate | Status | Timestamp |
|------|--------|-----------|
| D1 — numbers | PASS | 2026-06-15T02:05Z |
| D2 — operators & parens | PASS | 2026-06-15T02:05Z |
| D3 — whitespace & errors | PASS | 2026-06-15T02:05Z |
| D4 — tests green | PASS | 2026-06-15T02:05Z |
---
## Verdicts
### review(D4): PASS @2026-06-15T02:05Z
```
python -m unittest -q
→ Ran 12 tests in 0.000s OK
```
12 tests, 0 failures. Covers D1D3 including required cases.
### review(D1): PASS @2026-06-15T02:05Z
```
tokenize("42") → [('NUMBER', 42), ('EOF', None)] ✓
tokenize(".5") → [('NUMBER', 0.5), ('EOF', None)] ✓
tokenize("10.") → [('NUMBER', 10.0), ('EOF', None)] ✓
tokenize("3.14") → NUMBER(3.14) verified via test suite ✓
```
Integer values are `int`, float values are `float`. EOF is present.
### review(D2): PASS @2026-06-15T02:05Z
```
tokenize("1+2*3") kinds → ['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'] ✓
All six operators/parens + - * / ( ) each map to the correct kind.
```
### review(D3): PASS @2026-06-15T02:05Z
```
tokenize(" 12 + 3 ") → [('NUMBER', 12), ('PLUS', '+'), ('NUMBER', 3), ('EOF', None)] ✓
tokenize("1 @ 2") → LexError: Invalid character '@' at position 2 ✓
tokenize("abc") → LexError: Invalid character 'a' at position 0 ✓
tokenize("$") → LexError: Invalid character '$' at position 0 ✓
```
Tabs also skipped (verified in test suite). Error messages include the offending char and position.
---
## Adversarial edge cases tried (all behaved correctly)
- Empty string `""``[EOF]` (no crash)
- Whitespace-only `"\t \t"``[EOF]`
- Leading-dot `.5``NUMBER(0.5)`
- Trailing-dot `10.``NUMBER(10.0)`
- Bare dot `.` → raises `LexError` (not a valid number)
- Letter `abc` → raises `LexError` at position 0
- Dollar `$` → raises `LexError` at position 0
No issues found. All four DoD gates independently verified at commit `a1c68aa`.

View File

@ -0,0 +1,74 @@
# REVIEW — phase `parse`
## Verdict
review(D1-D6): PASS — all six gates verified cold at a05812d
---
## D1: PASS @2026-06-15T02:10Z
Precedence correct: `*`/`/` bind tighter than `+`/`-` via two-level grammar (`expr``_term``_unary``_primary`).
```
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)) ✓
parse(tokenize('9-4/2')) => BinOp('-', Num(9), BinOp('/', Num(4), Num(2))) ✓
```
## D2: PASS @2026-06-15T02:10Z
Left associativity correct: while-loops in `expr()` and `_term()` accumulate left-to-right.
```
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)) ✓
parse(tokenize('1+2+3')) => BinOp('+', BinOp('+', Num(1), Num(2)), Num(3)) ✓
```
## D3: PASS @2026-06-15T02:10Z
Parentheses override precedence via `_primary()` grouping.
```
parse(tokenize('(1+2)*3')) => BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)) ✓
parse(tokenize('((2+3))')) => BinOp('+', Num(2), Num(3)) ✓
```
## D4: PASS @2026-06-15T02:10Z
Unary minus handled at `_unary()`, right-recursive, correct for all cases including nested and post-operator.
```
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))) ✓
parse(tokenize('--5')) => Unary('-', Unary('-', Num(5))) ✓
parse(tokenize('---5')) => Unary('-', Unary('-', Unary('-', Num(5)))) ✓
parse(tokenize('-(1+2)*3')) => BinOp('*', Unary('-', BinOp('+', Num(1), Num(2))), Num(3)) ✓
```
## D5: PASS @2026-06-15T02:10Z
All five required error cases raise `ParseError` (not any other exception). Adversarial extras also raise correctly.
```
'1 +' => ParseError ✓
'(1' => ParseError ✓
'1 2' => ParseError ✓
')(' => ParseError ✓
'' => ParseError ✓
# additional probes:
'*5' => ParseError ✓
'()' => ParseError ✓
'+' => ParseError ✓
'1+2)' => ParseError ✓
```
## D6: PASS @2026-06-15T02:10Z
`python -m unittest -q` → 30 tests, 0 failures, 0 errors.
---
Gates to verify: D1 D2 D3 D4 D5 D6 — all PASS.

View File

@ -0,0 +1,82 @@
# STATUS — phase `eval`
## DONE
## Claims
Gates D1D5 are implemented and verified at commit `b1369091c751655df8925685b6a2187123aeb6ff`.
### D1 — arithmetic
**What:** `evaluate(parse(tokenize(s)))` returns correct integer results for `+`, `-`, `*`, `/` with precedence and parens.
**Verify:**
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate; print(evaluate(parse(tokenize('2+3*4'))))"
python -c "from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate; print(evaluate(parse(tokenize('(2+3)*4'))))"
python -c "from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate; print(evaluate(parse(tokenize('8-3-2'))))"
python -c "from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate; print(evaluate(parse(tokenize('-2+5'))))"
python -c "from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate; print(evaluate(parse(tokenize('2*-3'))))"
```
**Expected:** `14`, `20`, `3`, `3`, `-6`
### D2 — division
**What:** `/` is true division; division by zero raises `EvalError`, not bare `ZeroDivisionError`.
**Verify:**
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate; print(evaluate(parse(tokenize('7/2'))))"
python -c "
from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate, EvalError
try:
evaluate(parse(tokenize('1/0')))
print('FAIL: no exception')
except EvalError as e:
print('PASS: EvalError:', e)
except ZeroDivisionError:
print('FAIL: bare ZeroDivisionError')
"
```
**Expected:** `3.5` then `PASS: EvalError: division by zero`
### D3 — result type
**What:** Whole-valued results print without `.0`; non-whole print as float. Rule in `evaluator.format_result`.
**Verify:**
```bash
python -c "from calc.evaluator import format_result; from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate; print(repr(format_result(evaluate(parse(tokenize('4/2'))))))"
python -c "from calc.evaluator import format_result; from calc.lexer import tokenize; from calc.parser import parse; from calc.evaluator import evaluate; print(repr(format_result(evaluate(parse(tokenize('7/2'))))))"
```
**Expected:** `'2'` and `'3.5'`
### D4 — CLI
**What:** `python calc.py <expr>` evaluates expression; invalid expressions print to stderr and exit non-zero.
**Verify:**
```bash
python calc.py "2+3*4" # prints: 14, exit 0
python calc.py "(2+3)*4" # prints: 20, exit 0
python calc.py "7/2" # prints: 3.5, exit 0
python calc.py "4/2" # prints: 2, exit 0
python calc.py "1/0"; echo "exit: $?" # error on stderr, exit 1
python calc.py "1 +"; echo "exit: $?" # error on stderr, exit 1
```
### D5 — tests green
**What:** Full unittest suite (lex + parse + eval) passes with 0 failures.
**Verify:**
```bash
python -m unittest -q
```
**Expected:** `Ran 44 tests in ... OK` (0 failures)
## Files
- `calc/evaluator.py``EvalError`, `evaluate(node)`, `format_result(value)`
- `calc.py` — CLI entry point
- `calc/test_evaluator.py` — unittest suite covering D1D5

View File

@ -0,0 +1,59 @@
# STATUS — phase `lex`
## DONE
## Claimed Gates
claim(D1): Numbers — integers and floats tokenize to NUMBER tokens with correct numeric values.
claim(D2): Operators & parens — `+ - * / ( )` each produce the right kind token.
claim(D3): Whitespace & errors — spaces/tabs skipped; invalid chars raise `LexError` with char + position.
claim(D4): Tests green — `python -m unittest -q` passes, 12 tests, 0 failures.
## Commit
SHA: a1c68aa
Files:
- `calc/lexer.py``Token`, `LexError`, `tokenize()`
- `calc/test_lexer.py` — 12 unittest tests covering D1D4
- `calc/__init__.py` — empty package init
## How to Verify (exact commands from plan)
```bash
# D4 — tests pass
python -m unittest -q
# Expected: "Ran 12 tests in 0.00s" + "OK"
# D1 + D2 + D3 — tokenize complex expression
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
# Expected: [('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]
# D3 — invalid char raises LexError
python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
# Expected: LexError: Invalid character '@' at position 2
```
## Additional spot-checks
```bash
# D1 — integer
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('42')])"
# Expected: [('NUMBER', 42), ('EOF', None)]
# D1 — leading dot float
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('.5')])"
# Expected: [('NUMBER', 0.5), ('EOF', None)]
# D1 — trailing dot float
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('10.')])"
# Expected: [('NUMBER', 10.0), ('EOF', None)]
# D2 — "1+2*3" kinds
python -c "from calc.lexer import tokenize; print([t.kind for t in tokenize('1+2*3')])"
# Expected: ['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']
# D3 — whitespace test " 12 + 3 "
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize(' 12 + 3 ')])"
# Expected: [('NUMBER', 12), ('PLUS', '+'), ('NUMBER', 3), ('EOF', None)]
```

View File

@ -0,0 +1,70 @@
## DONE
# STATUS — phase `parse`
## Claimed gates: D1 D2 D3 D4 D5 D6
## Files
- `calc/parser.py` — parser implementation
- `calc/test_parser.py` — unittest suite
## AST node shapes
```
Num(value) — leaf numeric literal; value is int or float
BinOp(op, left, right) — binary op; op is one of '+' '-' '*' '/'
Unary(op, operand) — unary op; op is '-'
```
All nodes implement `__repr__` and `__eq__`.
## How to verify
```bash
# D6 — all tests green
python -m unittest -q
# D1 — * binds tighter than +
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)))
# D2 — left associativity
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))
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 precedence
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
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5')))"
# expected: Unary('-', Num(5))
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-(1+2)')))"
# expected: Unary('-', BinOp('+', Num(1), 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 — ParseError for malformed input
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('FAIL', s)
except ParseError:
print('OK', repr(s))
"
# expected: all 5 lines print OK
# D1/D3 anti-confusion check — structure differs:
# 1+2*3 => BinOp('+', Num(1), BinOp('*', Num(2), Num(3))) [* is the right child of +]
# (1+2)*3 => BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)) [+ is the left child of *]
```
## Commit
`a05812d` — claim(D1-D6): add recursive-descent parser with full gate coverage