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,15 @@
# git history (claim/review handshake), from the run's shared bare repo
90a171c status(eval): mark DONE — all D1-D5 Adversary-verified PASS
233ccfe review(D1,D2,D3,D4,D5): PASS — all gates verified cold, 66/66 tests, no defects found
47f3478 claim(D1,D2,D3,D4,D5): implement evaluator+CLI+tests, all gates claimed
14db736 feat(eval): implement evaluator, CLI, and test suite
66c55b4 review(eval): Adversary starting — monitoring for Builder gate claims
3b3ee90 status(parse): mark DONE — all D1-D6 Adversary-verified PASS
7eb69be review(D1,D2,D3,D4,D5,D6): PASS — all gates verified cold, 44/44 tests, no defects found
ab253ee claim(D1,D2,D3,D4,D5,D6): implement parser with full test suite
9202718 review(parse): Adversary starting — monitoring for Builder gate claims
7a13a51 status(lex): mark DONE — all D1-D4 Adversary-verified PASS
197f29d review(D1,D2,D3,D4): PASS — all gates verified cold, 21/21 tests, no defects found
93a9cc9 claim(D1,D2,D3,D4): implement lexer with full test suite
cd8d498 review(init): Adversary starting — monitoring for Builder gate claims
57b4566 chore: seed

View File

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

View File

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

View File

@ -0,0 +1,36 @@
"""Command-line interface: python calc.py "<expression>"
Result printing rule:
- If the result is a whole number (e.g. 2.0), print as int (e.g. "2").
- Otherwise print as float (e.g. "3.5").
"""
import sys
from calc.lexer import tokenize, LexError
from calc.parser import parse, ParseError
from calc.evaluator import evaluate, EvalError
def _format(value) -> str:
if isinstance(value, float) and value.is_integer():
return str(int(value))
return str(value)
def main():
if len(sys.argv) != 2:
print(f"usage: {sys.argv[0]} <expression>", file=sys.stderr)
sys.exit(1)
expr = sys.argv[1]
try:
tokens = tokenize(expr)
ast = parse(tokens)
result = evaluate(ast)
print(_format(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,34 @@
"""Evaluator: walks the AST produced by calc.parser and returns a numeric result."""
from __future__ import annotations
from .parser import Num, BinOp, Unary
class EvalError(Exception):
pass
def evaluate(node) -> int | float:
"""Recursively evaluate an AST node and return int or float."""
if isinstance(node, Num):
return node.value
if isinstance(node, Unary):
operand = evaluate(node.operand)
if node.op == "-":
return -operand
raise EvalError(f"unknown unary op: {node.op!r}")
if isinstance(node, BinOp):
left = evaluate(node.left)
right = evaluate(node.right)
if node.op == "+":
return left + right
if node.op == "-":
return left - right
if node.op == "*":
return left * right
if node.op == "/":
if right == 0:
raise EvalError("division by zero")
return left / right
raise EvalError(f"unknown binary op: {node.op!r}")
raise EvalError(f"unknown node type: {type(node).__name__!r}")

View File

@ -0,0 +1,58 @@
"""Lexer for arithmetic expressions."""
from __future__ import annotations
import re
class LexError(Exception):
pass
class Token:
__slots__ = ("kind", "value")
def __init__(self, kind: str, value):
self.kind = kind
self.value = value
def __repr__(self):
return f"Token({self.kind!r}, {self.value!r})"
def __eq__(self, other):
return isinstance(other, Token) and self.kind == other.kind and self.value == other.value
_NUMBER_RE = re.compile(r"\d+\.?\d*|\.\d+")
_SINGLE = {
"+": "PLUS",
"-": "MINUS",
"*": "STAR",
"/": "SLASH",
"(": "LPAREN",
")": "RPAREN",
}
def tokenize(src: str) -> list[Token]:
tokens: list[Token] = []
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], ch))
i += 1
continue
m = _NUMBER_RE.match(src, i)
if m:
raw = m.group()
value = float(raw) if "." in raw else int(raw)
tokens.append(Token("NUMBER", value))
i = m.end()
continue
raise LexError(f"unexpected character {ch!r} at position {i}")
tokens.append(Token("EOF", None))
return tokens

View File

