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 @@
# JOURNAL — phase lex
## Implementation notes
- Used `dataclass` for `Token` so equality works naturally in tests.
- `value` field: int for integers, float for floats (including `.5` and `10.`), str for operators, None for EOF.
- Number parsing handles leading-dot (`.5`) and trailing-dot (`10.`) cases via a single scan loop tracking `has_dot`.
- `LexError` extends `Exception` directly; message includes the char repr and position index.
- 15 tests cover all DoD items plus edge cases (tab whitespace, `$`, letter `x`).

View File

@ -0,0 +1,19 @@
# JOURNAL-parse
## Implementation notes
Grammar chosen (standard arithmetic precedence):
```
expr := term (('+' | '-') term)*
term := unary (('*' | '/') unary)*
unary := '-' unary | primary
primary := NUMBER | '(' expr ')'
```
`unary` is right-recursive, which gives right-associativity to stacked unary minuses (e.g. `--5``Unary('-', Unary('-', Num(5)))`). This is standard.
`expr` and `term` are iterative loops (not recursive), so same-level operators are naturally left-associative.
The `ParseError` class is defined in `parser.py` (not lexer.py) since it's the parser's concern. All five D5 error cases raise `ParseError`, not a generic exception.
Tests assert on exact tree structure using `==` on dataclasses, not on evaluation results, per the plan's requirement.

View File

@ -0,0 +1,62 @@
# REVIEW-eval
Phase: `eval`
Adversary cold-verified at commit `3ff8ae9`.
## Gates
| Gate | Status |
|------|--------|
| D1 — arithmetic | PASS @2026-06-15T |
| D2 — division / EvalError | PASS @2026-06-15T |
| D3 — result type formatting | PASS @2026-06-15T |
| D4 — CLI | PASS @2026-06-15T |
| D5 — tests green + end-to-end | PASS @2026-06-15T |
---
## review(D1): PASS @2026-06-15
Cold run of all D1 expressions:
| Expression | Expected | Got | Type |
|---|---|---|---|
| `2+3*4` | 14 | 14 | int ✓ |
| `(2+3)*4` | 20 | 20 | int ✓ |
| `8-3-2` | 3 | 3 | int ✓ |
| `-2+5` | 3 | 3 | int ✓ |
| `2*-3` | -6 | -6 | int ✓ |
Additional edge cases probed: `--5`→5, `-(3+2)`→-5, `0*100`→0. All correct.
## review(D2): PASS @2026-06-15
- `7/2` → 3.5 (true division, not integer) ✓
- `1/0` raises `EvalError("division by zero")`, not bare `ZeroDivisionError`
- `0/5` → 0 ✓
- `8/4/2` → 1 (left-associative) ✓
## review(D3): PASS @2026-06-15
- `4/2``2` type=int ✓ (whole result coerced to int)
- `7/2``3.5` type=float ✓ (fractional stays float)
- `2+3*4``14` type=int ✓ (integer arithmetic stays int)
- CLI output: `python calc.py "4/2"` prints `2` (no trailing `.0`) ✓
- CLI output: `python calc.py "7/2"` prints `3.5`
## review(D4): PASS @2026-06-15
- `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"``error: division by zero` to stderr, exit 1 ✓ (no traceback)
- `python calc.py "1 +"``error: unexpected token 'EOF' (None)` to stderr, exit 1 ✓ (no traceback)
## review(D5): PASS @2026-06-15
```
python -m unittest -q
Ran 52 tests in 0.001s
OK
```
52 tests, 0 failures, 0 errors. Covers lex + parse (prior phases) + evaluator (D1D3) + CLI. No regressions.

View File

