artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
@ -0,0 +1,3 @@
|
||||
# BACKLOG-parse
|
||||
|
||||
(empty — all D1-D6 gates implemented and tested)
|
||||
@ -0,0 +1,9 @@
|
||||
# JOURNAL — phase eval
|
||||
|
||||
## Implementation notes
|
||||
|
||||
- `evaluate()` walks the AST recursively using isinstance checks on Num/BinOp/Unary.
|
||||
- `_coerce()` converts whole-valued floats to int (e.g. `4/2 → 2`).
|
||||
- Division by zero raises `EvalError` (not the bare `ZeroDivisionError`).
|
||||
- `calc.py` CLI catches `LexError`, `ParseError`, `EvalError` and prints to stderr with exit 1.
|
||||
- Test suite has 49 total tests (lex + parse + eval); CLI tests use subprocess to exercise the full pipeline end-to-end.
|
||||
@ -0,0 +1,12 @@
|
||||
# JOURNAL-lex
|
||||
|
||||
## Implementation notes
|
||||
|
||||
Built `calc/lexer.py` with:
|
||||
- `Token` dataclass with `kind: str` and `value: Union[int, float, None]`
|
||||
- `LexError(Exception)` for invalid characters
|
||||
- `tokenize(src: str) -> list[Token]` scanning character by character
|
||||
|
||||
Number scanning handles all three forms: integer (`42`), standard float (`3.14`), leading-dot float (`.5`), and trailing-dot float (`10.`). The key invariant: when `ch` is `.` we only enter number scanning if the next char is also a digit (to avoid confusing `.` used as an operator in other contexts). Trailing dot (`10.`) is handled because after scanning digits we check if the next char is `.` and absorb it into a float.
|
||||
|
||||
14 tests written covering D1-D4 plus edge cases.
|
||||
@ -0,0 +1,14 @@
|
||||
# JOURNAL-parse
|
||||
|
||||
## Implementation notes
|
||||
|
||||
Grammar chose iterative (not recursive) form for expr and term to ensure left-associativity without
|
||||
risk of right-fold bugs. Unary uses right-recursive call to handle `--5` naturally.
|
||||
|
||||
Empty string: `tokenize("")` returns `[Token('EOF', None)]`. `primary()` sees EOF and raises
|
||||
ParseError — no special-case needed.
|
||||
|
||||
`"1 2"` error: `parse()` checks that after `expr()` the current token is EOF; NUMBER is not EOF, so
|
||||
ParseError raised cleanly.
|
||||
|
||||
`")("` error: `primary()` is called, sees RPAREN (not NUMBER or LPAREN), raises ParseError.
|
||||
@ -0,0 +1,57 @@
|
||||
# Adversary REVIEW — phase eval
|
||||
|
||||
## Gates
|
||||
|
||||
- D1: PASS @2026-06-15T02:05Z
|
||||
- D2: PASS @2026-06-15T02:05Z
|
||||
- D3: PASS @2026-06-15T02:05Z
|
||||
- D4: PASS @2026-06-15T02:05Z
|
||||
- D5: PASS @2026-06-15T02:05Z
|
||||
|
||||
## Evidence
|
||||
|
||||
### D5 — full suite (49 tests, 0 failures)
|
||||
```
|
||||
Ran 49 tests in 0.218s
|
||||
OK
|
||||
```
|
||||
|
||||
### D1 — arithmetic / precedence / parens / unary minus
|
||||
```
|
||||
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 on zero
|
||||
```
|
||||
python calc.py "7/2" → 3.5 (exit 0) ✓
|
||||
python calc.py "1/0" → Error: Division by zero (stderr, exit 1, no traceback) ✓
|
||||
python calc.py "0/0" → Error: Division by zero (stderr, exit 1) ✓
|
||||
```
|
||||
No bare ZeroDivisionError escapes; EvalError wraps it correctly.
|
||||
|
||||
### D3 — result type
|
||||
```
|
||||
python calc.py "4/2" → 2 (int, no trailing .0) ✓
|
||||
python calc.py "7/2" → 3.5 (float) ✓
|
||||
python calc.py "100/10"→ 10 (whole-valued) ✓
|
||||
python calc.py "1/3" → 0.3333333333333333 ✓
|
||||
```
|
||||
`_coerce()` correctly converts whole-valued floats to int.
|
||||
|
||||
### D4 — CLI
|
||||
```
|
||||
python calc.py "2+3*4" → stdout: 14, exit 0 ✓
|
||||
python calc.py "1 +" → stderr: Error: Unexpected token EOF(None), exit 1, no traceback ✓
|
||||
```
|
||||
|
||||
### Adversarial extras (all correct)
|
||||
- `(-2)*(-3)` → 6 ✓
|
||||
- `100/10` → 10 (no .0) ✓
|
||||
- `1/3` → 0.333... ✓
|
||||
- `0/0` → EvalError, exit 1 ✓
|
||||
|
||||
No regressions in lex or parse phases (49 tests = prior 39 + 10 new eval tests).
|
||||
@ -0,0 +1,34 @@
|
||||
# REVIEW-lex
|
||||
|
||||
Adversary review log for phase `lex`.
|
||||
|
||||
<!-- verdicts appended below -->
|
||||
|
||||
## review(lex): PASS — 2026-06-15T00:00:00Z
|
||||
|
||||
**Commit verified:** bb84bf1
|
||||
**Verified by:** Adversary (cold run, own clone)
|
||||
|
||||
### D1: PASS @2026-06-15T00:00:00Z
|
||||
- `tokenize("42")` → `[NUMBER(42), EOF]`, value is `int` ✓
|
||||
- `tokenize("3.14")` → `NUMBER(3.14)` as float ✓
|
||||
- `tokenize(".5")` → `NUMBER(0.5)` ✓ (leading-dot float branch correct)
|
||||
- `tokenize("10.")` → `NUMBER(10.0)` as float ✓ (trailing-dot float branch correct)
|
||||
|
||||
### D2: PASS @2026-06-15T00:00:00Z
|
||||
- `tokenize("1+2*3")` → `['NUMBER','PLUS','NUMBER','STAR','NUMBER','EOF']` ✓
|
||||
- All six operator/paren kinds recognized: `+ - * / ( )` ✓
|
||||
- `tokenize("3.5*(1-2)")` → `[('NUMBER',3.5),('STAR',None),('LPAREN',None),('NUMBER',1),('MINUS',None),('NUMBER',2),('RPAREN',None),('EOF',None)]` ✓
|
||||
|
||||
### D3: PASS @2026-06-15T00:00:00Z
|
||||
- `tokenize(" 12 + 3 ")` → `['NUMBER','PLUS','NUMBER','EOF']` ✓ (spaces/tabs skipped)
|
||||
- `tokenize("1 @ 2")` → `LexError: Invalid character '@' at position 2` ✓
|
||||
- `tokenize("abc")` → `LexError` ✓
|
||||
- `tokenize("$10")` → `LexError` ✓
|
||||
- Error message includes offending char and position ✓
|
||||
|
||||
### D4: PASS @2026-06-15T00:00:00Z
|
||||
- `python -m unittest -q` → 14 tests, 0 failures ✓
|
||||
- Required cases covered: `" 12 + 3 "`, `"3.5*(1-2)"`, `"1 @ 2"` raises `LexError` ✓
|
||||
|
||||
**All gates D1–D4: PASS. No vetoes.**
|
||||
@ -0,0 +1,52 @@
|
||||
# Adversary Review — phase `parse`
|
||||
|
||||
## Verdicts
|
||||
|
||||
| Gate | Result | Evidence |
|
||||
|------|--------|----------|
|
||||
| D1 | PASS | `parse(tokenize('1+2*3'))` → `BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))` |
|
||||
| D2 | PASS | `8-3-2` → `BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))`; `8/4/2` → `BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))` |
|
||||
| D3 | PASS | `(1+2)*3` → `BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))` |
|
||||
| D4 | PASS | `-5` → `Unary('-', Num(5))`; `-(1+2)` → `Unary('-', BinOp('+', ...))`; `3 * -2` → `BinOp('*', Num(3), Unary('-', Num(2)))`; `--5` → `Unary('-', Unary('-', Num(5)))` |
|
||||
| D5 | PASS | All 5 cases `'1 +'`, `'(1'`, `'1 2'`, `')('`, `''` raise `ParseError` (not any other exception) |
|
||||
| D6 | PASS | `python -m unittest -q` → `Ran 33 tests in 0.001s OK` |
|
||||
|
||||
---
|
||||
|
||||
## Cold-run commands executed
|
||||
|
||||
```
|
||||
python -m unittest -q
|
||||
python -c "... parse(tokenize('1+2*3'))" # D1
|
||||
python -c "... parse(tokenize('4+6/2'))" # D1
|
||||
python -c "... parse(tokenize('8-3-2'))" # D2
|
||||
python -c "... parse(tokenize('8/4/2'))" # D2
|
||||
python -c "... parse(tokenize('(1+2)*3'))" # D3
|
||||
python -c "... parse(tokenize('-5'))" # D4
|
||||
python -c "... parse(tokenize('-(1+2)'))" # D4
|
||||
python -c "... parse(tokenize('3 * -2'))" # D4
|
||||
python -c "... parse(tokenize('--5'))" # D4 extra
|
||||
python -c "... [all 5 error cases]" # D5
|
||||
```
|
||||
|
||||
Extra edge cases (adversarial):
|
||||
- `()` → ParseError ✓
|
||||
- `1++2` → ParseError ✓ (no unary plus)
|
||||
- `1 + + 2` → ParseError ✓
|
||||
- `*3` → ParseError ✓
|
||||
- `1*(2+3)*4` → `BinOp('*', BinOp('*', Num(1), BinOp('+', Num(2), Num(3))), Num(4))` ✓
|
||||
- `-1+2` → `BinOp('+', Unary('-', Num(1)), Num(2))` ✓
|
||||
- `1+2*3+4` → `BinOp('+', BinOp('+', Num(1), BinOp('*', Num(2), Num(3))), Num(4))` ✓
|
||||
|
||||
---
|
||||
|
||||
## Notes
|
||||
|
||||
- The `if not tokens: raise ParseError("Empty token list")` guard in `parse()` is **dead code** — `tokenize("")` always appends an EOF token so the list is never empty. The empty-string case still raises `ParseError` correctly via `primary()` seeing EOF. Not a bug; harmless dead code.
|
||||
- Grammar structure (expr → term → unary → primary) correctly encodes precedence and left-associativity by construction.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
review(parse): PASS D1,D2,D3,D4,D5,D6 — cold-verified all gates @2026-06-15T02:00Z
|
||||
@ -0,0 +1,47 @@
|
||||
# STATUS — phase eval
|
||||
|
||||
## WHAT is claimed
|
||||
|
||||
All gates D1–D5 implemented and locally verified.
|
||||
|
||||
### Files added
|
||||
- `calc/evaluator.py` — `EvalError`, `evaluate(node) -> int | float`
|
||||
- `calc/test_evaluator.py` — unittest suite covering D1–D4 (CLI via subprocess)
|
||||
- `calc.py` — top-level CLI entry point
|
||||
|
||||
## Verify commands (exact) + expected results
|
||||
|
||||
```bash
|
||||
# D5 — whole suite (lex + parse + eval), 0 failures
|
||||
python -m unittest -q
|
||||
# Expected: OK (49 tests)
|
||||
|
||||
# D1 — arithmetic / precedence / parens / unary
|
||||
python calc.py "2+3*4" # Expected: 14
|
||||
python calc.py "(2+3)*4" # Expected: 20
|
||||
python calc.py "8-3-2" # Expected: 3
|
||||
python calc.py "-2+5" # Expected: 3
|
||||
python calc.py "2*-3" # Expected: -6
|
||||
|
||||
# D2 — true division + EvalError on zero
|
||||
python calc.py "7/2" # Expected: 3.5
|
||||
python calc.py "1/0" # Expected: error to stderr, exit non-zero, no Traceback
|
||||
|
||||
# D3 — result type
|
||||
python calc.py "4/2" # Expected: 2 (int, no trailing .0)
|
||||
python calc.py "7/2" # Expected: 3.5
|
||||
|
||||
# D4 — CLI
|
||||
python calc.py "2+3*4" # Expected stdout: 14, exit 0
|
||||
python calc.py "1 +" # Expected: error to stderr, exit non-zero, no Traceback
|
||||
```
|
||||
|
||||
## WHERE
|
||||
|
||||
Commit: `74dddbc37523257cd90f6f2d5a46fbd943c26e64`
|
||||
|
||||
- `calc/evaluator.py` — evaluator + EvalError
|
||||
- `calc/test_evaluator.py` — unittest suite (15 tests)
|
||||
- `calc.py` — CLI entry point
|
||||
|
||||
## DONE
|
||||
@ -0,0 +1,55 @@
|
||||
# STATUS-lex
|
||||
|
||||
## DONE
|
||||
|
||||
## Claimed gates: D1, D2, D3, D4
|
||||
|
||||
**Commit:** bb84bf1
|
||||
**Files:** `calc/lexer.py`, `calc/test_lexer.py`, `calc/__init__.py`
|
||||
|
||||
---
|
||||
|
||||
## Verify commands (exact, run from repo root)
|
||||
|
||||
```bash
|
||||
# D4 — tests green
|
||||
python -m unittest -q
|
||||
|
||||
# D1+D2 — complex expression with float, paren, operators
|
||||
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
|
||||
|
||||
# D3 — invalid character raises LexError
|
||||
python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Expected results
|
||||
|
||||
### `python -m unittest -q`
|
||||
```
|
||||
..............
|
||||
----------------------------------------------------------------------
|
||||
Ran 14 tests in 0.000s
|
||||
|
||||
OK
|
||||
```
|
||||
|
||||
### `tokenize('3.5*(1-2)')`
|
||||
```
|
||||
[('NUMBER', 3.5), ('STAR', None), ('LPAREN', None), ('NUMBER', 1), ('MINUS', None), ('NUMBER', 2), ('RPAREN', None), ('EOF', None)]
|
||||
```
|
||||
|
||||
### `tokenize('1 @ 2')`
|
||||
Raises `LexError: Invalid character '@' at position 2`
|
||||
|
||||
---
|
||||
|
||||
## DoD mapping
|
||||
|
||||
| Gate | DoD item | How verified |
|
||||
|------|----------|-------------|
|
||||
| D1 | Integers (`42`) and floats (`3.14`, `.5`, `10.`) tokenize to `NUMBER` with correct numeric value | `test_integer`, `test_float_standard`, `test_float_leading_dot`, `test_float_trailing_dot` |
|
||||
| D2 | `+ - * / ( )` each tokenize to right kind; `1+2*3` → `NUMBER PLUS NUMBER STAR NUMBER EOF` | `test_operators`, `test_parens`, `test_complex_expr` |
|
||||
| D3 | Whitespace skipped; invalid char raises `LexError` with offending char and position | `test_whitespace_skipped`, `test_at_raises_lex_error`, `test_error_reports_position` |
|
||||
| D4 | `python -m unittest` passes: 14 tests, 0 failures, covers `" 12 + 3 "`, `"3.5*(1-2)"`, `"1 @ 2"` raises `LexError` | All tests in `calc/test_lexer.py` |
|
||||
@ -0,0 +1,94 @@
|
||||
# STATUS-parse
|
||||
|
||||
## DONE
|
||||
|
||||
## Claimed gates: D1, D2, D3, D4, D5, D6
|
||||
|
||||
**Commit:** (see git log — latest push on main)
|
||||
**Files:** `calc/parser.py`, `calc/test_parser.py`
|
||||
|
||||
---
|
||||
|
||||
## AST Node Shapes
|
||||
|
||||
```
|
||||
Num(value) — numeric literal; value is int or float
|
||||
BinOp(op, left, right) — binary op; op in {'+', '-', '*', '/'}
|
||||
Unary(op, operand) — unary op; op is '-'
|
||||
```
|
||||
|
||||
All nodes are Python `dataclass` instances with `__eq__` and `__repr__`.
|
||||
|
||||
---
|
||||
|
||||
## Verify commands (run from repo root)
|
||||
|
||||
### D6 — all tests green
|
||||
```bash
|
||||
python -m unittest -q
|
||||
# Expected: Ran 33 tests in X.XXXs OK (14 lex + 19 parser)
|
||||
```
|
||||
|
||||
### D1 — `*` binds tighter than `+`
|
||||
```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)))
|
||||
```
|
||||
|
||||
### D2 — left associativity
|
||||
```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 override precedence
|
||||
```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
|
||||
```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 — errors raise ParseError
|
||||
```bash
|
||||
python -c "
|
||||
from calc.lexer import tokenize
|
||||
from calc.parser import parse, ParseError
|
||||
cases = ['1 +', '(1', '1 2', ')(', '']
|
||||
for src in cases:
|
||||
try:
|
||||
parse(tokenize(src))
|
||||
print(f'FAIL {src!r}: no error raised')
|
||||
except ParseError as e:
|
||||
print(f'OK {src!r}: ParseError')
|
||||
except Exception as e:
|
||||
print(f'FAIL {src!r}: wrong exception {type(e).__name__}')
|
||||
"
|
||||
# Expected: OK for all 5 cases
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## DoD mapping
|
||||
|
||||
| Gate | DoD item | How verified |
|
||||
|------|----------|-------------|
|
||||
| D1 | `*`/`/` bind tighter than `+`/`-` | `1+2*3` → `BinOp('+', Num(1), BinOp('*', ...))` |
|
||||
| D2 | Same-precedence left-associative | `8-3-2` → left-nested; `8/4/2` → left-nested |
|
||||
| D3 | Parens override precedence | `(1+2)*3` → `BinOp('*', BinOp('+', ...), ...)` |
|
||||
| D4 | Unary minus (leading, nested, in mul) | `-5`, `-(1+2)`, `3 * -2` all parsed as Unary |
|
||||
| D5 | Malformed input raises ParseError | All 5 cases raise ParseError, not other exceptions |
|
||||
| D6 | 0 failures in `python -m unittest` | 33 tests OK (14 lex + 19 parser) |
|
||||
Reference in New Issue
Block a user