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,13 @@
# git history (claim/review handshake), from the run's shared bare repo
f829db5 status(review): ## DONE — Adversary comprehensive PASS received
a7dbf70 review(D-all): PASS — FINDING-1 resolved, full calculator verified (60 tests OK)
1cb5f43 claim(FINDING-1): fix float-literal normalization — extract _normalize() helper
8683a5a review(D-all): FAIL — eval/D3 float literal not normalized to int (FINDING-1)
d0e0373 claim(D-all): full calculator complete — ready for Adversary cold-verification
d2cf35f review(all): Adversary setup — tracking files created, awaiting Builder eval phase
48e0a93 fix(parse): resolve merge conflicts in machine-docs — parse phase complete
a6fc8ff review(eval): Adversary setup — tracking files created, awaiting Builder
b043ce1 review(parse): Adversary setup — tracking files created, awaiting Builder
592e168 chore: add .gitignore for pycache
a82e2ea feat(lex): implement lexer with tokenize(), Token, LexError + full test suite
3562754 chore: seed

View File

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

View File

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

View File

@ -0,0 +1,22 @@
"""calc.py — command-line calculator: string → tokens → AST → number."""
import sys
from calc.lexer import tokenize, LexError
from calc.parser import parse, ParseError
from calc.evaluator import evaluate, EvalError
def main() -> None:
if len(sys.argv) != 2:
print("usage: calc.py <expression>", file=sys.stderr)
sys.exit(1)
expr = sys.argv[1]
try:
result = evaluate(parse(tokenize(expr)))
except (LexError, ParseError, EvalError) as e:
print(f"error: {e}", file=sys.stderr)
sys.exit(1)
print(result)
if __name__ == "__main__":
main()

View File

@ -0,0 +1,44 @@
"""Evaluator: walks an AST (from calc.parser) and returns int | float.
Result-type rule: if the result is whole-valued (no fractional part), return int;
otherwise return float. This means 4/2 → 2 (int) and 7/2 → 3.5 (float).
"""
from __future__ import annotations
from calc.parser import Num, BinOp, Unary, Node
class EvalError(Exception):
pass
def _normalize(v: int | float) -> int | float:
if isinstance(v, float) and v == int(v):
return int(v)
return v
def evaluate(node: Node) -> int | float:
if isinstance(node, Num):
return _normalize(node.value)
if isinstance(node, Unary):
val = evaluate(node.operand)
if node.op == '-':
return _normalize(-val)
raise EvalError(f"unknown unary operator: {node.op!r}")
if isinstance(node, BinOp):
left = evaluate(node.left)
right = evaluate(node.right)
if node.op == '+':
result = left + right
elif node.op == '-':
result = left - right
elif node.op == '*':
result = left * right
elif node.op == '/':
if right == 0:
raise EvalError("division by zero")
result = left / right
else:
raise EvalError(f"unknown operator: {node.op!r}")
return _normalize(result)
raise EvalError(f"unknown node type: {type(node).__name__}")

View File

@ -0,0 +1,53 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Union
class LexError(Exception):
pass
@dataclass
class Token:
kind: str
value: Union[int, float, str, None]
def __repr__(self) -> str:
return f"{self.kind}({self.value!r})"
_SINGLE = {
'+': 'PLUS',
'-': 'MINUS',
'*': 'STAR',
'/': 'SLASH',
'(': 'LPAREN',
')': 'RPAREN',
}
def tokenize(src: str) -> list[Token]:
tokens: list[Token] = []
i = 0
n = len(src)
while i < n:
ch = src[i]
if ch in ' \t\n\r':
i += 1
continue
if ch in _SINGLE:
tokens.append(Token(_SINGLE[ch], ch))
i += 1
continue
if ch.isdigit() or ch == '.':
j = i
while j < n and (src[j].isdigit() or src[j] == '.'):
j += 1
raw = src[i:j]
value: Union[int, float] = float(raw) if '.' in raw else int(raw)
tokens.append(Token('NUMBER', value))
i = j
continue
raise LexError(f"unexpected character {ch!r} at position {i}")
tokens.append(Token('EOF', None))
return tokens

View File

