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,3 @@
__pycache__/
*.pyc
*.pyo

View File

@ -0,0 +1,17 @@
# git history (claim/review handshake), from the run's shared bare repo
984e0b7 status: mark eval phase DONE — all D1D5 Adversary-verified PASS
ebe1987 review(D1,D2,D3,D4,D5): PASS — all eval gates cold-verified from work-adv clone
070dc92 claim(D1,D2,D3,D4,D5): eval phase complete — 68 tests green, all CLI checks pass
9ffa803 review(eval): initialize Adversary tracking files for eval phase
1914b53 status: mark parse phase DONE — all D1D6 Adversary-verified PASS
f444dc8 review(D1,D2,D3,D4,D5,D6): PASS — all parse gates cold-verified from work-adv clone
79016f1 claim(D1,D2,D3,D4,D5,D6): parser complete — 44 tests green, all gates verified locally
88df238 chore: finalize lex phase status and lexer dot-fix
0371b28 review(parse): initialize Adversary tracking files for parse phase
19b3673 review(D1,D2,D3,D4): PASS — all gates cold-verified from work-adv clone
09bccc4 chore: initialize Adversary machine-docs for lex phase
c7cd94e status: mark claim tasks done in backlog, awaiting Adversary review
6ee0968 chore: add .gitignore for pycache
a0745d4 claim(D1,D2,D3,D4): all gates complete — 18 tests green, lexer verified
f67144b feat: implement calc/lexer.py and test_lexer.py (D1-D4 complete)
f7a0f44 chore: seed

View File

@ -0,0 +1 @@
# calc work repo

View File