@ -0,0 +1,65 @@
# REVIEW-lex
Adversary review log for phase `lex`.
<!-- verdicts appended as: review(<id>): PASS|FAIL ... -->
## review(D1): PASS @2026-06-15T00:00:00Z
Verified at sha `8d523a2`.
```
NUMBER 42 int EOF # tokenize("42")
NUMBER 0.5 # tokenize(".5")
NUMBER 10.0 float # tokenize("10.")
```
`tokenize("42")``[NUMBER(42), EOF]` with `int` type. `.5``0.5 float`. `10.``10.0 float`. All correct.
---
## review(D2): PASS @2026-06-15T00:00:00Z
Verified at sha `8d523a2`.
```
['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'] # tokenize("1+2*3")
['PLUS', 'MINUS', 'STAR', 'SLASH', 'LPAREN', 'RPAREN', 'EOF'] # tokenize("+-*/()")
```
All six operator/paren kinds correct.
---
## review(D3): PASS @2026-06-15T00:00:00Z
Verified at sha `8d523a2`.
```
['NUMBER', 'PLUS', 'NUMBER', 'EOF'] # tokenize(" 12 + 3 ")
calc.lexer.LexError: unexpected character '@' at position 2 # tokenize("1 @ 2")
```
Whitespace skipped; `LexError` raised with offending char and position for `@`.
**Edge-case warning (non-blocking):** A lone `.` raises `ValueError` (uncaught) rather than `LexError`. Not covered by the DoD but is a latent bug for callers. Also `1..2` silently lexes as `NUMBER(1.0) NUMBER(0.2)` instead of erroring. Neither breaks D1D4 as specified.
---
## review(D4): PASS @2026-06-15T00:00:00Z
Verified at sha `8d523a2`.
```
Ran 15 tests in 0.000s
OK
```
Plan cold-verify commands:
```
[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]
calc.lexer.LexError: unexpected character '@' at position 2
```
Both match expected output from the plan exactly. 15 tests, 0 failures.

View File

@ -0,0 +1,45 @@
# Adversary Review — parse phase
Verified cold at commit d97df78.
## Verdicts
| Gate | Verdict | Timestamp |
|------|---------|-----------|
| D1 | PASS | 2026-06-15T03:28Z |
| D2 | PASS | 2026-06-15T03:28Z |
| D3 | PASS | 2026-06-15T03:28Z |
| D4 | PASS | 2026-06-15T03:28Z |
| D5 | PASS | 2026-06-15T03:28Z |
| D6 | PASS | 2026-06-15T03:28Z |
## Evidence
**D6 — test suite:** `python -m unittest -q` → Ran 34 tests in 0.001s OK
**D1 — precedence:** `parse(tokenize('1+2*3'))``BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))`
`*` is in the right subtree, proving it binds tighter than `+`.
**D2 — left associativity:**
- `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))`
**D3 — parentheses:** `parse(tokenize('(1+2)*3'))``BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))`
**D4 — unary minus:**
- `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)))`
**D5 — ParseError for all 5 plan-specified malformed inputs:**
- `'1 +'``ParseError: unexpected token 'EOF' (None)`
- `'(1'``ParseError: expected ')' but got 'EOF'`
- `'1 2'``ParseError: unexpected token 'NUMBER' (2) after expression`
- `')('``ParseError: unexpected token 'RPAREN' (')')`
- `''``ParseError: empty input`
**Adversarial extras (not in plan, all pass):**
- `'--5'``Unary('-', Unary('-', Num(5)))` (recursive unary) ✓
- `'2+3*4-1'` → correct mixed-precedence left-assoc tree ✓
- `'-3*-2'``BinOp('*', Unary('-', Num(3)), Unary('-', Num(2)))`
- `'*5'``ParseError` (bare leading operator) ✓

View File

@ -0,0 +1,39 @@
# STATUS — eval phase
## Claimed gates: D1, D2, D3, D4, D5
## Verify commands (cold, run from repo root)
```bash
python -m unittest -q
python calc.py "2+3*4"
python calc.py "(2+3)*4"
python calc.py "7/2"
python calc.py "4/2"
python calc.py "1/0" # error to stderr, exit 1
python calc.py "1 +" # error to stderr, exit 1
```
## Expected results
| Command | Expected output | Expected exit |
|---------|----------------|---------------|
| `python -m unittest -q` | `Ran 52 tests in …s OK` | 0 |
| `python calc.py "2+3*4"` | `14` | 0 |
| `python calc.py "(2+3)*4"` | `20` | 0 |
| `python calc.py "7/2"` | `3.5` | 0 |
| `python calc.py "4/2"` | `2` | 0 |
| `python calc.py "1/0"` | `error: division by zero` (stderr) | 1 |
| `python calc.py "1 +"` | `error: …` (stderr) | 1 |
## Files added
- `calc/evaluator.py``evaluate(node)` walker + `EvalError`
- `calc/test_evaluator.py` — 18 unittest cases covering D1D3
- `calc.py` — CLI entry point (D4)
## Commit SHA
3ff8ae9
## DONE

View File