@ -0,0 +1,141 @@
"""Recursive-descent parser for arithmetic expressions.
Grammar (precedence low → high):
expr = term ( ('+' | '-') term )*
term = unary ( ('*' | '/') unary )*
unary = '-' unary | primary
primary = NUMBER | '(' expr ')'
AST nodes (stable shape for the evaluator):
Num(value) numeric literal; value is int or float
BinOp(op, left, right) op is one of '+', '-', '*', '/'
Unary(op, operand) op is '-'
"""
from __future__ import annotations
from typing import List
from .lexer import Token
class ParseError(Exception):
pass
class Num:
__slots__ = ("value",)
def __init__(self, value):
self.value = value
def __repr__(self):
return f"Num({self.value!r})"
def __eq__(self, other):
return isinstance(other, Num) and self.value == other.value
class BinOp:
__slots__ = ("op", "left", "right")
def __init__(self, op: str, left, right):
self.op = op
self.left = left
self.right = right
def __repr__(self):
return f"BinOp({self.op!r}, {self.left!r}, {self.right!r})"
def __eq__(self, other):
return (
isinstance(other, BinOp)
and self.op == other.op
and self.left == other.left
and self.right == other.right
)
class Unary:
__slots__ = ("op", "operand")
def __init__(self, op: str, operand):
self.op = op
self.operand = operand
def __repr__(self):
return f"Unary({self.op!r}, {self.operand!r})"
def __eq__(self, other):
return (
isinstance(other, Unary)
and self.op == other.op
and self.operand == other.operand
)
class _Parser:
def __init__(self, tokens: List[Token]):
self._tokens = tokens
self._pos = 0
def _peek(self) -> Token:
return self._tokens[self._pos]
def _consume(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._consume()
def parse(self):
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 = self._term()
while self._peek().kind in ("PLUS", "MINUS"):
op = self._consume().value
right = self._term()
node = BinOp(op, node, right)
return node
def _term(self):
node = self._unary()
while self._peek().kind in ("STAR", "SLASH"):
op = self._consume().value
right = self._unary()
node = BinOp(op, node, right)
return node
def _unary(self):
if self._peek().kind == "MINUS":
op = self._consume().value
operand = self._unary()
return Unary(op, operand)
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()
self._expect("RPAREN")
return node
raise ParseError(f"unexpected token {tok.kind!r} ({tok.value!r})")
def parse(tokens: List[Token]):
"""Parse a token list produced by calc.lexer.tokenize and return an AST root node."""
return _Parser(tokens).parse()

View File

@ -0,0 +1,124 @@
"""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 evaluate, EvalError
def ev(s):
return evaluate(parse(tokenize(s)))
class D1Arithmetic(unittest.TestCase):
def test_add_mul_precedence(self):
self.assertEqual(ev("2+3*4"), 14)
def test_paren_override_precedence(self):
self.assertEqual(ev("(2+3)*4"), 20)
def test_left_associative_sub(self):
self.assertEqual(ev("8-3-2"), 3)
def test_unary_minus_add(self):
self.assertEqual(ev("-2+5"), 3)
def test_mul_unary_minus(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("10-4"), 6)
def test_double_unary(self):
self.assertEqual(ev("--3"), 3)
class D2Division(unittest.TestCase):
def test_true_division(self):
self.assertAlmostEqual(ev("7/2"), 3.5)
def test_exact_division(self):
self.assertEqual(ev("4/2"), 2.0)
def test_div_by_zero_raises_eval_error(self):
with self.assertRaises(EvalError):
ev("1/0")
def test_div_by_zero_not_zero_division_error(self):
try:
ev("1/0")
except EvalError:
pass
except ZeroDivisionError:
self.fail("ZeroDivisionError escaped; expected EvalError")
def test_div_by_zero_expression(self):
with self.assertRaises(EvalError):
ev("5/(3-3)")
class D3ResultType(unittest.TestCase):
"""_format is tested indirectly through the CLI; here we check evaluate() types."""
def test_int_input_returns_int(self):
self.assertIsInstance(ev("3"), int)
def test_non_whole_div_returns_float(self):
result = ev("7/2")
self.assertIsInstance(result, float)
self.assertAlmostEqual(result, 3.5)
def test_whole_div_result_is_float_value_2(self):
result = ev("4/2")
self.assertAlmostEqual(result, 2.0)
class D4CLI(unittest.TestCase):
def _run(self, expr):
return subprocess.run(
[sys.executable, "calc.py", expr],
capture_output=True,
text=True,
)
def test_precedence_14(self):
r = self._run("2+3*4")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "14")
def test_paren_20(self):
r = self._run("(2+3)*4")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "20")
def test_true_division_3_5(self):
r = self._run("7/2")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "3.5")
def test_whole_division_no_trailing_dot(self):
r = self._run("4/2")
self.assertEqual(r.returncode, 0)
self.assertEqual(r.stdout.strip(), "2")
def test_div_by_zero_stderr_nonzero(self):
r = self._run("1/0")
self.assertNotEqual(r.returncode, 0)
self.assertGreater(len(r.stderr.strip()), 0)
self.assertEqual(r.stdout.strip(), "")
def test_invalid_expr_stderr_nonzero(self):
r = self._run("1 +")
self.assertNotEqual(r.returncode, 0)
self.assertGreater(len(r.stderr.strip()), 0)
self.assertEqual(r.stdout.strip(), "")
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,126 @@
"""Unit tests for calc.lexer — covers D1D3."""
import unittest
from calc.lexer import tokenize, Token, LexError
def kinds(src: str) -> list[str]:
return [t.kind for t in tokenize(src)]
def values(src: str) -> list:
return [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_standard(self):
toks = tokenize("3.14")
self.assertEqual(len(toks), 2)
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)
def test_zero(self):
toks = tokenize("0")
self.assertEqual(toks[0], Token("NUMBER", 0))
class TestOperatorsAndParens(unittest.TestCase):
def test_plus(self):
self.assertIn("PLUS", kinds("1+2"))
def test_minus(self):
self.assertIn("MINUS", kinds("1-2"))
def test_star(self):
self.assertIn("STAR", kinds("1*2"))
def test_slash(self):
self.assertIn("SLASH", kinds("1/2"))
def test_lparen(self):
self.assertIn("LPAREN", kinds("(1)"))
def test_rparen(self):
self.assertIn("RPAREN", kinds("(1)"))
def test_expr_kinds(self):
self.assertEqual(
kinds("1+2*3"),
["NUMBER", "PLUS", "NUMBER", "STAR", "NUMBER", "EOF"],
)
def test_eof_always_last(self):
for src in ["", "1", "1+2", "()"]:
self.assertEqual(tokenize(src)[-1].kind, "EOF")
class TestWhitespaceAndErrors(unittest.TestCase):
def test_spaces_between_tokens(self):
self.assertEqual(
kinds(" 12 + 3 "),
["NUMBER", "PLUS", "NUMBER", "EOF"],
)
toks = tokenize(" 12 + 3 ")
self.assertEqual(toks[0].value, 12)
self.assertEqual(toks[2].value, 3)
def test_tabs_skipped(self):
self.assertEqual(kinds("\t1\t+\t2\t"), ["NUMBER", "PLUS", "NUMBER", "EOF"])
def test_complex_expr(self):
self.assertEqual(
kinds("3.5*(1-2)"),
["NUMBER", "STAR", "LPAREN", "NUMBER", "MINUS", "NUMBER", "RPAREN", "EOF"],
)
toks = tokenize("3.5*(1-2)")
self.assertAlmostEqual(toks[0].value, 3.5)
self.assertEqual(toks[3].value, 1)
self.assertEqual(toks[5].value, 2)
def test_invalid_at_raises(self):
with self.assertRaises(LexError):
tokenize("1 @ 2")
def test_invalid_dollar_raises(self):
with self.assertRaises(LexError):
tokenize("$")
def test_invalid_letter_raises(self):
with self.assertRaises(LexError):
tokenize("a")
def test_lex_error_message_contains_char(self):
try:
tokenize("1 @ 2")
self.fail("LexError not raised")
except LexError as e:
self.assertIn("@", str(e))
def test_lex_error_message_contains_position(self):
try:
tokenize("1 @ 2")
self.fail("LexError not raised")
except LexError as e:
self.assertIn("2", str(e))
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,133 @@
"""Tests for calc.parser — asserts on AST structure, not evaluation."""
import unittest
from .lexer import tokenize
from .parser import parse, ParseError, Num, BinOp, Unary
def p(src: str):
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_mixed_four_ops(self):
# 1+2*3-4/2 => BinOp('-', BinOp('+', Num(1), BinOp('*', Num(2), Num(3))),
# BinOp('/', Num(4), Num(2)))
self.assertEqual(
p("1+2*3-4/2"),
BinOp(
"-",
BinOp("+", Num(1), BinOp("*", Num(2), Num(3))),
BinOp("/", Num(4), Num(2)),
),
)
class TestLeftAssociativity(unittest.TestCase):
"""D2 — same-precedence operators associate left."""
def test_subtraction_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_division_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_addition_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_multiplication_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_add_then_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_nested(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_paren_changes_assoc(self):
# 8-(3-2) => BinOp('-', Num(8), BinOp('-', Num(3), Num(2)))
self.assertEqual(p("8-(3-2)"), BinOp("-", Num(8), BinOp("-", Num(3), Num(2))))
def test_paren_single_number(self):
self.assertEqual(p("(42)"), Num(42))
class TestUnaryMinus(unittest.TestCase):
"""D4 — leading and nested unary minus."""
def test_simple_unary(self):
self.assertEqual(p("-5"), Unary("-", Num(5)))
def test_unary_paren(self):
# -(1+2) => Unary('-', BinOp('+', Num(1), Num(2)))
self.assertEqual(p("-(1+2)"), Unary("-", BinOp("+", Num(1), Num(2))))
def test_mul_unary(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_expr(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_operator(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_open_paren(self):
with self.assertRaises(ParseError):
p(")(")
def test_empty_string(self):
with self.assertRaises(ParseError):
p("")
def test_only_operator(self):
with self.assertRaises(ParseError):
p("*")
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,9 @@
# BACKLOG-eval
## Build backlog
(Builder-owned — read-only for Adversary)
All gates D1-D5 implemented and claimed. Awaiting Adversary PASS.
## Adversary findings
None yet.

View File

@ -0,0 +1,12 @@
# BACKLOG — Phase `lex`
## Build backlog
- [x] D1: integers and floats tokenize correctly
- [x] D2: operators and parens tokenize correctly
- [x] D3: whitespace skipped; invalid chars raise LexError
- [x] D4: test suite green (21 tests, 0 failures)
- [ ] Await Adversary PASS on D1D4
## Adversary findings
<!-- Adversary writes here -->

View File

@ -0,0 +1,13 @@
# BACKLOG — Phase `parse`
## Build backlog
- [x] D1 — Precedence: implement grammar with `*`/`/` tighter than `+`/`-`
- [x] D2 — Left associativity: iterative left-fold in `_expr` and `_term`
- [x] D3 — Parentheses: `_primary` handles `LPAREN expr RPAREN`
- [x] D4 — Unary minus: `_unary` handles leading `-` recursively
- [x] D5 — Errors: `ParseError` raised for all malformed inputs
- [x] D6 — Tests green: `calc/test_parser.py` with 23 structural assertions
## Adversary findings
(none yet)

View File

@ -0,0 +1,10 @@
# DECISIONS (append-only, shared)
## D-LEX-1: Token representation
Chose `__slots__`-based class over dataclass or namedtuple. Reason: explicit, zero-overhead, easy for downstream parser/evaluator to consume via `t.kind` / `t.value` without import coupling.
## D-LEX-2: Number regex
`r"\d+\.?\d*|\.\d+"` handles integers, trailing-dot floats (`10.`), leading-dot floats (`.5`), and standard floats (`3.14`). The alternation order matters: `\d+\.?\d*` before `\.\d+` so integers match first.
## D-LEX-3: int vs float distinction
`int(raw)` when no `.` in the matched string; `float(raw)` otherwise. Preserves the plan's requirement that `42` yields int value and `3.14` yields float value.

View File

@ -0,0 +1,45 @@
# JOURNAL-eval — Adversary notes
## 2026-06-15T04:08Z — Initialized
Adversary starting for eval phase. Parse phase completed (all D1-D6 PASS).
Waiting for Builder to create STATUS-eval.md and claim gates.
Planned verification approach:
- D1: Run exact arithmetic expressions from plan, check output values
- D2: Verify true division (7/2=3.5), EvalError on divide-by-zero (not bare ZeroDivisionError)
- D3: Check output format — whole numbers no .0, fractions as float
- D4: CLI exit codes, stderr for errors, no tracebacks
- D5: Full test suite green, no regressions in lex+parse tests
---
## 2026-06-15T04:10Z — Builder implementation notes
Built evaluator, CLI, and tests from scratch in one pass.
### evaluator.py design
- EvalError wraps division-by-zero so the bare ZeroDivisionError never escapes the API.
- evaluate() is a simple recursive dispatch on Num/BinOp/Unary node types.
- True division via Python's `/` operator naturally produces float.
### calc.py CLI design
- _format() prints int for whole-valued floats (value.is_integer()), str(float) otherwise.
- All LexError/ParseError/EvalError caught → stderr message + exit(1), no traceback.
### Test run (commit 14db736)
```
$ python -m unittest -q
Ran 66 tests in 0.147s
OK
```
### CLI spot checks
```
$ 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" → stderr: error: division by zero, exit 1
$ python calc.py "1 +" → stderr: error: unexpected token 'EOF' (None), exit 1
```

View File

@ -0,0 +1,32 @@
# JOURNAL — Phase `lex` (Builder)
## Implementation
Built `calc/lexer.py` with:
- `Token` dataclass-style class with `__slots__ = ("kind", "value")` for efficiency
- `LexError(Exception)` for invalid characters
- `tokenize(src)` using `re.compile(r"\d+\.?\d*|\.\d+")` for number matching
- Integer if no `.` in raw string; float otherwise
- Single-char dispatch table `_SINGLE` for operators/parens
- Raises `LexError` with char + position for unknown characters
- Appends `EOF` token at end
## Test run
```
$ python -m unittest -q
Ran 21 tests in 0.000s
OK
```
## Cold-verify outputs
```
$ 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,43 @@
# JOURNAL — Phase `parse` (Builder)
## 2026-06-15 — Implementation
### Design decisions
- Grammar: `expr → term ((+|-) term)*`, `term → unary ((*|/) unary)*`, `unary → - unary | primary`, `primary → NUMBER | ( expr )`. Standard Pratt/recursive-descent, iterative left-fold for left associativity.
- Node types: `Num`, `BinOp`, `Unary` with `__repr__` and `__eq__` for structural test assertions.
- `ParseError` defined in `parser.py`.
### Test run output
```
$ python -m unittest -q
Ran 44 tests in 0.001s
OK
```
### Cold-verify commands verified locally
```
$ 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 +'))"
calc.parser.ParseError: unexpected token 'EOF' (None)
(exit 1)
$ 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))
$ 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))
$ 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)))
```

View File

@ -0,0 +1,73 @@
# REVIEW-eval — Adversary verdicts
## Phase: eval
Adversary started: 2026-06-15T04:08Z
---
## eval/D1: PASS @2026-06-15T04:12Z
Cold re-run: all 5 plan expressions correct.
- `2+3*4``14`
- `(2+3)*4``20`
- `8-3-2``3`
- `-2+5``3`
- `2*-3``-6`
Additional adversarial: `((2+3)*4-10)/2``5`, `-(-3)``3`, `1-2*3+4``-1` — all correct.
D1Arithmetic suite: 8 tests, OK.
---
## eval/D2: PASS @2026-06-15T04:12Z
Cold re-run:
- `7/2``3.5` (true division) ✓
- `1/0``EvalError("division by zero")` raised, not bare `ZeroDivisionError`
- `5/(3-3)``EvalError` (expression-based zero denominator) ✓
- `EvalError.__bases__` = `(Exception,)` — not a subclass of `ZeroDivisionError`
D2Division suite: 5 tests, OK.
---
## eval/D3: PASS @2026-06-15T04:12Z
Cold re-run:
- `python calc.py "4/2"``2` (no trailing `.0`) ✓
- `python calc.py "7/2"``3.5`
- `_format(5)``'5'`, `_format(5.0)``'5'`, `_format(3.5)``'3.5'`
- Integer inputs (e.g. `2+3`) return `int` from `evaluate()`, format correctly ✓
D3ResultType suite: 3 tests, OK.
---
## eval/D4: PASS @2026-06-15T04:12Z
Cold re-run:
- `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"` → stderr `error: division by zero`, exit 1, stdout empty ✓
- `python calc.py "1 +"` → stderr `error: unexpected token 'EOF' (None)`, exit 1, stdout empty ✓
- No traceback in stderr on error ✓
- No args / too many args → usage to stderr, exit 1 ✓
D4CLI suite: 6 tests, OK.
---
## eval/D5: PASS @2026-06-15T04:12Z
Cold re-run: `python -m unittest -q``Ran 66 tests in 0.153s` / `OK`
- Lex tests (21) + parse tests (23) + evaluator/CLI tests (22) all green ✓
- No regressions in prior phases ✓
- Full suite run twice; consistent result ✓
---
## Summary
All gates D1D5: **PASS**. No defects. No VETO.
Verified cold from commit `14db736` / claimed at `47f3478`.

View File

@ -0,0 +1,44 @@
# REVIEW — Phase `lex` (Adversary)
## Verdicts
### D1: PASS @2026-06-15T04:02Z
Cold-run evidence:
- `tokenize("42")``[('NUMBER', 42), ('EOF', None)]`, value is `<class 'int'>`
- `tokenize("3.14")``NUMBER(3.14)` as float ✓
- `tokenize(".5")``NUMBER(0.5)` as float ✓
- `tokenize("10.")``NUMBER(10.0)` as float ✓
Regex `\d+\.?\d*|\.\d+` correctly handles all three float forms.
### D2: PASS @2026-06-15T04:02Z
Cold-run evidence:
- `tokenize("1+2*3")``['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']`
- All six operators (`+ - * / ( )`) mapped to correct kinds ✓
- Plan cold-verify: `tokenize('3.5*(1-2)')``[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
### D3: PASS @2026-06-15T04:02Z
Cold-run evidence:
- `tokenize(" 12 + 3 ")``['NUMBER', 'PLUS', 'NUMBER', 'EOF']`
- `tokenize("1 @ 2")` raises `LexError: unexpected character '@' at position 2`
- Error message contains offending char and position ✓
- Letters and `$` also raise LexError with position ✓
- Tabs skipped correctly ✓
### D4: PASS @2026-06-15T04:02Z
Cold-run evidence:
```
Ran 21 tests in 0.000s
OK
```
All 21 tests pass, 0 failures. Test suite covers D1D3 including `" 12 + 3 "`, `"3.5*(1-2)"`, and `"1 @ 2"` raising `LexError`.
## Adversarial probes (no failures found)
- Empty string `""``[EOF]` (correct)
- Whitespace-only `" "``[EOF]` (correct)
- `.5+10.` → two floats with operator between (correct)
- Newline raises LexError — consistent with plan (plan specifies only spaces/tabs are skipped)
- Position reporting is 0-indexed and accurate
## Conclusion
All four DoD gates PASS. No defects found. Builder may mark STATUS as DONE.

View File

@ -0,0 +1,44 @@
# REVIEW — Phase `parse` (Adversary)
## Status
All gates D1D6: PASS. Phase complete — no defects found.
## Gate verdicts
### D1 — Precedence: PASS @2026-06-15T04:08Z
Cold run: `parse(tokenize('1+2*3'))``BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))`
Matches expected. Also verified `2*3+1`, `10-4/2`, `1+2*3-4/2` all produce correct precedence trees.
### D2 — Left Associativity: PASS @2026-06-15T04:08Z
Cold run:
- `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))`
Also verified `1+2+3` and `2*3*4` fold left correctly.
### D3 — Parentheses: PASS @2026-06-15T04:08Z
Cold run: `parse(tokenize('(1+2)*3'))``BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))`
Also verified nested parens `((2+3))`, `8-(3-2)`, and `(42)`.
### D4 — Unary Minus: PASS @2026-06-15T04:08Z
Cold run:
- `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)))`
Also verified `--5``Unary('-', Unary('-', Num(5)))` and `1 + -(2 * 3)`.
### D5 — Errors: PASS @2026-06-15T04:08Z
All five plan-specified malformed inputs raise `ParseError` (not any other exception):
- `"1 +"` → ParseError: unexpected token 'EOF' ✓
- `"(1"` → ParseError: expected RPAREN, got 'EOF' ✓
- `"1 2"` → ParseError: unexpected token 'NUMBER' ✓
- `")("` → ParseError: unexpected token 'RPAREN' ✓
- `""` → ParseError: empty input ✓
Extra adversarial cases also raise ParseError: `*5`, `/5`, `()`, `1++2`, `*` alone.
### D6 — Tests Green: PASS @2026-06-15T04:08Z
`python -m unittest -q``Ran 44 tests in 0.001s OK`
23 parser tests (TestPrecedence×4, TestLeftAssociativity×4, TestParentheses×4, TestUnaryMinus×5, TestErrors×6) + 21 lexer tests.
All assertions are structural (equality via `__eq__` on `Num`/`BinOp`/`Unary` nodes), not evaluation.
## Adversary findings
None. No defects found.

View File

@ -0,0 +1,49 @@
# STATUS-eval — Builder
## Phase: eval
Builder claim commit: 14db7363662f9d74fcee7344c41f1bca04e31488
## Gates Claimed
### Gate D1 — arithmetic
WHAT: evaluate(parse(tokenize(s))) correct for +, -, *, /, precedence, parens, unary minus.
HOW: `python -m unittest calc.test_evaluator.D1Arithmetic -q`
EXPECTED: 8 tests, 0 failures, OK
WHERE: calc/test_evaluator.py::D1Arithmetic, commit 14db736
### Gate D2 — division
WHAT: / is true division; division by zero raises EvalError (not ZeroDivisionError).
HOW: `python -m unittest calc.test_evaluator.D2Division -q`
EXPECTED: 5 tests, 0 failures, OK
WHERE: calc/test_evaluator.py::D2Division, commit 14db736
### Gate D3 — result type
WHAT: Whole-valued results print without .0; non-whole as float.
HOW: `python -m unittest calc.test_evaluator.D3ResultType -q` AND:
`python calc.py "4/2"` → stdout: `2`
`python calc.py "7/2"` → stdout: `3.5`
EXPECTED: 3 tests OK; CLI prints "2" and "3.5"
WHERE: calc/evaluator.py, calc.py::_format(), commit 14db736
### Gate D4 — CLI
WHAT: `python calc.py "2+3*4"` prints 14 exits 0; invalid input to stderr exits non-zero.
HOW: `python -m unittest calc.test_evaluator.D4CLI -q`
EXPECTED: 6 tests, 0 failures, OK
Manual spot-checks:
`python calc.py "2+3*4"` → stdout: `14`, exit 0
`python calc.py "(2+3)*4"` → stdout: `20`, exit 0
`python calc.py "7/2"` → stdout: `3.5`, exit 0
`python calc.py "4/2"` → stdout: `2`, exit 0
`python calc.py "1/0"` → stderr: `error: division by zero`, exit 1
`python calc.py "1 +"` → stderr: `error: unexpected token 'EOF' (None)`, exit 1
WHERE: calc.py, commit 14db736
### Gate D5 — tests green + end-to-end
WHAT: Full suite (lex + parse + eval) passes, 0 failures.
HOW: `python -m unittest -q`
EXPECTED: 66 tests, 0 failures, OK
WHERE: calc/test_lexer.py, calc/test_parser.py, calc/test_evaluator.py, commit 14db736
## DONE
All gates D1D5 Adversary-verified PASS (REVIEW-eval.md @2026-06-15T04:12Z). No VETO. Phase eval complete.

View File

@ -0,0 +1,49 @@
# STATUS — Phase `lex` (Builder)
## DONE
All gates D1, D2, D3, D4 verified PASS by Adversary @2026-06-15T04:02Z. Phase `lex` complete.
## Current State
Gates D1, D2, D3, D4: Adversary-verified PASS.
## Gate Claims
### D1 — Numbers
**WHAT:** `tokenize("42")``[NUMBER(42), EOF]`; integers return int, floats return float.
**HOW:** `python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('42')]); print(type(tokenize('42')[0].value))"`
**EXPECTED:** `[('NUMBER', 42), ('EOF', None)]` then `<class 'int'>`
**WHERE:** `calc/lexer.py` commit to be pushed; `calc/test_lexer.py` class `TestNumbers`
### D2 — Operators & Parens
**WHAT:** `+ - * / ( )` each tokenize to the right kind; `tokenize("1+2*3")` yields NUMBER PLUS NUMBER STAR NUMBER EOF.
**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`; `calc/test_lexer.py` class `TestOperatorsAndParens`
### D3 — Whitespace & Errors
**WHAT:** Spaces/tabs skipped; invalid chars raise `LexError` with offending char and position.
**HOW:**
```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: `['NUMBER', 'PLUS', 'NUMBER', 'EOF']`. Second: raises `calc.lexer.LexError: unexpected character '@' at position 2`
**WHERE:** `calc/lexer.py`; `calc/test_lexer.py` class `TestWhitespaceAndErrors`
### D4 — Tests Green
**WHAT:** `python -m unittest -q` passes 21 tests, 0 failures.
**HOW:** `python -m unittest -q` (run from repo root)
**EXPECTED:** `Ran 21 tests in ...s\n\nOK`
**WHERE:** `calc/test_lexer.py`
## Cold-verify commands (from plan)
```bash
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 21 tests in ...s\n\nOK`
2. `[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
3. `calc.lexer.LexError: unexpected character '@' at position 2` (raises, exits non-zero)

View File

@ -0,0 +1,87 @@
# STATUS — Phase `parse` (Builder)
## DONE
All gates D1D6: Adversary-verified PASS (see REVIEW-parse.md @2026-06-15T04:08Z). Phase complete.
## AST Node Shapes (stable API for evaluator)
```
Num(value) # numeric literal; value is int or float
BinOp(op, left, right) # op in {'+', '-', '*', '/'}; left/right are nodes
Unary(op, operand) # op is '-'; operand is a node
```
All nodes implement `__repr__` and `__eq__`.
## Gate Claims
### D1 — Precedence
**WHAT:** `*` and `/` bind tighter than `+` and `-`; `1+2*3` parses as `1+(2*3)`.
**HOW:**
```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)))`
**WHERE:** `calc/parser.py``_expr` iterates `+/-`, `_term` iterates `*//`, so `*` folds first.
### D2 — Left Associativity
**WHAT:** `8-3-2` parses as `(8-3)-2`; `8/4/2` as `(8/4)/2`.
**HOW:**
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8-3-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(3)), Num(2))`
- `BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))`
**WHERE:** `calc/parser.py``_expr` and `_term` use `while` loops (iterative left fold).
### D3 — Parentheses
**WHAT:** `(1+2)*3` parses with `+` under `*`.
**HOW:**
```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))`
**WHERE:** `calc/parser.py``_primary` handles `LPAREN expr RPAREN`, returning inner node to be used in `_term`.
### D4 — Unary Minus
**WHAT:** `-5`, `-(1+2)`, `3 * -2` each parse correctly.
**HOW:**
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5')))"
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-(1+2)')))"
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('3 * -2')))"
```
**EXPECTED:**
- `Unary('-', Num(5))`
- `Unary('-', BinOp('+', Num(1), Num(2)))`
- `BinOp('*', Num(3), Unary('-', Num(2)))`
**WHERE:** `calc/parser.py``_unary` handles `MINUS` recursively before `_primary`.
### D5 — Errors
**WHAT:** Malformed inputs raise `ParseError` (not any other exception).
**HOW:**
```bash
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
try:
parse(tokenize('1 +'))
print('NO ERROR')
except ParseError:
print('ParseError OK')
"
# Repeat for each case: '(1', '1 2', ')(', ''
```
**EXPECTED:** `ParseError` raised for all five inputs: `"1 +"`, `"(1"`, `"1 2"`, `")("`, `""`.
Shortcut — test suite already covers all five (TestErrors class).
**WHERE:** `calc/parser.py``_primary` raises on bad token; `parse()` raises on trailing token or empty input; `_expect` raises on mismatched RPAREN.
### D6 — Tests Green
**WHAT:** `python -m unittest -q` passes, 0 failures; covers D1D5 with structural assertions.
**HOW:**
```bash
python -m unittest -q
```
**EXPECTED:** `Ran 44 tests in ...s\n\nOK` (21 lexer + 23 parser)
**WHERE:** `calc/test_parser.py` — classes TestPrecedence, TestLeftAssociativity, TestParentheses, TestUnaryMinus, TestErrors.