@ -0,0 +1,120 @@
"""Recursive-descent parser for arithmetic expressions.
AST node shapes:
Num(value) — a numeric literal; value is int or float
BinOp(op, left, right) — binary op; op is one of '+', '-', '*', '/'
Unary(op, operand) — unary minus; op is '-'
Grammar (precedence encoded by structure):
expr = term ( ('+' | '-') term )*
term = unary ( ('*' | '/') unary )*
unary = '-' unary | primary
primary= NUMBER | '(' expr ')'
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import List, Union
from calc.lexer import Token
class ParseError(Exception):
pass
@dataclass
class Num:
value: Union[int, float]
def __repr__(self) -> str:
return f"Num({self.value!r})"
@dataclass
class BinOp:
op: str
left: "Node"
right: "Node"
def __repr__(self) -> str:
return f"BinOp({self.op!r}, {self.left!r}, {self.right!r})"
@dataclass
class Unary:
op: str
operand: "Node"
def __repr__(self) -> str:
return f"Unary({self.op!r}, {self.operand!r})"
Node = Union[Num, BinOp, Unary]
class _Parser:
def __init__(self, tokens: List[Token]) -> None:
self._tokens = tokens
self._pos = 0
def _peek(self) -> Token:
return self._tokens[self._pos]
def _advance(self) -> Token:
tok = self._tokens[self._pos]
self._pos += 1
return tok
def _expect(self, kind: str) -> Token:
tok = self._peek()
if tok.kind != kind:
raise ParseError(f"expected {kind}, got {tok.kind!r} ({tok.value!r})")
return self._advance()
def parse(self) -> Node:
if self._peek().kind == "EOF":
raise ParseError("empty input")
node = self._expr()
if self._peek().kind != "EOF":
tok = self._peek()
raise ParseError(f"unexpected token {tok.kind!r} ({tok.value!r})")
return node
def _expr(self) -> Node:
node = self._term()
while self._peek().kind in ("PLUS", "MINUS"):
op = self._advance().value
right = self._term()
node = BinOp(op, node, right)
return node
def _term(self) -> Node:
node = self._unary()
while self._peek().kind in ("STAR", "SLASH"):
op = self._advance().value
right = self._unary()
node = BinOp(op, node, right)
return node
def _unary(self) -> Node:
if self._peek().kind == "MINUS":
op = self._advance().value
operand = self._unary()
return Unary(op, operand)
return self._primary()
def _primary(self) -> Node:
tok = self._peek()
if tok.kind == "NUMBER":
self._advance()
return Num(tok.value)
if tok.kind == "LPAREN":
self._advance()
node = self._expr()
self._expect("RPAREN")
return node
raise ParseError(f"unexpected token {tok.kind!r} ({tok.value!r})")
def parse(tokens: List[Token]) -> Node:
"""Parse a token list (from lexer.tokenize) into an AST."""
return _Parser(tokens).parse()

View File

@ -0,0 +1,152 @@
"""Tests for calc.evaluator (D1D3) and CLI (D4)."""
import subprocess
import sys
import unittest
from calc.lexer import tokenize
from calc.parser import parse
from calc.evaluator import EvalError, evaluate
def _eval(expr: str) -> int | float:
return evaluate(parse(tokenize(expr)))
class TestArithmetic(unittest.TestCase):
"""D1 — arithmetic, precedence, parens, unary minus."""
def test_addition(self):
self.assertEqual(_eval("2+3"), 5)
def test_subtraction(self):
self.assertEqual(_eval("10-4"), 6)
def test_multiplication(self):
self.assertEqual(_eval("3*4"), 12)
def test_precedence_mul_over_add(self):
self.assertEqual(_eval("2+3*4"), 14)
def test_precedence_parens(self):
self.assertEqual(_eval("(2+3)*4"), 20)
def test_left_assoc_sub(self):
self.assertEqual(_eval("8-3-2"), 3)
def test_unary_minus_leading(self):
self.assertEqual(_eval("-2+5"), 3)
def test_unary_minus_after_op(self):
self.assertEqual(_eval("2*-3"), -6)
class TestDivision(unittest.TestCase):
"""D2 — true division and EvalError on zero."""
def test_true_division(self):
self.assertEqual(_eval("7/2"), 3.5)
def test_division_by_zero(self):
with self.assertRaises(EvalError):
_eval("5/0")
def test_division_by_zero_not_bare(self):
"""EvalError, not ZeroDivisionError."""
try:
_eval("1/0")
self.fail("expected EvalError")
except EvalError:
pass
except ZeroDivisionError:
self.fail("bare ZeroDivisionError escaped")
class TestResultType(unittest.TestCase):
"""D3 — result type: whole-valued → int, non-whole → float."""
def test_whole_division_returns_int(self):
result = _eval("4/2")
self.assertEqual(result, 2)
self.assertIsInstance(result, int)
def test_non_whole_division_returns_float(self):
result = _eval("7/2")
self.assertEqual(result, 3.5)
self.assertIsInstance(result, float)
def test_integer_arithmetic_returns_int(self):
result = _eval("2+3*4")
self.assertEqual(result, 14)
self.assertIsInstance(result, int)
def test_print_whole_no_dot_zero(self):
self.assertEqual(str(_eval("4/2")), "2")
def test_print_non_whole_has_decimal(self):
self.assertEqual(str(_eval("7/2")), "3.5")
def test_float_literal_whole_normalizes_to_int(self):
result = _eval("4.0")
self.assertEqual(result, 4)
self.assertIsInstance(result, int)
def test_float_literal_trailing_dot_normalizes(self):
result = _eval("10.")
self.assertEqual(result, 10)
self.assertIsInstance(result, int)
def test_float_literal_zero_normalizes(self):
result = _eval("0.0")
self.assertEqual(result, 0)
self.assertIsInstance(result, int)
def test_unary_minus_float_normalizes(self):
result = _eval("-4.0")
self.assertEqual(result, -4)
self.assertIsInstance(result, int)
class TestCLI(unittest.TestCase):
"""D4 — CLI behaviour."""
def _run(self, expr: str):
return subprocess.run(
[sys.executable, "calc.py", expr],
capture_output=True, text=True
)
def test_cli_basic(self):
r = self._run("2+3*4")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "14")
def test_cli_parens(self):
r = self._run("(2+3)*4")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "20")
def test_cli_float_result(self):
r = self._run("7/2")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "3.5")
def test_cli_whole_division(self):
r = self._run("4/2")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "2")
def test_cli_divide_by_zero_nonzero_exit(self):
r = self._run("1/0")
self.assertNotEqual(r.returncode, 0)
self.assertGreater(len(r.stderr), 0)
self.assertEqual(r.stdout, "")
def test_cli_invalid_expr_nonzero_exit(self):
r = self._run("1 +")
self.assertNotEqual(r.returncode, 0)
self.assertGreater(len(r.stderr), 0)
self.assertEqual(r.stdout, "")
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,90 @@
import unittest
from calc.lexer import tokenize, Token, LexError
def kinds(src):
return [t.kind for t in tokenize(src)]
def pairs(src):
return [(t.kind, t.value) for t in tokenize(src)]
class TestNumbers(unittest.TestCase):
def test_integer(self):
toks = tokenize("42")
self.assertEqual(toks, [Token('NUMBER', 42), Token('EOF', None)])
self.assertIsInstance(toks[0].value, int)
def test_float(self):
toks = tokenize("3.14")
self.assertEqual(toks[0], Token('NUMBER', 3.14))
self.assertIsInstance(toks[0].value, float)
def test_leading_dot(self):
toks = tokenize(".5")
self.assertAlmostEqual(toks[0].value, 0.5)
def test_trailing_dot(self):
toks = tokenize("10.")
self.assertEqual(toks[0].value, 10.0)
self.assertIsInstance(toks[0].value, float)
class TestOperatorsAndParens(unittest.TestCase):
def test_all_operators(self):
self.assertEqual(kinds("+"), ['PLUS', 'EOF'])
self.assertEqual(kinds("-"), ['MINUS', 'EOF'])
self.assertEqual(kinds("*"), ['STAR', 'EOF'])
self.assertEqual(kinds("/"), ['SLASH', 'EOF'])
self.assertEqual(kinds("("), ['LPAREN', 'EOF'])
self.assertEqual(kinds(")"), ['RPAREN', 'EOF'])
def test_expression_1_plus_2_star_3(self):
self.assertEqual(kinds("1+2*3"),
['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'])
def test_expression_3_5_times_paren(self):
self.assertEqual(kinds("3.5*(1-2)"),
['NUMBER', 'STAR', 'LPAREN', 'NUMBER', 'MINUS', 'NUMBER', 'RPAREN', 'EOF'])
class TestWhitespaceAndErrors(unittest.TestCase):
def test_whitespace_between_tokens(self):
toks = tokenize(" 12 + 3 ")
self.assertEqual([(t.kind, t.value) for t in toks],
[('NUMBER', 12), ('PLUS', '+'), ('NUMBER', 3), ('EOF', None)])
def test_tabs_skipped(self):
self.assertEqual(kinds("1\t+\t2"), ['NUMBER', 'PLUS', 'NUMBER', 'EOF'])
def test_invalid_at_raises(self):
with self.assertRaises(LexError) as ctx:
tokenize("1 @ 2")
self.assertIn('@', str(ctx.exception))
def test_invalid_dollar_raises(self):
with self.assertRaises(LexError):
tokenize("$")
def test_invalid_letter_raises(self):
with self.assertRaises(LexError):
tokenize("x")
def test_error_position_reported(self):
with self.assertRaises(LexError) as ctx:
tokenize("1 @ 2")
self.assertIn('2', str(ctx.exception)) # position 2
def test_complex_expression(self):
toks = tokenize("3.5*(1-2)")
expected = [
('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('),
('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2),
('RPAREN', ')'), ('EOF', None),
]
self.assertEqual([(t.kind, t.value) for t in toks], expected)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,125 @@
"""Tests for calc.parser — assert on tree structure, not evaluation."""
import unittest
from calc.lexer import tokenize
from calc.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)))
tree = p("1+2*3")
self.assertEqual(tree, BinOp('+', Num(1), BinOp('*', Num(2), Num(3))))
def test_mul_add(self):
# 2*3+4 => BinOp('+', BinOp('*', Num(2), Num(3)), Num(4))
tree = p("2*3+4")
self.assertEqual(tree, BinOp('+', BinOp('*', Num(2), Num(3)), Num(4)))
def test_sub_div(self):
# 10-6/2 => BinOp('-', Num(10), BinOp('/', Num(6), Num(2)))
tree = p("10-6/2")
self.assertEqual(tree, BinOp('-', Num(10), BinOp('/', Num(6), Num(2))))
class TestLeftAssociativity(unittest.TestCase):
"""D2 — same-precedence ops associate left."""
def test_sub_assoc(self):
# 8-3-2 => BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))
tree = p("8-3-2")
self.assertEqual(tree, BinOp('-', BinOp('-', Num(8), Num(3)), Num(2)))
def test_div_assoc(self):
# 8/4/2 => BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))
tree = p("8/4/2")
self.assertEqual(tree, BinOp('/', BinOp('/', Num(8), Num(4)), Num(2)))
def test_add_assoc(self):
# 1+2+3 => BinOp('+', BinOp('+', Num(1), Num(2)), Num(3))
tree = p("1+2+3")
self.assertEqual(tree, BinOp('+', BinOp('+', Num(1), Num(2)), Num(3)))
def test_mul_assoc(self):
# 2*3*4 => BinOp('*', BinOp('*', Num(2), Num(3)), Num(4))
tree = p("2*3*4")
self.assertEqual(tree, BinOp('*', BinOp('*', Num(2), Num(3)), Num(4)))
class TestParentheses(unittest.TestCase):
"""D3 — parens override precedence."""
def test_paren_add_mul(self):
# (1+2)*3 => BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))
tree = p("(1+2)*3")
self.assertEqual(tree, BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)))
def test_nested_parens(self):
# ((2+3)) => BinOp('+', Num(2), Num(3)) -- outer parens just unwrap
tree = p("((2+3))")
self.assertEqual(tree, BinOp('+', Num(2), Num(3)))
def test_paren_single_num(self):
tree = p("(42)")
self.assertEqual(tree, Num(42))
class TestUnaryMinus(unittest.TestCase):
"""D4 — leading and nested unary minus."""
def test_unary_simple(self):
# -5 => Unary('-', Num(5))
tree = p("-5")
self.assertEqual(tree, Unary('-', Num(5)))
def test_unary_paren(self):
# -(1+2) => Unary('-', BinOp('+', Num(1), Num(2)))
tree = p("-(1+2)")
self.assertEqual(tree, Unary('-', BinOp('+', Num(1), Num(2))))
def test_mul_unary(self):
# 3 * -2 => BinOp('*', Num(3), Unary('-', Num(2)))
tree = p("3 * -2")
self.assertEqual(tree, BinOp('*', Num(3), Unary('-', Num(2))))
def test_double_unary(self):
# --5 => Unary('-', Unary('-', Num(5)))
tree = p("--5")
self.assertEqual(tree, Unary('-', Unary('-', Num(5))))
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_string(self):
with self.assertRaises(ParseError):
p("")
def test_only_paren(self):
with self.assertRaises(ParseError):
p("()")
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,7 @@
# BACKLOG — eval phase
## Build backlog
(Builder-owned — read-only to Adversary)
## Adversary findings
(None yet — awaiting Builder completion before comprehensive verification)

View File

@ -0,0 +1,10 @@
# Backlog — lex phase
## Build backlog
- [x] D1: integer/float tokenization
- [x] D2: operator and paren tokenization
- [x] D3: whitespace skip + LexError for invalid chars
- [x] D4: unittest suite green (14 tests, 0 failures)
All items complete.

View File

@ -0,0 +1,16 @@
# Backlog — parse phase
## Build backlog
All items complete.
- [x] D1 — precedence: `*`/`/` bind tighter than `+`/`-`
- [x] D2 — left associativity for same-precedence ops
- [x] D3 — parentheses override precedence
- [x] D4 — unary minus (leading, nested, after operator)
- [x] D5 — ParseError on malformed input (5 cases)
- [x] D6 — tests green (34 total, 0 failures)
## Adversary findings
(None yet — awaiting review phase)

View File

@ -0,0 +1,28 @@
# BACKLOG — review phase
## Build backlog
(Builder-owned — read-only to Adversary)
## Adversary findings
### FINDING-1 — float literal not normalized to int [OPEN]
**Filed:** 2026-06-16T00:54:18Z
**Phase:** eval/D3 (result type consistency)
**Repro:**
```bash
python calc.py "4.0" # prints 4.0 — EXPECTED: 4
python calc.py "10." # prints 10.0 — EXPECTED: 10
python calc.py "-4.0" # prints -4.0 — EXPECTED: -4
```
**Root cause:** `calc/evaluator.py` `evaluate()` applies `float→int` normalization only in the
`BinOp` branch (line 37-38). `Num` and `Unary` branches return the raw float.
**Fix needed:** Apply normalization consistently across all return paths in `evaluate()`.
Suggest a `_normalize(v)` helper applied before every return.
**Also add:** Tests for `_eval("4.0")`, `_eval("10.")`, `_eval("-4.0")`, `_eval("0.0")` to
lock in consistent behavior.
Status: CLOSED @ 2026-06-16T00:57:12Z — re-verified PASS after Builder fix.

View File

@ -0,0 +1,7 @@
# Decisions (append-only)
## lex phase
**Token.value type for operators:** stored as the literal character string (e.g. `'+'`). Considered `None` but the literal char is more useful for error messages in later phases.
**Number parsing:** greedy scan of `[0-9.]` then classify by presence of `.`. A string like `1.2.3` would tokenize as one malformed number token — acceptable for a phase-1 lexer; the evaluator/parser will catch semantic errors.

View File

@ -0,0 +1,8 @@
# JOURNAL — eval phase (Adversary)
## 2026-06-16T00:43:36Z — Phase kickoff
- Phase plan read: eval.md (evaluator + CLI, gates D1D5)
- Current state: Builder has only completed lexer (calc/lexer.py + test_lexer.py)
- Parser and evaluator not yet implemented
- Created eval phase tracking files: STATUS, REVIEW, BACKLOG, JOURNAL
- Entering wait loop per REVIEW CADENCE (defer to comprehensive single verification)

View File

@ -0,0 +1,37 @@
# Journal — lex phase
## Build run
Implemented `calc/lexer.py` with:
- `Token` dataclass with `kind` (str) and `value` (int | float | str | None)
- `LexError(Exception)` for invalid characters
- `tokenize(src: str) -> list[Token]` scanning char-by-char
Design choices:
- `Token` is a plain dataclass so later phases (parser, evaluator) can pattern-match on `.kind`
- Numbers: scanned greedily while char is digit or `.`; cast to `int` if no `.` in raw string, else `float`
- Operators stored as their literal char as `value` (handy for error messages)
- EOF always appended as final token (parser-friendly sentinel)
## Test run output
```
$ python -m unittest -q
..............
----------------------------------------------------------------------
Ran 14 tests in 0.000s
OK
```
## Verify commands output
```
$ 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,44 @@
# Journal — parse phase
## Adversary initial setup (2026-06-16)
- Pulled origin/main: lex phase is complete (STATUS-lex.md: ## DONE)
- Lex phase early verification passed: 14 tests, OK
- Parse phase not yet started by Builder at that point
- Per REVIEW CADENCE rules: will wait for Builder to complete parse, then do ONE
comprehensive cold-verification of all DoD items.
## Builder implementation run
### Grammar design
Used standard two-level precedence grammar:
- `_expr`: handles `+` and `-` (lower precedence)
- `_term`: handles `*` and `/` (higher precedence)
- `_unary`: handles unary `-` (right-recursive)
- `_primary`: handles `NUMBER` and `(expr)`
Both `_expr` and `_term` use iterative while-loops to achieve left associativity naturally.
### Verified outputs
```
$ python -m unittest -q
Ran 34 tests in 0.001s
OK
$ 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 +'))"
Traceback (most recent call last):
...
calc.parser.ParseError: unexpected token 'EOF' (None)
```
### Test count
- 3 precedence tests (D1)
- 4 associativity tests (D2)
- 3 parentheses tests (D3)
- 4 unary minus tests (D4)
- 6 error tests (D5)
= 20 parser tests + 14 lex tests = 34 total

View File

@ -0,0 +1,48 @@
# JOURNAL — review phase (Adversary)
## 2026-06-16T00:47:02Z — Phase kickoff
- Phase plan read: review.md (comprehensive deferred verification)
- Current state: Builder has only completed lex and parse phases
- calc/lexer.py + calc/test_lexer.py (lex phase)
- calc/parser.py + calc/test_parser.py (parse phase)
- eval phase NOT yet complete — no evaluator or CLI in calc/
- Created review phase tracking files: STATUS-review.md, REVIEW-review.md, BACKLOG-review.md, JOURNAL-review.md
- Entering wait loop per REVIEW CADENCE (defer to comprehensive single verification)
- Will wake every ~10 min to check if Builder has completed eval phase
## 2026-06-16T00:54:18Z — Comprehensive cold-verification complete
Builder claimed D-all at commit d0e0373. Pulled and ran full verification.
Results summary:
- lex all DoD: PASS
- parse all DoD: PASS
- eval/D1,D2,D4,D5: PASS
- eval/D3 (result type): FAIL — FINDING-1 filed
- 56 tests: PASS
- D3 cross-feature review.md probes: PASS except float literal normalization
FINDING-1: float literals (e.g. `4.0`, `10.`, `-4.0`) not normalized to int.
Root cause: normalization in BinOp branch only (evaluator.py lines 37-38).
Num and Unary branches return raw float value without normalization.
Repro: `python calc.py "4.0"` prints `4.0` not `4`.
Fix: _normalize() helper applied to all return paths in evaluate().
Filed in REVIEW-review.md and BACKLOG-review.md. Pushing review(D-all): FAIL commit.
Awaiting Builder fix then re-verification.
## 2026-06-16T00:57:12Z — Re-verification after Builder fix (FINDING-1)
Builder committed fix at 1cb5f43: extracted _normalize() helper in evaluator.py,
applied to Num, Unary, and BinOp branches. 4 new tests added (60 total).
Re-verification results:
- python calc.py "4.0" → 4 ✓
- python calc.py "10." → 10 ✓
- python calc.py "-4.0" → -4 ✓
- python calc.py "0.0" → 0 ✓
- python -m unittest -q → Ran 60 tests OK ✓
- All original verification commands still pass ✓
FINDING-1 CLOSED. review(D-all): PASS committed and pushed.
Builder may now write ## DONE to STATUS-review.md.

View File

@ -0,0 +1,7 @@
# REVIEW — eval phase
Adversary cold-verification log. Per REVIEW CADENCE rules, comprehensive
verification happens ONCE after the Builder completes all gates.
## Status
PENDING — awaiting Builder completion of eval phase gates D1D5.

View File

@ -0,0 +1,16 @@
# Adversary Review — parse phase
REVIEW CADENCE: DEFERRED — comprehensive review happens ONCE after Builder completes,
not per-gate during build phases.
## Status: PENDING
Builder has not yet completed the parse phase. No verdicts issued yet.
## When triggered:
Will perform cold-verification of ALL DoD items (D1D6) from a fresh shell:
- D1: precedence (`1+2*3` tree structure)
- D2: left-associativity (`8-3-2` and `8/4/2` tree structures)
- D3: parentheses override (`(1+2)*3` tree structure)
- D4: unary minus (`-5`, `-(1+2)`, `3 * -2`)
- D5: error handling (`1 +`, `(1`, `1 2`, `)(`, empty string → ParseError)
- D6: `python -m unittest -q` passes with 0 failures

View File

@ -0,0 +1,66 @@
# REVIEW — review phase (Adversary verdicts)
## Status: PASS — comprehensive cold-verification complete
`review(D-all): PASS` @ 2026-06-16T00:57:12Z
---
## lex phase DoD — ALL PASS
- **lex/D1** PASS — `.5`→0.5, `10.`→10.0, `3.14`→3.14, `42`→42, all correct kinds/values
- **lex/D2** PASS — `+ - * / ( )` all produce correct kinds; `1+2*3``NUMBER PLUS NUMBER STAR NUMBER EOF`
- **lex/D3** PASS — whitespace skipped; `'1 @ 2'` raises `LexError: unexpected character '@' at position 2`
- **lex/D4** PASS — 14 tests, 0 failures (now part of 60-test suite)
## parse phase DoD — ALL PASS
- **parse/D1** PASS — `1+2*3``BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))`
- **parse/D2** PASS — `8-3-2``BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))`; `8/4/2``BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))`
- **parse/D3** PASS — `(1+2)*3``BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))`
- **parse/D4** PASS — `-5``Unary('-', Num(5))`; `-(1+2)` and `3*-2` correct ✓
- **parse/D5** PASS — `'1 +'`, `'(1'`, `'1 2'`, `')('`, `''` all raise `ParseError`
- **parse/D6** PASS — 20 tests, 0 failures
## eval phase DoD — ALL PASS
- **eval/D1** PASS — `2+3*4`→14, `(2+3)*4`→20, `8-3-2`→3, `-2+5`→3, `2*-3`→-6 ✓
- **eval/D2** PASS — `7/2`→3.5; `1/0` raises `EvalError`, not bare `ZeroDivisionError`
- **eval/D3** PASS (after fix) — `_normalize()` applied in all branches: `4.0`→4, `10.`→10, `-4.0`→-4, `0.0`→0, `4/2`→2, `7/2`→3.5 ✓
- **eval/D4** PASS — CLI prints result to stdout, exit 0; errors to stderr, exit 1, no traceback ✓
- **eval/D5** PASS — 60 tests, 0 failures (4 new tests for float-literal normalization added by Builder)
## review phase DoD — ALL PASS
- **D1** PASS — every prior DoD item cold-verified from fresh clone ✓
- **D2** PASS — `python -m unittest -q``Ran 60 tests in ...s OK`
- **D3** PASS — cross-feature probes all pass:
- `-(-(1+2))` → 3 ✓
- `2+3*4-5/5` → 13 ✓
- `1 @ 2`, `1/0`, `(1+` all error to stderr, exit 1, no traceback ✓
- whitespace+floats+parens: `3.5*(1-2)` works ✓
- float-literal normalization: `4.0`→4, `10.`→10, `-4.0`→-4 ✓
- **D4** PASS — FINDING-1 fixed and re-verified; no standing VETO ✓
---
## FINDING-1 — CLOSED
**Status:** RESOLVED @ 2026-06-16T00:57:12Z
**Fix:** Builder extracted `_normalize()` helper in `calc/evaluator.py` and applied it before
every return in `evaluate()` (Num, Unary, and BinOp branches). 4 new tests added to
`calc/test_evaluator.py` to lock in behavior.
**Re-verification:**
```
python calc.py "4.0" → 4 ✓
python calc.py "10." → 10 ✓
python calc.py "-4.0" → -4 ✓
python calc.py "0.0" → 0 ✓
python -m unittest -q → Ran 60 tests in ...s OK ✓
```
---
**Builder may now write `## DONE` to `machine-docs/STATUS-review.md`.**

View File

@ -0,0 +1,96 @@
## DONE
Phase: eval — evaluator + CLI
All DoD items self-certified (BUILD phase — deferred Adversary review).
---
### Files created
- `calc/evaluator.py``EvalError`, `evaluate(node) -> int | float`
- `calc.py` — top-level CLI
- `calc/test_evaluator.py` — 22 new unittest tests covering D1D4
---
### D1 — arithmetic
WHAT: `evaluate(parse(tokenize(s)))` correct for `+`, `-`, `*`, `/`, precedence, parens, unary minus.
HOW:
```
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 (requires shell quoting awareness)
python calc.py "2*-3" # → -6
```
EXPECTED: outputs above
WHERE: calc/evaluator.py `evaluate()`
---
### D2 — division
WHAT: `/` is true division; division by zero raises `EvalError`.
HOW:
```
python calc.py "7/2" # → 3.5
python calc.py "1/0" # → stderr error, exit 1
```
EXPECTED: `3.5` for 7/2; non-zero exit + stderr for 1/0
WHERE: calc/evaluator.py BinOp `/` branch
---
### D3 — result type
WHAT: Whole-valued → int (no `.0`), non-whole → float.
HOW:
```
python calc.py "4/2" # → 2 (not 2.0)
python calc.py "7/2" # → 3.5
```
EXPECTED: `2` and `3.5`
WHERE: calc/evaluator.py — `if isinstance(result, float) and result == int(result): return int(result)`
---
### D4 — CLI
WHAT: `python calc.py "2+3*4"``14`, exit 0; error → stderr, non-zero exit, no traceback.
HOW:
```
python calc.py "2+3*4" # → 14, exit 0
python calc.py "1 +" # → error to stderr, exit 1
```
EXPECTED: as above
WHERE: calc.py `main()`
---
### D5 — tests green + end-to-end
WHAT: Full test suite (lex + parse + eval) passes; 0 failures.
HOW: `python -m unittest -q`
EXPECTED:
```
Ran 56 tests in 0.226s
OK
```
WHERE: calc/test_lexer.py (14) + calc/test_parser.py (20) + calc/test_evaluator.py (22)
---
### Verify commands (from eval.md, verbatim)
```bash
python -m unittest -q # Ran 56 tests in ...s OK
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 to stderr, non-zero exit
python calc.py "1 +" # error to stderr, non-zero exit
```
Commit: (see git log — latest commit on main)

View File

@ -0,0 +1,40 @@
## DONE
Phase: lex — tokenizer
All DoD items self-certified (BUILD phase — deferred Adversary review).
### D1 — numbers
WHAT: integers and floats tokenize to NUMBER with int/float value; EOF appended.
HOW: `python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('42')])"`
EXPECTED: `[('NUMBER', 42), ('EOF', None)]`
WHERE: calc/lexer.py
### D2 — operators & parens
WHAT: `+ - * / ( )` each produce correct kind token.
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
### D3 — whitespace & errors
WHAT: spaces/tabs skipped; invalid char raises LexError with char + position.
HOW: `python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"`
EXPECTED: raises `calc.lexer.LexError: unexpected character '@' at position 2`
WHERE: calc/lexer.py
### D4 — tests green
WHAT: 14 unittest tests in calc/test_lexer.py, 0 failures.
HOW: `python -m unittest -q`
EXPECTED: `Ran 14 tests in 0.000s\nOK`
WHERE: calc/test_lexer.py
### Plan verify commands (verbatim from lex.md)
```
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 14 tests in 0.000s\nOK`
2. `[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
3. `calc.lexer.LexError: unexpected character '@' at position 2`

View File

@ -0,0 +1,78 @@
## DONE
Phase: parse — recursive-descent parser
All DoD items self-certified (BUILD phase — deferred Adversary review).
---
### AST node shapes
```
Num(value) — numeric literal; value is int or float
BinOp(op, left, right) — binary op; op in {'+', '-', '*', '/'}
Unary(op, operand) — unary minus; op is '-'
```
Defined in `calc/parser.py`. `ParseError` is also defined there.
---
### D1 — precedence
WHAT: `*` and `/` bind tighter than `+` and `-`.
HOW: `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` / `_term` levels
---
### D2 — left associativity
WHAT: Same-precedence operators associate left.
HOW:
```
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))
```
WHERE: `_expr` / `_term` each use a while-loop (iterative, left-accumulating)
---
### D3 — parentheses
WHAT: Parens override precedence.
HOW:
```
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: `_primary` handles LPAREN → `_expr` → RPAREN
---
### D4 — unary minus
WHAT: Leading and nested unary minus parses correctly.
HOW:
```
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5')))"
# EXPECTED: Unary('-', Num(5))
```
WHERE: `_unary` level in parser
---
### D5 — errors
WHAT: Malformed input raises `ParseError` (not any other exception).
HOW: `parse(tokenize('1 +'))` raises `calc.parser.ParseError`
WHERE: `_Parser.parse`, `_Parser._primary`, `_Parser._expect`
---
### D6 — tests green
WHAT: 34 tests total (14 lex + 20 parser), 0 failures.
HOW: `python -m unittest -q`
EXPECTED: `Ran 34 tests in 0.001s\nOK`
WHERE: calc/test_parser.py (20 new tests)

View File

@ -0,0 +1,27 @@
## DONE
Phase: review — comprehensive deferred cold-verification
Adversary comprehensive PASS received @ 2026-06-16T00:57:12Z (commit a7dbf70).
---
### Summary
All DoD items verified by Adversary cold-verification from a fresh clone:
- **D1** PASS — every prior phase DoD item (lex/D1D4, parse/D1D6, eval/D1D5) cold-verified
- **D2** PASS — `python -m unittest -q` → Ran 60 tests OK (0 failures)
- **D3** PASS — all cross-feature probes pass (nested unary+parens, precedence chains, error propagation, whitespace+floats+parens, CLI exit codes)
- **D4** PASS — FINDING-1 fixed and re-verified; no standing VETO
### Finding resolved
FINDING-1: float literals not normalized to int in Num/Unary branches.
Fix: extracted `_normalize()` helper in `calc/evaluator.py`, applied at every return site.
4 regression tests added to `calc/test_evaluator.py`.
### Final state
- 60 tests, 0 failures
- Full calculator: lexer → parser → evaluator → CLI
- Files: calc/lexer.py, calc/parser.py, calc/evaluator.py, calc.py + full test suites