@ -0,0 +1,95 @@
# STATUS — phase lex
## DONE
## claim(D1): numbers tokenize correctly
**What:** Integers and floats (including `.5`, `10.`) produce a single `NUMBER` token with the correct numeric type (int or float), followed by `EOF`.
**How to verify:**
```bash
python -c "from calc.lexer import tokenize; toks = tokenize('42'); print(toks[0].kind, toks[0].value, type(toks[0].value).__name__, toks[1].kind)"
python -c "from calc.lexer import tokenize; toks = tokenize('3.14'); print(toks[0].kind, toks[0].value, type(toks[0].value).__name__)"
python -c "from calc.lexer import tokenize; toks = tokenize('.5'); print(toks[0].kind, toks[0].value)"
python -c "from calc.lexer import tokenize; toks = tokenize('10.'); print(toks[0].kind, toks[0].value, type(toks[0].value).__name__)"
```
**Expected:**
```
NUMBER 42 int EOF
NUMBER 3.14 float
NUMBER 0.5 float
NUMBER 10.0 float
```
**Files:** `calc/lexer.py`, `calc/__init__.py`
---
## claim(D2): operators and parens tokenize correctly
**What:** `+ - * / ( )` each produce their respective token kind; `tokenize("1+2*3")` yields `NUMBER PLUS NUMBER STAR NUMBER EOF`.
**How to verify:**
```bash
python -c "from calc.lexer import tokenize; print([t.kind for t in tokenize('1+2*3')])"
python -c "from calc.lexer import tokenize; print([t.kind for t in tokenize('+-*/()')] )"
```
**Expected:**
```
['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']
['PLUS', 'MINUS', 'STAR', 'SLASH', 'LPAREN', 'RPAREN', 'EOF']
```
**Files:** `calc/lexer.py`
---
## claim(D3): whitespace skipped, invalid chars raise LexError
**What:** Spaces/tabs between tokens are silently skipped. Any unrecognized character (e.g. `@`, `$`, letter) raises `LexError` with the offending character and its position in the message.
**How to verify:**
```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 command: `['NUMBER', 'PLUS', 'NUMBER', 'EOF']`
- Second command: raises `calc.lexer.LexError: unexpected character '@' at position 2`
**Files:** `calc/lexer.py`
---
## claim(D4): all tests pass
**What:** `calc/test_lexer.py` (15 test cases covering D1D3, including the three required expressions) passes with 0 failures.
**How to verify:**
```bash
python -m unittest -q
```
**Expected:**
```
Ran 15 tests in 0.000s
OK
```
**Also run the plan's exact cold-verify commands:**
```bash
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:**
```
[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]
Traceback ... LexError: unexpected character '@' at position 2
```
**Files:** `calc/test_lexer.py`

View File

@ -0,0 +1,92 @@
# STATUS-parse
Phase: `parse`
Builder: CLAIMING D1D6
## Claims
| Gate | DoD item | Status |
|------|----------|--------|
| D1 | Correct precedence: `*`/`/` bind tighter than `+`/`-` | CLAIMED |
| D2 | Left associativity for same-precedence operators | CLAIMED |
| D3 | Parentheses override precedence | CLAIMED |
| D4 | Unary minus (leading, nested, after operator) | CLAIMED |
| D5 | Malformed input raises `ParseError` | CLAIMED |
| D6 | `calc/test_parser.py` passes: 34 tests, 0 failures | CLAIMED |
## Implementation
- **`calc/parser.py`** — recursive-descent parser exposing `parse(tokens) -> Node`
- **`calc/test_parser.py`** — unittest suite covering D1D5 (34 tests)
### AST node shapes
```
Num(value) # value: int | float
BinOp(op, left, right) # op: '+' | '-' | '*' | '/'
Unary(op, operand) # op: '-'
```
## Verify (cold)
```bash
python -m unittest -q
# Expected: Ran 34 tests in 0.00xs OK
```
### D1 — precedence assertion
```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)))
```
The `*` is in the subtree (right of `+`), proving `*` binds tighter.
### D2 — left associativity assertions
```bash
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 — parentheses assertion
```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))
```
### D4 — unary minus assertions
```bash
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 — error assertions
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; parse(tokenize('1 +'))" 2>&1 | grep ParseError
# Expected: line containing "calc.parser.ParseError"
python -c "from calc.lexer import tokenize; from calc.parser import parse; parse(tokenize('(1'))" 2>&1 | grep ParseError
python -c "from calc.lexer import tokenize; from calc.parser import parse; parse(tokenize('1 2'))" 2>&1 | grep ParseError
python -c "from calc.lexer import tokenize; from calc.parser import parse; parse(tokenize(')('))" 2>&1 | grep ParseError
python -c "from calc.lexer import tokenize; from calc.parser import parse; parse(tokenize(''))" 2>&1 | grep ParseError
```
All five raise `calc.parser.ParseError`.
## Commit
Implemented at HEAD (see `git log --oneline -1` after push).
## DONE
All D1D6 gates verified PASS by Adversary at 2026-06-15T03:28Z (commit d97df78). Phase complete.