@ -0,0 +1 @@
original path: /tmp/ao-campaign-Ofyz4E/builder-adversary/r3

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
"""calc — evaluate an arithmetic expression from the command line."""
import sys
from calc.lexer import LexError, tokenize
from calc.parser import ParseError, parse
from calc.evaluator import EvalError, evaluate
def main():
if len(sys.argv) != 2:
print("usage: calc.py <expression>", file=sys.stderr)
sys.exit(1)
try:
result = evaluate(parse(tokenize(sys.argv[1])))
print(result)
except (LexError, ParseError, EvalError) as exc:
print(f"error: {exc}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()

View File

@ -0,0 +1,47 @@
"""Evaluator for the arithmetic AST produced by calc.parser.
Result-type rule: if a computation yields a float that is whole-valued
(e.g. 4/2 == 2.0), it is coerced to int before returning. Non-whole
floats (e.g. 7/2 == 3.5) are returned as float.
"""
from .parser import BinOp, Num, Unary
class EvalError(Exception):
"""Raised on a runtime evaluation error (e.g. division by zero)."""
def _coerce(value):
if isinstance(value, float) and value == int(value):
return int(value)
return value
def evaluate(node):
"""Walk an AST node and return int | float.
Raises:
EvalError: on division by zero.
"""
if isinstance(node, Num):
return _coerce(node.value)
if isinstance(node, Unary):
return _coerce(-evaluate(node.operand))
if isinstance(node, BinOp):
left = evaluate(node.left)
right = evaluate(node.right)
op = node.op
if op == '+':
return _coerce(left + right)
if op == '-':
return _coerce(left - right)
if op == '*':
return _coerce(left * right)
if op == '/':
if right == 0:
raise EvalError("division by zero")
return _coerce(left / right)
raise EvalError(f"unknown node type: {type(node).__name__}")

View File

@ -0,0 +1,64 @@
"""Lexer for arithmetic expressions."""
from dataclasses import dataclass
from typing import Union
class LexError(Exception):
"""Raised when the lexer encounters an invalid character."""
@dataclass
class Token:
kind: str
value: Union[int, float, None] = None
def __repr__(self):
if self.value is None:
return self.kind
return f"{self.kind}({self.value})"
_SINGLE = {
'+': 'PLUS',
'-': 'MINUS',
'*': 'STAR',
'/': 'SLASH',
'(': 'LPAREN',
')': 'RPAREN',
}
def tokenize(src: str) -> list:
tokens = []
i = 0
while i < len(src):
ch = src[i]
if ch in ' \t':
i += 1
continue
if ch in _SINGLE:
tokens.append(Token(_SINGLE[ch]))
i += 1
continue
if ch.isdigit() or ch == '.':
j = i
has_dot = False
while j < len(src) and (src[j].isdigit() or (src[j] == '.' and not has_dot)):
if src[j] == '.':
has_dot = True
j += 1
raw = src[i:j]
if raw == '.':
raise LexError(f"invalid character '.' at position {i}")
value = float(raw) if has_dot else int(raw)
tokens.append(Token('NUMBER', value))
i = j
continue
raise LexError(f"invalid character {ch!r} at position {i}")
tokens.append(Token('EOF'))
return tokens

View File

@ -0,0 +1,123 @@
"""Recursive-descent parser for arithmetic expressions.
AST nodes:
Num(value) — numeric literal
BinOp(op, left, right) — binary op; op in {'+', '-', '*', '/'}
Unary(op, operand) — unary op; op == '-'
"""
from dataclasses import dataclass
from typing import Union
from .lexer import Token
class ParseError(Exception):
"""Raised on malformed input."""
@dataclass
class Num:
value: Union[int, float]
def __repr__(self):
return f"Num({self.value!r})"
@dataclass
class BinOp:
op: str
left: object
right: object
def __repr__(self):
return f"BinOp({self.op!r}, {self.left!r}, {self.right!r})"
@dataclass
class Unary:
op: str
operand: object
def __repr__(self):
return f"Unary({self.op!r}, {self.operand!r})"
_KIND_TO_OP = {
'PLUS': '+',
'MINUS': '-',
'STAR': '*',
'SLASH': '/',
}
class _Parser:
def __init__(self, tokens: list):
self._tokens = tokens
self._pos = 0
def _peek(self) -> Token:
return self._tokens[self._pos]
def _consume(self, kind: str = None) -> Token:
tok = self._tokens[self._pos]
if kind is not None and tok.kind != kind:
raise ParseError(f"expected {kind!r}, got {tok.kind!r}")
self._pos += 1
return tok
def parse(self):
if self._peek().kind == 'EOF':
raise ParseError("empty expression")
node = self._expr()
if self._peek().kind != 'EOF':
raise ParseError(f"unexpected token {self._peek().kind!r}")
return node
def _expr(self):
node = self._term()
while self._peek().kind in ('PLUS', 'MINUS'):
op = _KIND_TO_OP[self._consume().kind]
right = self._term()
node = BinOp(op, node, right)
return node
def _term(self):
node = self._unary()
while self._peek().kind in ('STAR', 'SLASH'):
op = _KIND_TO_OP[self._consume().kind]
right = self._unary()
node = BinOp(op, node, right)
return node
def _unary(self):
if self._peek().kind == 'MINUS':
self._consume()
return Unary('-', self._unary())
return self._primary()
def _primary(self):
tok = self._peek()
if tok.kind == 'NUMBER':
self._consume()
return Num(tok.value)
if tok.kind == 'LPAREN':
self._consume()
node = self._expr()
if self._peek().kind != 'RPAREN':
raise ParseError("unclosed parenthesis, expected ')'")
self._consume()
return node
if tok.kind == 'EOF':
raise ParseError("unexpected end of input")
raise ParseError(f"unexpected token {tok.kind!r}")
def parse(tokens: list):
"""Parse a token list into an AST.
Returns:
Num | BinOp | Unary — root node.
Raises:
ParseError: on any malformed input.
"""
return _Parser(tokens).parse()

View File

@ -0,0 +1,140 @@
"""Tests for calc.evaluator — covers eval/D1 through eval/D4 (CLI)."""
import pathlib
import subprocess
import sys
import unittest
from .lexer import tokenize
from .parser import parse
from .evaluator import evaluate, EvalError
CLI = str(pathlib.Path(__file__).parent.parent / "calc.py")
def ev(src):
return evaluate(parse(tokenize(src)))
class TestArithmetic(unittest.TestCase):
"""D1 — correct arithmetic with precedence, parens, unary minus."""
def test_add_mul_precedence(self):
self.assertEqual(ev("2+3*4"), 14)
def test_paren_overrides(self):
self.assertEqual(ev("(2+3)*4"), 20)
def test_left_associative_sub(self):
self.assertEqual(ev("8-3-2"), 3)
def test_unary_leading(self):
self.assertEqual(ev("-2+5"), 3)
def test_unary_after_mul(self):
self.assertEqual(ev("2*-3"), -6)
def test_simple_add(self):
self.assertEqual(ev("1+2"), 3)
def test_simple_sub(self):
self.assertEqual(ev("5-3"), 2)
def test_double_unary(self):
self.assertEqual(ev("--5"), 5)
def test_nested_parens(self):
self.assertEqual(ev("((3+2))*4"), 20)
class TestDivision(unittest.TestCase):
"""D2 — true division and EvalError on division by zero."""
def test_true_division(self):
self.assertEqual(ev("7/2"), 3.5)
def test_division_by_zero(self):
with self.assertRaises(EvalError):
ev("1/0")
def test_division_by_zero_expr(self):
with self.assertRaises(EvalError):
ev("5/(3-3)")
def test_not_bare_zerodiv(self):
try:
ev("1/0")
except EvalError:
pass
except ZeroDivisionError:
self.fail("bare ZeroDivisionError escaped; expected EvalError")
class TestResultType(unittest.TestCase):
"""D3 — whole-valued results are int; non-whole are float."""
def test_whole_division_is_int(self):
result = ev("4/2")
self.assertEqual(result, 2)
self.assertIsInstance(result, int)
def test_non_whole_division_is_float(self):
result = ev("7/2")
self.assertEqual(result, 3.5)
self.assertIsInstance(result, float)
def test_integer_arithmetic_stays_int(self):
result = ev("2+3*4")
self.assertIsInstance(result, int)
def test_whole_float_literal(self):
result = ev("4.0/2")
self.assertIsInstance(result, int)
self.assertEqual(result, 2)
class TestCLI(unittest.TestCase):
"""D4 — CLI prints correct output, exits correctly, errors to stderr."""
def _run(self, expr):
return subprocess.run(
[sys.executable, CLI, expr],
capture_output=True, text=True,
)
def test_precedence(self):
r = self._run("2+3*4")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "14")
def test_parens(self):
r = self._run("(2+3)*4")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "20")
def test_true_division(self):
r = self._run("7/2")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "3.5")
def test_whole_division(self):
r = self._run("4/2")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "2")
def test_invalid_expr_exits_nonzero(self):
r = self._run("1 +")
self.assertNotEqual(r.returncode, 0)
self.assertTrue(r.stderr.strip(), "expected error on stderr")
def test_div_by_zero_exits_nonzero(self):
r = self._run("1/0")
self.assertNotEqual(r.returncode, 0)
self.assertTrue(r.stderr.strip(), "expected error on stderr")
def test_no_traceback_on_error(self):
r = self._run("1 +")
self.assertNotIn("Traceback", r.stderr)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,111 @@
"""Tests for calc/lexer.py — covers D1, D2, D3."""
import unittest
from calc.lexer import tokenize, Token, LexError
def kinds(src):
return [t.kind for t in tokenize(src)]
def vals(src):
return [(t.kind, t.value) for t in tokenize(src)]
class TestNumbers(unittest.TestCase):
"""D1 — integers and floats tokenize to NUMBER with numeric value."""
def test_integer(self):
toks = tokenize("42")
self.assertEqual(len(toks), 2)
self.assertEqual(toks[0].kind, 'NUMBER')
self.assertEqual(toks[0].value, 42)
self.assertIsInstance(toks[0].value, int)
self.assertEqual(toks[1].kind, 'EOF')
def test_float_standard(self):
toks = tokenize("3.14")
self.assertEqual(toks[0].kind, 'NUMBER')
self.assertAlmostEqual(toks[0].value, 3.14)
self.assertIsInstance(toks[0].value, float)
def test_float_leading_dot(self):
toks = tokenize(".5")
self.assertEqual(toks[0].kind, 'NUMBER')
self.assertAlmostEqual(toks[0].value, 0.5)
self.assertIsInstance(toks[0].value, float)
def test_float_trailing_dot(self):
toks = tokenize("10.")
self.assertEqual(toks[0].kind, 'NUMBER')
self.assertAlmostEqual(toks[0].value, 10.0)
self.assertIsInstance(toks[0].value, float)
class TestOperatorsAndParens(unittest.TestCase):
"""D2 — operators and parens produce correct kinds."""
def test_plus(self):
self.assertIn('PLUS', kinds('+'))
def test_minus(self):
self.assertIn('MINUS', kinds('-'))
def test_star(self):
self.assertIn('STAR', kinds('*'))
def test_slash(self):
self.assertIn('SLASH', kinds('/'))
def test_lparen(self):
self.assertIn('LPAREN', kinds('('))
def test_rparen(self):
self.assertIn('RPAREN', kinds(')'))
def test_expression_1plus2star3(self):
self.assertEqual(
kinds("1+2*3"),
['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'],
)
class TestWhitespaceAndErrors(unittest.TestCase):
"""D3 — whitespace skipped; invalid chars raise LexError."""
def test_whitespace_skipped(self):
self.assertEqual(
kinds(" 12 + 3 "),
['NUMBER', 'PLUS', 'NUMBER', 'EOF'],
)
def test_tab_skipped(self):
self.assertEqual(kinds("1\t+\t2"), ['NUMBER', 'PLUS', 'NUMBER', 'EOF'])
def test_complex_expression(self):
self.assertEqual(
kinds("3.5*(1-2)"),
['NUMBER', 'STAR', 'LPAREN', 'NUMBER', 'MINUS', 'NUMBER', 'RPAREN', 'EOF'],
)
def test_invalid_at_sign(self):
with self.assertRaises(LexError) as ctx:
tokenize("1 @ 2")
self.assertIn('@', str(ctx.exception))
def test_invalid_dollar(self):
with self.assertRaises(LexError):
tokenize("$")
def test_invalid_letter(self):
with self.assertRaises(LexError):
tokenize("x")
def test_error_includes_position(self):
with self.assertRaises(LexError) as ctx:
tokenize("1 @ 2")
# position 2 (0-indexed) is where '@' lives
self.assertIn('2', str(ctx.exception))
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,134 @@
"""Tests for calc.parser — covers D1 through D5."""
import unittest
from .lexer import tokenize
from .parser import parse, ParseError, Num, BinOp, Unary
def p(src):
return parse(tokenize(src))
class TestPrecedence(unittest.TestCase):
"""D1 — * and / bind tighter than + and -."""
def test_add_mul(self):
# 1+2*3 → BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))
self.assertEqual(p("1+2*3"), BinOp('+', Num(1), BinOp('*', Num(2), Num(3))))
def test_mul_add(self):
# 2*3+1 → BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))
self.assertEqual(p("2*3+1"), BinOp('+', BinOp('*', Num(2), Num(3)), Num(1)))
def test_sub_div(self):
# 10-4/2 → BinOp('-', Num(10), BinOp('/', Num(4), Num(2)))
self.assertEqual(p("10-4/2"), BinOp('-', Num(10), BinOp('/', Num(4), Num(2))))
def test_single_number(self):
self.assertEqual(p("42"), Num(42))
def test_single_float(self):
self.assertEqual(p("3.5"), Num(3.5))
class TestLeftAssociativity(unittest.TestCase):
"""D2 — same-precedence operators associate left."""
def test_sub_left(self):
# 8-3-2 → BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))
self.assertEqual(p("8-3-2"), BinOp('-', BinOp('-', Num(8), Num(3)), Num(2)))
def test_div_left(self):
# 8/4/2 → BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))
self.assertEqual(p("8/4/2"), BinOp('/', BinOp('/', Num(8), Num(4)), Num(2)))
def test_add_left(self):
# 1+2+3 → BinOp('+', BinOp('+', Num(1), Num(2)), Num(3))
self.assertEqual(p("1+2+3"), BinOp('+', BinOp('+', Num(1), Num(2)), Num(3)))
def test_mul_left(self):
# 2*3*4 → BinOp('*', BinOp('*', Num(2), Num(3)), Num(4))
self.assertEqual(p("2*3*4"), BinOp('*', BinOp('*', Num(2), Num(3)), Num(4)))
class TestParentheses(unittest.TestCase):
"""D3 — parentheses override precedence."""
def test_paren_overrides_mul(self):
# (1+2)*3 → BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))
self.assertEqual(p("(1+2)*3"), BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)))
def test_paren_overrides_div(self):
# 6/(1+2) → BinOp('/', Num(6), BinOp('+', Num(1), Num(2)))
self.assertEqual(p("6/(1+2)"), BinOp('/', Num(6), BinOp('+', Num(1), Num(2))))
def test_nested_parens(self):
# ((2+3)) → BinOp('+', Num(2), Num(3))
self.assertEqual(p("((2+3))"), BinOp('+', Num(2), Num(3)))
def test_paren_single(self):
self.assertEqual(p("(5)"), Num(5))
class TestUnaryMinus(unittest.TestCase):
"""D4 — unary minus."""
def test_leading(self):
self.assertEqual(p("-5"), Unary('-', Num(5)))
def test_paren_group(self):
# -(1+2) → Unary('-', BinOp('+', Num(1), Num(2)))
self.assertEqual(p("-(1+2)"), Unary('-', BinOp('+', Num(1), Num(2))))
def test_after_mul(self):
# 3 * -2 → BinOp('*', Num(3), Unary('-', Num(2)))
self.assertEqual(p("3 * -2"), BinOp('*', Num(3), Unary('-', Num(2))))
def test_double_unary(self):
# --5 → Unary('-', Unary('-', Num(5)))
self.assertEqual(p("--5"), Unary('-', Unary('-', Num(5))))
def test_unary_in_add(self):
# 1 + -2 → BinOp('+', Num(1), Unary('-', Num(2)))
self.assertEqual(p("1 + -2"), BinOp('+', Num(1), Unary('-', Num(2))))
class TestErrors(unittest.TestCase):
"""D5 — malformed input raises ParseError."""
def test_trailing_op(self):
with self.assertRaises(ParseError):
p("1 +")
def test_unclosed_paren(self):
with self.assertRaises(ParseError):
p("(1")
def test_two_numbers(self):
with self.assertRaises(ParseError):
p("1 2")
def test_close_then_open(self):
with self.assertRaises(ParseError):
p(")(")
def test_empty(self):
with self.assertRaises(ParseError):
p("")
def test_only_op(self):
with self.assertRaises(ParseError):
p("+")
def test_mismatched_parens(self):
with self.assertRaises(ParseError):
p("(1+2")
def test_parse_error_not_other(self):
for src in ("1 +", "(1", "1 2", ")(", ""):
with self.subTest(src=src):
with self.assertRaises(ParseError):
p(src)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,12 @@
# BACKLOG-eval
## Build backlog
- [x] D1 arithmetic — `evaluate()` correct for +/-/*//, precedence, parens, unary minus
- [x] D2 division — true division; `EvalError` on zero; not bare `ZeroDivisionError`
- [x] D3 result type — whole floats → int; non-whole → float; rule documented in evaluator.py
- [x] D4 CLI — `calc.py` at work root; stdout+exit-0 on success; stderr+exit-1 on error; no traceback
- [x] D5 tests — 24 new tests in `calc/test_evaluator.py`; 68 total pass; prior suite unaffected
## Adversary findings
_(none yet)_

View File

@ -0,0 +1,21 @@
# BACKLOG-lex
## Build backlog
- [x] Create calc/ package with __init__.py
- [x] Implement calc/lexer.py (Token, LexError, tokenize)
- [x] Implement calc/test_lexer.py covering D1-D3
- [x] Claim D1 (committed in a0745d4)
- [x] Claim D2 (committed in a0745d4)
- [x] Claim D3 (committed in a0745d4)
- [x] Claim D4 (committed in a0745d4)
- [x] Await Adversary REVIEW-lex.md PASS for all gates
## Adversary findings
### AF-1 — Missing test file (2026-06-15T00:58Z) — RESOLVED
Initially observed calc/test_lexer.py missing from Builder's untracked files.
Builder subsequently committed and pushed the file. **Status: CLOSED** (resolved in commit f67144b)
### AF-2 — Gates not yet committed to origin (2026-06-15T00:58Z) — RESOLVED
Builder pushed all gates in commit a0745d4. **Status: CLOSED**

View File

@ -0,0 +1,9 @@
# BACKLOG-parse
## Build backlog
_This section is owned by the Builder. Adversary does not edit here._
## Adversary findings
_No findings yet._

View File

@ -0,0 +1,11 @@
# DECISIONS (append-only, shared)
## D-001: Token representation
Token is a dataclass with `kind: str` and `value` (int | float | None).
NUMBER tokens carry their parsed numeric value; operator/paren tokens carry None.
EOF carries None value.
Rationale: parser phases can pattern-match on kind and directly use value without re-parsing.
## D-002: Integer vs float distinction
If a number string contains '.' it is parsed as float, otherwise int.
Edge cases: '.5' → float(0.5), '10.' → float(10.0), '42' → int(42).

View File

@ -0,0 +1,47 @@
# JOURNAL-eval — Builder
## Build log
### Approach
AST walker in `calc/evaluator.py`:
- `Num` → return `_coerce(node.value)`
- `Unary('-', ...)``_coerce(-evaluate(operand))`
- `BinOp` → evaluate both sides; for `/`, check `right == 0` before dividing; apply `_coerce` to result
`_coerce(value)`: if `isinstance(value, float) and value == int(value)``int(value)`, else pass-through.
This keeps the API return clean (no `2.0` leaking out) and is applied consistently at every node evaluation site.
### Test run (local)
```
python -m unittest -v 2>&1
...
Ran 68 tests in 0.270s
OK
```
All 68 tests pass:
- 18 lexer tests (unchanged)
- 26 parser tests (unchanged)
- 24 evaluator + CLI tests (new)
### CLI spot-check
```
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 (stderr, exit 1)
python calc.py "1 +" → error: unexpected end of input (stderr, exit 1)
```
### D3 rule rationale
Python `/` always returns `float`. Applying `_coerce` at every evaluate site means:
- `4/2``2.0``int(2)` = `2`
- `7/2``3.5` (not whole → stays float)
- `2+3``5` (int arithmetic → already int, _coerce is a no-op)
This is documented in `calc/evaluator.py` module docstring.

View File

@ -0,0 +1,34 @@
# JOURNAL-lex
## Session 1
Starting implementation of calc/lexer.py per lex.md plan.
Design choices:
- Token is a dataclass with `kind: str` and `value` (str | int | float | None)
- NUMBER tokens carry numeric value (int for integers, float for floats)
- All other tokens carry None value
- LexError subclasses Exception, message includes offending char and position
## Implementation results
Ran tests:
```
$ python -m unittest -q
....................
Ran 18 tests in 0.000s
OK
```
Verification commands from plan:
```
$ python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
[('NUMBER', 3.5), ('STAR', None), ('LPAREN', None), ('NUMBER', 1), ('MINUS', None), ('NUMBER', 2), ('RPAREN', None), ('EOF', None)]
$ python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
Traceback (most recent call last):
...
calc.lexer.LexError: invalid character '@' at position 2
```
All DoD items satisfied. Claiming all gates D1-D4 together.

View File

@ -0,0 +1,73 @@
# JOURNAL-parse
## 2026-06-15 — Initial implementation
### Design choices
Grammar used:
```
expr = term (('+' | '-') term)*
term = unary (('*' | '/') unary)*
unary = '-' unary | primary
primary = NUMBER | '(' expr ')'
```
This naturally encodes precedence (* and / via term, + and - via expr) and left-associativity (via the while loop that builds left-deep trees in _expr and _term). Unary minus is right-recursive via _unary → _unary, which handles chaining (--5) correctly.
### Operator representation
The Adversary's pre-claim probes in REVIEW-parse.md used symbol format ('+', '-', '*', '/') rather than token kind names ('PLUS', 'MINUS', etc.). I aligned the implementation to use symbols to match their expected cold-verification output.
### Test run output
```
$ python -m unittest -q
Ran 44 tests in 0.001s
OK
```
### D1 shape verification
```
$ 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)))
```
### D2 shape verification
```
$ 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))
```
### D3 shape verification
```
$ 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))
```
### D4 shape verification
```
$ 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)))
```
### D5 error verification
All five required error cases raise ParseError specifically:
- "1 +" → ParseError: unexpected end of input
- "(1" → ParseError: unclosed parenthesis, expected ')'
- "1 2" → ParseError: unexpected token 'NUMBER'
- ")(" → ParseError: unexpected token 'RPAREN'
- "" → ParseError: empty expression

View File

@ -0,0 +1,65 @@
# REVIEW-eval — Adversary Verdicts
Phase: eval
Plan SSOT: /home/loops/project-orchestrator/projects/agent-orchestrator-benchmark/plans/calc/eval.md
## Gates
- D1 — arithmetic: PASS @2026-06-15T01:12:53Z
- D2 — division / EvalError: PASS @2026-06-15T01:12:53Z
- D3 — result type (no trailing .0): PASS @2026-06-15T01:12:53Z
- D4 — CLI: PASS @2026-06-15T01:12:53Z
- D5 — tests green + end-to-end: PASS @2026-06-15T01:12:53Z
## Verdicts
### D1 — arithmetic: PASS @2026-06-15T01:12:53Z
Cold-verified from work-adv clone (commit after pull: 070dc92).
Evidence (all outputs match expected):
- `python calc.py "2+3*4"``14` exit 0 ✓
- `python calc.py "(2+3)*4"``20` exit 0 ✓
- `python calc.py "8-3-2"``3` exit 0 ✓
- `python calc.py "-2+5"``3` exit 0 ✓
- `python calc.py "2*-3"``-6` exit 0 ✓
- `python calc.py "--5"``5` exit 0 ✓ (double unary)
- `python calc.py "3-3"``0` exit 0 ✓
### D2 — division / EvalError: PASS @2026-06-15T01:12:53Z
Evidence:
- `python calc.py "7/2"``3.5` exit 0 ✓ (true division)
- `1/0` raises `EvalError("division by zero")`, NOT bare `ZeroDivisionError`
- `5/(3-3)` also raises `EvalError`
### D3 — result type: PASS @2026-06-15T01:12:53Z
Evidence (types confirmed via Python `isinstance` check):
- `4/2``int(2)` (not `float(2.0)`) ✓
- `7/2``float(3.5)`
- `2+3*4``int(14)`
- `0.0/1``int(0)` (whole-float coercion works for zero) ✓
- `1.5+1.5``3` exit 0 (coerces 3.0 → int) ✓
- Rule documented in evaluator.py docstring ✓
### D4 — CLI: PASS @2026-06-15T01:12:53Z
Evidence:
- `python calc.py "2+3*4"` → stdout `14`, exit 0 ✓
- `python calc.py "1 +"` → stderr error, exit 1, no "Traceback" ✓
- `python calc.py "1/0"` → stderr error, exit 1, no "Traceback" ✓
- `python calc.py` (no args) → stderr usage msg, exit 1 ✓
- Error output confirmed routed to stderr (stdout suppressed, still exits 1) ✓
### D5 — tests green + end-to-end: PASS @2026-06-15T01:12:53Z
Evidence:
- `python -m unittest -q``Ran 68 tests in ...s` / `OK`
- Breakdown: 18 lex + 26 parse + 24 eval = 68 total ✓
- Prior 44 tests (lex + parse) still pass — no regression ✓
- `python -m unittest calc.test_lexer calc.test_parser -q` → 44 tests OK ✓
## Adversary findings
None. No defects found. No VETO.

View File

@ -0,0 +1,53 @@
# REVIEW-lex — Adversary Verdicts
## Gate Verdicts (cold-verified from work-adv clone, commit a0745d4)
### lex/D1: PASS @2026-06-15T01:00Z
Cold run from work-adv clone:
- `tokenize("42")``[NUMBER(42), EOF]`, value is `int(42)`
- `tokenize("3.14")``[NUMBER(3.14), EOF]`, value is `float`
- `tokenize(".5")``[NUMBER(0.5), EOF]`, value is `float`
- `tokenize("10.")``[NUMBER(10.0), EOF]`, value is `float`
### lex/D2: PASS @2026-06-15T01:00Z
Cold run:
- `tokenize("1+2*3")``['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']`
- All six operators/parens (`+`,`-`,`*`,`/`,`(`,`)`) produce correct kinds ✓
### lex/D3: PASS @2026-06-15T01:00Z
Cold run:
- `tokenize(" 12 + 3 ")``['NUMBER', 'PLUS', 'NUMBER', 'EOF']`
- `tokenize("1 @ 2")` → raises `LexError: invalid character '@' at position 2`
- Message contains offending char (`@`) and position (`2`) ✓
- Letters raise LexError ✓
### lex/D4: PASS @2026-06-15T01:00Z
```
$ python -m unittest -q
..................
Ran 18 tests in 0.000s
OK
```
18 tests, 0 failures, exit 0 ✓
Covers D1D3 including `" 12 + 3 "`, `"3.5*(1-2)"`, and `"1 @ 2"` raises LexError ✓
## Minor out-of-spec finding (does NOT affect DoD)
**Solo dot (`.`) raises `ValueError` instead of `LexError`.**
`tokenize(".")` crashes with `ValueError: could not convert string to float: '.'`
The plan specifies `.5` (dot + digit) as valid; bare `.` is undefined in the spec.
Not a DoD failure — filing as informational for future phases.
## Pre-claim probes (noted before Builder pushed)
Initially found test file missing from local untracked files; Builder then committed and pushed
the complete implementation. Both issues AF-1 and AF-2 from BACKLOG are now closed.
## Summary
All four gates D1D4 verified PASS from cold start in work-adv clone at commit a0745d4.

View File

@ -0,0 +1,73 @@
# REVIEW-parse — Adversary Verdicts
## Gate Verdicts (cold-verified from work-adv clone, commit 79016f1)
### parse/D1: PASS @2026-06-15T01:15Z
Cold run — precedence verified structurally:
- `parse(tokenize('1+2*3'))``BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))` ✓ (`*` deeper than `+`)
- `parse(tokenize('2*3+1'))``BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))`
- `parse(tokenize('10-4/2'))``BinOp('-', Num(10), BinOp('/', Num(4), Num(2)))`
- Extra: `1+2*3+4``BinOp('+', BinOp('+', Num(1), BinOp('*', Num(2), Num(3))), Num(4))`
### parse/D2: PASS @2026-06-15T01:15Z
Cold run — left associativity verified structurally:
- `parse(tokenize('8-3-2'))``BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))` ✓ (first `-` is left child)
- `parse(tokenize('8/4/2'))``BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))`
- Extra: `1+2+3``BinOp('+', BinOp('+', Num(1), Num(2)), Num(3))`
### parse/D3: PASS @2026-06-15T01:15Z
Cold run — parens override precedence:
- `parse(tokenize('(1+2)*3'))``BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))` ✓ (`+` deeper than `*`)
- Extra nested: `((((1))))``Num(1)`
- Extra mixed: `2*(3+4*5)``BinOp('*', Num(2), BinOp('+', Num(3), BinOp('*', Num(4), Num(5))))`
### parse/D4: PASS @2026-06-15T01:15Z
Cold run — 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)))`
- Extra: `--5``Unary('-', Unary('-', Num(5)))` ✓ (right-recursive unary)
- Extra: `-1+-2``BinOp('+', Unary('-', Num(1)), Unary('-', Num(2)))`
- Extra: `-3*2``BinOp('*', Unary('-', Num(3)), Num(2))` ✓ (unary higher-prec than `*`)
- Extra: `3--2``BinOp('-', Num(3), Unary('-', Num(2)))`
### parse/D5: PASS @2026-06-15T01:15Z
Cold run — all 5 plan-mandated cases raise `ParseError` (not any other exception type):
- `"1 +"``ParseError: unexpected end of input`
- `"(1"``ParseError: unclosed parenthesis, expected ')'`
- `"1 2"``ParseError: unexpected token 'NUMBER'`
- `")("``ParseError: unexpected token 'RPAREN'`
- `""``ParseError: empty expression`
Extra adversarial cases also raise `ParseError` correctly:
- `"*2"`, `"/2"`, `"()"`, `"1+"`, `"1/"` — all `ParseError`
### parse/D6: PASS @2026-06-15T01:15Z
```
$ python -m unittest -q
............................................
Ran 44 tests in 0.001s
OK
```
44 tests (18 lexer + 26 parser), 0 failures, exit 0 ✓
Tests assert structural equality using dataclass `__eq__` — not weak string comparison. Covers all D1-D5 cases including boundary and combinatorial inputs.
## Adversary Findings
No findings. Implementation is clean.
- `ParseError` is a proper subclass of `Exception` (not `SyntaxError` or other built-in) ✓
- AST nodes use operator symbols (`'+'`, `'-'`, etc.) not token kind names ✓
- Stable documented shape: `Num(value)`, `BinOp(op, left, right)`, `Unary(op, operand)`
## Summary
All six gates D1D6 verified PASS from cold start in work-adv clone at commit 79016f1.

View File

@ -0,0 +1,64 @@
# STATUS-eval — Builder
## DONE
All five eval gates D1D5 Adversary-verified PASS @2026-06-15T01:12:53Z (commit 070dc92). No findings, no VETO. This is the last phase — sequence complete.
---
## Gate: D1D5 CLAIMED (closed — all PASS)
Commit: see `git log --oneline -1` after push
### What is claimed
All five eval phase gates (D1D5):
- **D1** arithmetic — correct results for `+`, `-`, `*`, `/`, precedence, parens, unary minus
- **D2** division — true division; `EvalError` (not `ZeroDivisionError`) on divide-by-zero
- **D3** result type — whole-valued floats returned as `int`; non-whole as `float`
- **D4** CLI — `calc.py` prints result to stdout/exit-0 on success; error to stderr/exit-1 on failure; no traceback
- **D5** tests green — 68 tests pass (18 lex + 26 parse + 24 eval), 0 failures; CLI checks included
### How to verify (exact commands, run from work-adv clone root)
```bash
python -m unittest -q
```
Expected: `Ran 68 tests in ...s` / `OK` / exit 0
```bash
python calc.py "2+3*4"
```
Expected stdout: `14` / exit 0
```bash
python calc.py "(2+3)*4"
```
Expected stdout: `20` / exit 0
```bash
python calc.py "7/2"
```
Expected stdout: `3.5` / exit 0
```bash
python calc.py "4/2"
```
Expected stdout: `2` / exit 0
```bash
python calc.py "1/0"
```
Expected: error message on stderr / exit non-zero / no traceback
```bash
python calc.py "1 +"
```
Expected: error message on stderr / exit non-zero / no traceback
### Where
- `calc/evaluator.py``EvalError`, `evaluate(node) -> int | float`
- `calc/test_evaluator.py` — 24 new unittest tests covering D1D4
- `calc.py` — top-level CLI (work root)

View File

@ -0,0 +1,48 @@
# STATUS-lex
## DONE
All gates D1D4 verified PASS by Adversary at 2026-06-15T01:00Z (commit a0745d4).
## Gates
- D1 — numbers: PASS (Adversary verified)
- D2 — operators & parens: PASS (Adversary verified)
- D3 — whitespace & errors: PASS (Adversary verified)
- D4 — tests green: PASS (Adversary verified)
## Commit
SHA: f67144b
Files: calc/lexer.py, calc/test_lexer.py, calc/__init__.py
## Verification commands (re-run from a fresh clone)
```bash
# D4 — all tests pass
python -m unittest -q
# Expected: 18 tests, 0 failures, exit 0
# D2 — operator/paren sequence
python -c "from calc.lexer import tokenize; print([t.kind for t in tokenize('1+2*3')])"
# Expected: ['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']
# D1 + D2 + D3 combined
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', None), ('LPAREN', None), ('NUMBER', 1), ('MINUS', None), ('NUMBER', 2), ('RPAREN', None), ('EOF', None)]
# D3 — LexError on invalid char
python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
# Expected: raises calc.lexer.LexError: invalid character '@' at position 2
```
## What is claimed (DoD items)
- **D1**: Integers and floats → NUMBER token with numeric value (int or float).
- `tokenize("42")``[NUMBER(42), EOF]` (int)
- `tokenize("3.14")``[NUMBER(3.14), EOF]` (float)
- `tokenize(".5")``[NUMBER(0.5), EOF]` (float)
- `tokenize("10.")``[NUMBER(10.0), EOF]` (float)
- **D2**: `+ - * / ( )` tokenize to `PLUS MINUS STAR SLASH LPAREN RPAREN`; `tokenize("1+2*3")``NUMBER PLUS NUMBER STAR NUMBER EOF`.
- **D3**: Spaces/tabs skipped; invalid chars raise `LexError` with offending char and position in message.
- `tokenize(" 12 + 3 ")``NUMBER PLUS NUMBER EOF`
- `tokenize("1 @ 2")` raises `LexError: invalid character '@' at position 2`
- **D4**: `python -m unittest -q` → 18 tests, 0 failures.

View File

@ -0,0 +1,113 @@
# STATUS-parse
## DONE
All gates D1D6 verified PASS by Adversary at 2026-06-15T01:15Z (commit 79016f1).
## Gates
- D1 — precedence: PASS (Adversary verified)
- D2 — left associativity: PASS (Adversary verified)
- D3 — parentheses: PASS (Adversary verified)
- D4 — unary minus: PASS (Adversary verified)
- D5 — errors: PASS (Adversary verified)
- D6 — tests green: PASS (Adversary verified)
## Commit
SHA: 88df238
Files: calc/parser.py, calc/test_parser.py
## What is claimed (DoD items)
### AST node shapes
All nodes use operator symbols (not token kind names):
- `Num(value)` — numeric literal, value is int or float
- `BinOp(op, left, right)` — binary op; op ∈ {'+', '-', '*', '/'}
- `Unary(op, operand)` — unary op; op == '-'
### D1 — precedence
`*` and `/` bind tighter than `+` and `-`.
```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)))
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))
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('10-4/2')))"
# Expected: BinOp('-', Num(10), BinOp('/', Num(4), Num(2)))
```
### D2 — left associativity
Same-precedence operators associate left (left child is the deeper node).
```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
Parens override default 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))
# Note: '+' node is DEEPER (left child) of '*' — opposite of D1's case
```
### 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 (all must raise ParseError, not any other exception)
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
try: parse(tokenize('1 +'))
except ParseError: print('PASS')
except Exception as e: print('FAIL:', type(e).__name__, e)"
# Expected: PASS
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
try: parse(tokenize('(1'))
except ParseError: print('PASS')
except Exception as e: print('FAIL:', type(e).__name__, e)"
# Expected: PASS
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
try: parse(tokenize('1 2'))
except ParseError: print('PASS')
except Exception as e: print('FAIL:', type(e).__name__, e)"
# Expected: PASS
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
try: parse(tokenize(')('))
except ParseError: print('PASS')
except Exception as e: print('FAIL:', type(e).__name__, e)"
# Expected: PASS
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
try: parse(tokenize(''))
except ParseError: print('PASS')
except Exception as e: print('FAIL:', type(e).__name__, e)"
# Expected: PASS
```
### D6 — tests green
```bash
python -m unittest -q
# Expected: Ran 44 tests in ...s\n\nOK (18 lexer + 26 parser)
```