artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
3
calculators/builder-adversary/run-02/.gitignore
vendored
Normal file
3
calculators/builder-adversary/run-02/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
14
calculators/builder-adversary/run-02/GIT-LOG.txt
Normal file
14
calculators/builder-adversary/run-02/GIT-LOG.txt
Normal file
@ -0,0 +1,14 @@
|
||||
# git history (claim/review handshake), from the run's shared bare repo
|
||||
2b8551c status(eval): DONE — all D1-D5 gates PASS, Adversary verified
|
||||
ef58876 review(D1,D2,D3,D4,D5): PASS — all eval gates verified, no findings
|
||||
a047a79 claim(D1,D2,D3,D4,D5): implement evaluator + CLI + tests — all gates claimed
|
||||
ce36ed2 review(eval/init): Adversary initialized for eval phase — awaiting Builder claims
|
||||
a8775c9 status(parse): DONE — all D1-D6 gates PASS, Adversary verified
|
||||
2fbb241 review(D1,D2,D3,D4,D5,D6): PASS — all parse gates verified, no findings
|
||||
92b0c52 claim(D1,D2,D3,D4,D5,D6): implement parser + tests — all gates claimed
|
||||
b66e732 review(parse/init): Adversary initialized for parse phase — awaiting Builder claims
|
||||
1da6170 fix(AF-01): wrap float() in LexError for malformed number literals
|
||||
9f97633 review(D1,D2,D3,D4): PASS — all gates verified; one informational finding AF-01 (ValueError for malformed numbers, non-blocking)
|
||||
c756a0f claim(D1,D2,D3,D4): implement lexer, tests — all gates claimed
|
||||
a081340 review(init): Adversary initialized, awaiting Builder gate claims
|
||||
68c8f88 chore: seed
|
||||
1
calculators/builder-adversary/run-02/README.md
Normal file
1
calculators/builder-adversary/run-02/README.md
Normal file
@ -0,0 +1 @@
|
||||
# calc work repo
|
||||
1
calculators/builder-adversary/run-02/SOURCE.txt
Normal file
1
calculators/builder-adversary/run-02/SOURCE.txt
Normal file
@ -0,0 +1 @@
|
||||
original path: /tmp/ao-campaign-Ofyz4E/builder-adversary/r2
|
||||
33
calculators/builder-adversary/run-02/calc.py
Normal file
33
calculators/builder-adversary/run-02/calc.py
Normal file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Calculator CLI: python calc.py "<expression>"""
|
||||
import sys
|
||||
|
||||
from calc.lexer import tokenize, LexError
|
||||
from calc.parser import parse, ParseError
|
||||
from calc.evaluator import evaluate, EvalError
|
||||
|
||||
|
||||
def _fmt(value) -> str:
|
||||
"""Return value as int string if whole, else as float string."""
|
||||
if isinstance(value, float) and value.is_integer():
|
||||
return str(int(value))
|
||||
return str(value)
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print("usage: calc.py <expression>", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
expr = sys.argv[1]
|
||||
try:
|
||||
tokens = tokenize(expr)
|
||||
ast = parse(tokens)
|
||||
result = evaluate(ast)
|
||||
print(_fmt(result))
|
||||
except (LexError, ParseError, EvalError) as exc:
|
||||
print(f"error: {exc}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
26
calculators/builder-adversary/run-02/calc/evaluator.py
Normal file
26
calculators/builder-adversary/run-02/calc/evaluator.py
Normal file
@ -0,0 +1,26 @@
|
||||
from calc.parser import Num, BinOp, Unary
|
||||
|
||||
|
||||
class EvalError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def evaluate(node) -> "int | float":
|
||||
if isinstance(node, Num):
|
||||
return node.value
|
||||
if isinstance(node, Unary):
|
||||
return -evaluate(node.operand)
|
||||
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 node type: {type(node).__name__!r}")
|
||||
52
calculators/builder-adversary/run-02/calc/lexer.py
Normal file
52
calculators/builder-adversary/run-02/calc/lexer.py
Normal file
@ -0,0 +1,52 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
class LexError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Token:
|
||||
kind: str
|
||||
value: Any
|
||||
|
||||
|
||||
_OPERATORS = {
|
||||
'+': 'PLUS',
|
||||
'-': 'MINUS',
|
||||
'*': 'STAR',
|
||||
'/': 'SLASH',
|
||||
'(': 'LPAREN',
|
||||
')': 'RPAREN',
|
||||
}
|
||||
|
||||
|
||||
def tokenize(src: str) -> list:
|
||||
tokens = []
|
||||
i = 0
|
||||
n = len(src)
|
||||
while i < n:
|
||||
ch = src[i]
|
||||
if ch in ' \t':
|
||||
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]
|
||||
try:
|
||||
value = float(raw) if '.' in raw else int(raw)
|
||||
except ValueError:
|
||||
raise LexError(f"malformed number {raw!r} at position {i}")
|
||||
tokens.append(Token('NUMBER', value))
|
||||
i = j
|
||||
continue
|
||||
if ch in _OPERATORS:
|
||||
tokens.append(Token(_OPERATORS[ch], ch))
|
||||
i += 1
|
||||
continue
|
||||
raise LexError(f"unexpected character {ch!r} at position {i}")
|
||||
tokens.append(Token('EOF', None))
|
||||
return tokens
|
||||
121
calculators/builder-adversary/run-02/calc/parser.py
Normal file
121
calculators/builder-adversary/run-02/calc/parser.py
Normal file
@ -0,0 +1,121 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from calc.lexer import Token
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"Num({self.value!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str
|
||||
left: Any
|
||||
right: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"BinOp({self.op!r}, {self.left!r}, {self.right!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str
|
||||
operand: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"Unary({self.op!r}, {self.operand!r})"
|
||||
|
||||
|
||||
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) -> Token:
|
||||
tok = self._peek()
|
||||
if tok.kind != kind:
|
||||
raise ParseError(
|
||||
f"expected {kind}, got {tok.kind!r} ({tok.value!r})"
|
||||
)
|
||||
self._pos += 1
|
||||
return tok
|
||||
|
||||
def _advance(self) -> Token:
|
||||
tok = self._tokens[self._pos]
|
||||
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':
|
||||
tok = self._peek()
|
||||
raise ParseError(
|
||||
f"unexpected token {tok.kind!r} ({tok.value!r}) after expression"
|
||||
)
|
||||
return node
|
||||
|
||||
def _expr(self):
|
||||
# addition / subtraction — left-associative, lowest precedence
|
||||
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):
|
||||
# multiplication / division — left-associative, higher precedence
|
||||
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):
|
||||
if self._peek().kind == 'MINUS':
|
||||
op = self._advance().value
|
||||
operand = self._unary()
|
||||
return Unary(op, operand)
|
||||
return self._primary()
|
||||
|
||||
def _primary(self):
|
||||
tok = self._peek()
|
||||
if tok.kind == 'NUMBER':
|
||||
self._advance()
|
||||
return Num(tok.value)
|
||||
if tok.kind == 'LPAREN':
|
||||
self._advance()
|
||||
node = self._expr()
|
||||
self._consume('RPAREN')
|
||||
return node
|
||||
raise ParseError(
|
||||
f"unexpected token {tok.kind!r} ({tok.value!r})"
|
||||
)
|
||||
|
||||
|
||||
def parse(tokens: list):
|
||||
"""Parse a token list produced by calc.lexer.tokenize into an AST.
|
||||
|
||||
AST node types:
|
||||
Num(value) — a numeric literal
|
||||
BinOp(op, left, right) — binary +, -, *, /
|
||||
Unary(op, operand) — unary -
|
||||
|
||||
Raises ParseError on malformed input.
|
||||
"""
|
||||
return _Parser(tokens).parse()
|
||||
126
calculators/builder-adversary/run-02/calc/test_evaluator.py
Normal file
126
calculators/builder-adversary/run-02/calc/test_evaluator.py
Normal file
@ -0,0 +1,126 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from calc.lexer import tokenize
|
||||
from calc.parser import parse
|
||||
from calc.evaluator import evaluate, EvalError
|
||||
|
||||
|
||||
def ev(src):
|
||||
return evaluate(parse(tokenize(src)))
|
||||
|
||||
|
||||
class TestArithmetic(unittest.TestCase):
|
||||
# D1: basic arithmetic, precedence, parens, unary minus
|
||||
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_assoc_subtraction(self):
|
||||
self.assertEqual(ev("8-3-2"), 3)
|
||||
|
||||
def test_unary_minus_add(self):
|
||||
self.assertEqual(ev("-2+5"), 3)
|
||||
|
||||
def test_unary_minus_in_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_simple_mul(self):
|
||||
self.assertEqual(ev("3*4"), 12)
|
||||
|
||||
def test_nested_parens(self):
|
||||
self.assertEqual(ev("((3+2))*2"), 10)
|
||||
|
||||
|
||||
class TestDivision(unittest.TestCase):
|
||||
# D2: true division, division by zero raises EvalError
|
||||
def test_true_division(self):
|
||||
self.assertAlmostEqual(ev("7/2"), 3.5)
|
||||
|
||||
def test_div_by_zero(self):
|
||||
with self.assertRaises(EvalError):
|
||||
ev("1/0")
|
||||
|
||||
def test_div_by_zero_expr(self):
|
||||
with self.assertRaises(EvalError):
|
||||
ev("5/(3-3)")
|
||||
|
||||
def test_no_bare_zerodivision(self):
|
||||
try:
|
||||
ev("1/0")
|
||||
except EvalError:
|
||||
pass
|
||||
except ZeroDivisionError:
|
||||
self.fail("ZeroDivisionError escaped the API — should be EvalError")
|
||||
|
||||
|
||||
class TestResultType(unittest.TestCase):
|
||||
# D3: whole-valued results → int-like, non-whole → float
|
||||
def test_whole_division_is_int(self):
|
||||
result = ev("4/2")
|
||||
# 4/2 returns 2.0 as a float; CLI formats it as "2"
|
||||
# The _fmt function in calc.py handles display; here we verify the value
|
||||
self.assertEqual(result, 2)
|
||||
|
||||
def test_non_whole_is_float(self):
|
||||
result = ev("7/2")
|
||||
self.assertIsInstance(result, float)
|
||||
self.assertAlmostEqual(result, 3.5)
|
||||
|
||||
def test_integer_arithmetic_stays_int(self):
|
||||
result = ev("3+4")
|
||||
self.assertIsInstance(result, int)
|
||||
self.assertEqual(result, 7)
|
||||
|
||||
|
||||
class TestCLI(unittest.TestCase):
|
||||
# D4: CLI output and exit codes
|
||||
def _run(self, expr):
|
||||
return subprocess.run(
|
||||
[sys.executable, "calc.py", expr],
|
||||
capture_output=True, text=True,
|
||||
cwd=__file__.rsplit("/calc/", 1)[0],
|
||||
)
|
||||
|
||||
def test_basic_expression(self):
|
||||
r = self._run("2+3*4")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "14")
|
||||
|
||||
def test_paren_expression(self):
|
||||
r = self._run("(2+3)*4")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "20")
|
||||
|
||||
def test_true_division_output(self):
|
||||
r = self._run("7/2")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "3.5")
|
||||
|
||||
def test_whole_division_no_dot(self):
|
||||
r = self._run("4/2")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "2")
|
||||
|
||||
def test_div_by_zero_exits_nonzero(self):
|
||||
r = self._run("1/0")
|
||||
self.assertNotEqual(r.returncode, 0)
|
||||
self.assertGreater(len(r.stderr.strip()), 0)
|
||||
|
||||
def test_invalid_expr_exits_nonzero(self):
|
||||
r = self._run("1 +")
|
||||
self.assertNotEqual(r.returncode, 0)
|
||||
self.assertGreater(len(r.stderr.strip()), 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
132
calculators/builder-adversary/run-02/calc/test_lexer.py
Normal file
132
calculators/builder-adversary/run-02/calc/test_lexer.py
Normal file
@ -0,0 +1,132 @@
|
||||
import unittest
|
||||
from calc.lexer import tokenize, Token, LexError
|
||||
|
||||
|
||||
def kinds(src):
|
||||
return [t.kind for t in tokenize(src)]
|
||||
|
||||
|
||||
def values(src):
|
||||
return [(t.kind, t.value) for t in tokenize(src)]
|
||||
|
||||
|
||||
class TestNumbers(unittest.TestCase):
|
||||
def test_integer(self):
|
||||
toks = tokenize("42")
|
||||
self.assertEqual(len(toks), 2)
|
||||
self.assertEqual(toks[0], Token('NUMBER', 42))
|
||||
self.assertEqual(toks[1], Token('EOF', None))
|
||||
|
||||
def test_float(self):
|
||||
toks = tokenize("3.14")
|
||||
self.assertEqual(toks[0], Token('NUMBER', 3.14))
|
||||
self.assertEqual(toks[1], Token('EOF', None))
|
||||
|
||||
def test_leading_dot(self):
|
||||
toks = tokenize(".5")
|
||||
self.assertEqual(toks[0], Token('NUMBER', 0.5))
|
||||
|
||||
def test_trailing_dot(self):
|
||||
toks = tokenize("10.")
|
||||
self.assertEqual(toks[0], Token('NUMBER', 10.0))
|
||||
|
||||
def test_integer_value_type(self):
|
||||
toks = tokenize("42")
|
||||
self.assertIsInstance(toks[0].value, int)
|
||||
|
||||
def test_float_value_type(self):
|
||||
toks = tokenize("3.14")
|
||||
self.assertIsInstance(toks[0].value, float)
|
||||
|
||||
|
||||
class TestOperatorsAndParens(unittest.TestCase):
|
||||
def test_plus(self):
|
||||
self.assertIn(Token('PLUS', '+'), tokenize("+"))
|
||||
|
||||
def test_minus(self):
|
||||
self.assertIn(Token('MINUS', '-'), tokenize("-"))
|
||||
|
||||
def test_star(self):
|
||||
self.assertIn(Token('STAR', '*'), tokenize("*"))
|
||||
|
||||
def test_slash(self):
|
||||
self.assertIn(Token('SLASH', '/'), tokenize("/"))
|
||||
|
||||
def test_lparen(self):
|
||||
self.assertIn(Token('LPAREN', '('), tokenize("("))
|
||||
|
||||
def test_rparen(self):
|
||||
self.assertIn(Token('RPAREN', ')'), tokenize(")"))
|
||||
|
||||
def test_expression_kinds(self):
|
||||
self.assertEqual(
|
||||
kinds("1+2*3"),
|
||||
['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'],
|
||||
)
|
||||
|
||||
def test_complex_expression(self):
|
||||
self.assertEqual(
|
||||
kinds("3.5*(1-2)"),
|
||||
['NUMBER', 'STAR', 'LPAREN', 'NUMBER', 'MINUS', 'NUMBER', 'RPAREN', 'EOF'],
|
||||
)
|
||||
|
||||
|
||||
class TestWhitespaceAndErrors(unittest.TestCase):
|
||||
def test_spaces_between_tokens(self):
|
||||
self.assertEqual(
|
||||
kinds(" 12 + 3 "),
|
||||
['NUMBER', 'PLUS', 'NUMBER', 'EOF'],
|
||||
)
|
||||
|
||||
def test_tabs_skipped(self):
|
||||
self.assertEqual(kinds("1\t+\t2"), ['NUMBER', 'PLUS', 'NUMBER', 'EOF'])
|
||||
|
||||
def test_invalid_char_raises(self):
|
||||
with self.assertRaises(LexError) as ctx:
|
||||
tokenize("1 @ 2")
|
||||
self.assertIn('@', str(ctx.exception))
|
||||
|
||||
def test_dollar_raises(self):
|
||||
with self.assertRaises(LexError):
|
||||
tokenize("$")
|
||||
|
||||
def test_letter_raises(self):
|
||||
with self.assertRaises(LexError):
|
||||
tokenize("x")
|
||||
|
||||
def test_lex_error_position(self):
|
||||
with self.assertRaises(LexError) as ctx:
|
||||
tokenize("1 @ 2")
|
||||
self.assertIn('2', str(ctx.exception)) # position 2
|
||||
|
||||
|
||||
class TestEndToEnd(unittest.TestCase):
|
||||
def test_padded_addition(self):
|
||||
v = values(" 12 + 3 ")
|
||||
self.assertEqual(v, [('NUMBER', 12), ('PLUS', '+'), ('NUMBER', 3), ('EOF', None)])
|
||||
|
||||
def test_complex_with_values(self):
|
||||
v = values("3.5*(1-2)")
|
||||
self.assertEqual(v, [
|
||||
('NUMBER', 3.5),
|
||||
('STAR', '*'),
|
||||
('LPAREN', '('),
|
||||
('NUMBER', 1),
|
||||
('MINUS', '-'),
|
||||
('NUMBER', 2),
|
||||
('RPAREN', ')'),
|
||||
('EOF', None),
|
||||
])
|
||||
|
||||
def test_eof_always_last(self):
|
||||
for src in ["", "1", "1+2", "()"]:
|
||||
toks = tokenize(src)
|
||||
self.assertEqual(toks[-1].kind, 'EOF')
|
||||
|
||||
def test_empty_string(self):
|
||||
toks = tokenize("")
|
||||
self.assertEqual(toks, [Token('EOF', None)])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
128
calculators/builder-adversary/run-02/calc/test_parser.py
Normal file
128
calculators/builder-adversary/run-02/calc/test_parser.py
Normal file
@ -0,0 +1,128 @@
|
||||
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_precedence(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_precedence(self):
|
||||
# 2*3+1 => BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))
|
||||
tree = p("2*3+1")
|
||||
self.assertEqual(tree, BinOp('+', BinOp('*', Num(2), Num(3)), Num(1)))
|
||||
|
||||
def test_sub_div_precedence(self):
|
||||
# 6-4/2 => BinOp('-', Num(6), BinOp('/', Num(4), Num(2)))
|
||||
tree = p("6-4/2")
|
||||
self.assertEqual(tree, BinOp('-', Num(6), BinOp('/', Num(4), Num(2))))
|
||||
|
||||
|
||||
class TestLeftAssociativity(unittest.TestCase):
|
||||
# D2: same-precedence operators associate left
|
||||
def test_subtraction_left_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_division_left_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_addition_left_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_left_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_plus_under_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):
|
||||
tree = p("((4))")
|
||||
self.assertEqual(tree, Num(4))
|
||||
|
||||
def test_paren_changes_assoc(self):
|
||||
# 8-(3-2) => BinOp('-', Num(8), BinOp('-', Num(3), Num(2)))
|
||||
tree = p("8-(3-2)")
|
||||
self.assertEqual(tree, BinOp('-', Num(8), BinOp('-', Num(3), Num(2))))
|
||||
|
||||
|
||||
class TestUnaryMinus(unittest.TestCase):
|
||||
# D4: unary minus
|
||||
def test_simple_unary(self):
|
||||
tree = p("-5")
|
||||
self.assertEqual(tree, Unary('-', Num(5)))
|
||||
|
||||
def test_unary_in_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_unary_in_mul(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))))
|
||||
|
||||
def test_unary_plus_expr(self):
|
||||
# -1 + 2 => BinOp('+', Unary('-', Num(1)), Num(2))
|
||||
tree = p("-1+2")
|
||||
self.assertEqual(tree, BinOp('+', Unary('-', Num(1)), 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_extra_number(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("1 2")
|
||||
|
||||
def test_close_before_open(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("*")
|
||||
|
||||
def test_mismatched_paren(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("(1+2")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@ -0,0 +1,13 @@
|
||||
# BACKLOG-eval
|
||||
|
||||
## Build backlog
|
||||
(Builder-owned — read only to Adversary)
|
||||
|
||||
- [x] D1: implement evaluate() for arithmetic, precedence, parens, unary minus
|
||||
- [x] D2: true division; EvalError on divide-by-zero
|
||||
- [x] D3: _fmt() for whole vs non-whole display
|
||||
- [x] D4: calc.py CLI
|
||||
- [x] D5: test_evaluator.py (22 tests); full suite 68 tests green
|
||||
|
||||
## Adversary findings
|
||||
(No findings yet — eval phase not started)
|
||||
@ -0,0 +1,27 @@
|
||||
# BACKLOG-lex
|
||||
|
||||
## Build backlog
|
||||
(Builder-owned — read-only to Adversary)
|
||||
|
||||
## Adversary findings
|
||||
|
||||
### AF-01: unhandled ValueError for malformed number literals [informational, non-blocking]
|
||||
|
||||
**Repro:**
|
||||
```python
|
||||
from calc.lexer import tokenize, LexError
|
||||
tokenize('1.2.3') # raises ValueError, not LexError
|
||||
tokenize('.') # raises ValueError, not LexError
|
||||
tokenize('..') # raises ValueError, not LexError
|
||||
```
|
||||
|
||||
**Root cause:** `lexer.py` line 39: `float(raw)` is called without a try/except. If the
|
||||
greedy digit/dot scan produces an unparseable string (e.g. `1.2.3` or bare `.`), Python
|
||||
raises `ValueError` instead of the module's `LexError`.
|
||||
|
||||
**Impact:** Not a DoD violation (D3 specifies invalid *characters*, not malformed tokens).
|
||||
However it leaks internal Python exceptions for unusual but possible inputs. Recommend
|
||||
wrapping in `try/except ValueError` and re-raising as `LexError` with position info.
|
||||
|
||||
**Status:** Informational — Builder may address in this phase or a follow-up. Adversary
|
||||
will close this finding if re-tested and passing.
|
||||
@ -0,0 +1,16 @@
|
||||
# BACKLOG-parse
|
||||
|
||||
## Build backlog
|
||||
|
||||
- [x] D1 — precedence: implemented via separate `_expr`/`_term` levels
|
||||
- [x] D2 — left associativity: `while` loop in `_expr`/`_term`
|
||||
- [x] D3 — parentheses: `_primary` handles LPAREN/RPAREN
|
||||
- [x] D4 — unary minus: `_unary` level, right-recursive
|
||||
- [x] D5 — ParseError: defined and raised for all malformed inputs
|
||||
- [x] D6 — tests green: 46 tests, 0 failures
|
||||
|
||||
All items complete. Awaiting Adversary verification.
|
||||
|
||||
## Adversary findings
|
||||
|
||||
_(Adversary writes here)_
|
||||
@ -0,0 +1,10 @@
|
||||
# DECISIONS — shared (append-only)
|
||||
|
||||
## 2026-06-15
|
||||
- Adversary initialized; awaiting Builder gate claims on D1–D4
|
||||
|
||||
## D-001: Token representation
|
||||
Token is a dataclass with `kind: str` and `value: Any`. NUMBER tokens carry int or float value; operator tokens carry the character string; EOF carries None. This makes the type easy to pattern-match in future parser/evaluator phases.
|
||||
|
||||
## D-002: LexError
|
||||
LexError subclasses Exception (not ValueError) for clean catching. Message format: `"unexpected character {char!r} at position {pos}"`.
|
||||
@ -0,0 +1,35 @@
|
||||
# JOURNAL-eval — Builder
|
||||
|
||||
## 2026-06-15 — Implementation
|
||||
|
||||
### What was built
|
||||
|
||||
- `calc/evaluator.py`: `EvalError` exception + `evaluate(node) -> int | float` walking AST nodes (Num, BinOp, Unary). Division by zero raises `EvalError` explicitly before Python's `ZeroDivisionError` can escape.
|
||||
- `calc.py` (root): CLI entry point. Calls `tokenize → parse → evaluate`. `_fmt()` converts whole-valued floats to int display.
|
||||
- `calc/test_evaluator.py`: 22 unittest tests across TestArithmetic (9), TestDivision (4), TestResultType (3), TestCLI (6).
|
||||
|
||||
### Test run
|
||||
|
||||
```
|
||||
$ python -m unittest -q
|
||||
Ran 68 tests in 0.224s
|
||||
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" → error: division by zero (exit 1)
|
||||
$ python calc.py "1 +" → error: unexpected token 'EOF' (None) (exit 1)
|
||||
```
|
||||
|
||||
All match DoD expected values.
|
||||
|
||||
### Design notes
|
||||
|
||||
- `evaluate` always returns `int` for integer operations and `float` for true division. The `_fmt` function in `calc.py` handles D3 display: floats that are whole become int strings.
|
||||
- `EvalError` wraps division by zero via an explicit `if right == 0` check before the `/` operator — avoids bare `ZeroDivisionError`.
|
||||
@ -0,0 +1,72 @@
|
||||
# JOURNAL-lex — Adversary
|
||||
|
||||
## 2026-06-15 — Wake 1
|
||||
- Read phase plan: mission is lexer for Python arithmetic calculator
|
||||
- Checked origin/main: only seed commit, Builder has not pushed any work yet
|
||||
- Set up REVIEW-lex.md, BACKLOG-lex.md, STATUS-lex.md, JOURNAL-lex.md
|
||||
- Will poll for Builder claims
|
||||
|
||||
## 2026-06-15 — Wake 2 (watchdog ping)
|
||||
- Pulled claim(D1,D2,D3,D4) commit from Builder — all gates claimed at once
|
||||
- Read STATUS-lex.md for verification commands (did NOT read JOURNAL before verdicts)
|
||||
- Cold-verified all four gates from my own clone:
|
||||
|
||||
### D1 verification
|
||||
- `tokenize('42')` → NUMBER(42 int) EOF ✓
|
||||
- `tokenize('3.14')` → NUMBER(3.14 float) EOF ✓
|
||||
- `tokenize('.5')` → NUMBER(0.5) EOF ✓
|
||||
- `tokenize('10.')` → NUMBER(10.0) EOF ✓
|
||||
|
||||
### D2 verification
|
||||
- `tokenize('1+2*3')` kinds → ['NUMBER','PLUS','NUMBER','STAR','NUMBER','EOF'] ✓
|
||||
- `tokenize('3.5*(1-2)')` → correct full token list ✓
|
||||
- All 6 operator kinds present in test suite ✓
|
||||
|
||||
### D3 verification
|
||||
- `tokenize(' 12 + 3 ')` → ['NUMBER','PLUS','NUMBER','EOF'] ✓
|
||||
- `tokenize('1 @ 2')` → LexError: "unexpected character '@' at position 2" ✓
|
||||
- '@' and '2' both in error message ✓
|
||||
|
||||
### D4 verification
|
||||
- `python -m unittest -q` → Ran 24 tests, OK, exit 0 ✓
|
||||
- Three mandatory test inputs from DoD confirmed in test suite ✓
|
||||
|
||||
### Break-it probes
|
||||
- `tokenize('1.2.3')` → ValueError (not LexError) — logged as AF-01 (non-blocking)
|
||||
- `tokenize('.')` → ValueError (not LexError) — same issue
|
||||
- `tokenize('..')` → ValueError (not LexError) — same issue
|
||||
- `tokenize('1x')` → LexError at position 1 ✓ (x is invalid char)
|
||||
- `tokenize('\n')` → LexError ✓ (newline not in whitespace set)
|
||||
- `tokenize('')` → [Token(EOF, None)] ✓
|
||||
- SLASH operator explicitly tested ✓
|
||||
|
||||
### Verdict
|
||||
All four gates: PASS. One informational finding (AF-01: ValueError for malformed number
|
||||
literals). No VETO issued. Builder may write ## DONE.
|
||||
|
||||
## 2026-06-15 — Builder Wake 2 (post-verdicts)
|
||||
|
||||
Pulled Adversary verdict commit. All four gates PASS.
|
||||
|
||||
Addressed AF-01: wrapped `float(raw)` in try/except ValueError → re-raises as LexError.
|
||||
|
||||
```
|
||||
$ python -m unittest -q
|
||||
Ran 24 tests in 0.000s
|
||||
OK
|
||||
|
||||
$ python -c "
|
||||
from calc.lexer import tokenize, LexError
|
||||
for case in ['1.2.3', '.', '..']:
|
||||
try:
|
||||
tokenize(case)
|
||||
print(f'FAIL: {case!r}')
|
||||
except LexError as e:
|
||||
print(f'OK: {case!r} -> LexError: {e}')
|
||||
"
|
||||
OK: '1.2.3' -> LexError: malformed number '1.2.3' at position 0
|
||||
OK: '.' -> LexError: malformed number '.' at position 0
|
||||
OK: '..' -> LexError: malformed number '..' at position 0
|
||||
```
|
||||
|
||||
Updated STATUS-lex.md to ## DONE. Phase lex complete.
|
||||
@ -0,0 +1,45 @@
|
||||
# JOURNAL-parse — Builder
|
||||
|
||||
## 2026-06-15
|
||||
|
||||
### Implementation
|
||||
|
||||
Built `calc/parser.py` as a classic recursive-descent parser with three precedence levels:
|
||||
|
||||
```
|
||||
expr → term (('+' | '-') term)* # left-assoc, lowest
|
||||
term → unary (('*' | '/') unary)* # left-assoc, higher
|
||||
unary → '-' unary | primary # right-recursive for nested --
|
||||
primary→ NUMBER | '(' expr ')'
|
||||
```
|
||||
|
||||
This naturally yields left-associativity (the `while` loop builds left-leaning trees) and correct precedence (mul/div are parsed inside `term` which is called from `expr`).
|
||||
|
||||
### Test run output
|
||||
|
||||
```
|
||||
$ python -m unittest -q
|
||||
......................................................
|
||||
Ran 46 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
(46 = 9 existing lex tests + 17 new parser tests)
|
||||
|
||||
### Manual gate verification
|
||||
|
||||
```
|
||||
D1 add-mul: BinOp('+', Num(1), BinOp('*', Num(2), Num(3))) ✓
|
||||
D1 mul-add: BinOp('+', BinOp('*', Num(2), Num(3)), Num(1)) ✓
|
||||
D2 sub: BinOp('-', BinOp('-', Num(8), Num(3)), Num(2)) ✓
|
||||
D2 div: BinOp('/', BinOp('/', Num(8), Num(4)), Num(2)) ✓
|
||||
D3 paren: BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)) ✓
|
||||
D4 unary: Unary('-', Num(5)) ✓
|
||||
D4 u-paren: Unary('-', BinOp('+', Num(1), Num(2))) ✓
|
||||
D4 mul-u: BinOp('*', Num(3), Unary('-', Num(2))) ✓
|
||||
D5 '1 +' → ParseError: unexpected token 'EOF' (None) ✓
|
||||
D5 '(1' → ParseError: expected RPAREN, got 'EOF' (None) ✓
|
||||
D5 '1 2' → ParseError: unexpected token 'NUMBER' (2) after expression ✓
|
||||
D5 ')(' → ParseError: unexpected token 'RPAREN' (')') ✓
|
||||
D5 '' → ParseError: empty expression ✓
|
||||
```
|
||||
120
calculators/builder-adversary/run-02/machine-docs/REVIEW-eval.md
Normal file
120
calculators/builder-adversary/run-02/machine-docs/REVIEW-eval.md
Normal file
@ -0,0 +1,120 @@
|
||||
# REVIEW-eval — Adversary Verdicts
|
||||
|
||||
## Legend
|
||||
- PASS @<ts> — gate accepted, evidence below
|
||||
- FAIL — repro steps below, Builder must fix
|
||||
|
||||
---
|
||||
|
||||
## D1 — arithmetic
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run — all 5 DoD-mandated cases:
|
||||
```
|
||||
'2+3*4' -> 14 expected 14 OK
|
||||
'(2+3)*4' -> 20 expected 20 OK
|
||||
'8-3-2' -> 3 expected 3 OK
|
||||
'-2+5' -> 3 expected 3 OK
|
||||
'2*-3' -> -6 expected -6 OK
|
||||
```
|
||||
|
||||
Extra break-it probes (all correct):
|
||||
```
|
||||
'2+3+4' -> 9 OK (left-assoc addition)
|
||||
'10-2-3' -> 5 OK (left-assoc subtraction)
|
||||
'2*3*4' -> 24 OK (left-assoc multiplication)
|
||||
'--5' -> 5 OK (double unary minus)
|
||||
'(-3)*(-2)' -> 6 OK (unary in parens)
|
||||
'(1+2)*(3+4)' -> 21 OK (nested parens)
|
||||
'0*100' -> 0 OK
|
||||
```
|
||||
|
||||
`python -m unittest calc.test_evaluator.TestArithmetic -q` — 0 failures.
|
||||
|
||||
---
|
||||
|
||||
## D2 — division
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run:
|
||||
```
|
||||
'7/2' -> 3.5 OK (true division)
|
||||
'1/0' -> EvalError: division by zero OK (not ZeroDivisionError)
|
||||
'5/(3-3)' -> EvalError: division by zero OK (dynamic zero denominator)
|
||||
```
|
||||
|
||||
Implementation: explicit `if right == 0: raise EvalError(...)` at `calc/evaluator.py:18-21` — `ZeroDivisionError` cannot escape the API boundary.
|
||||
|
||||
`python -m unittest calc.test_evaluator.TestDivision -q` — 0 failures.
|
||||
|
||||
---
|
||||
|
||||
## D3 — result type
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run — CLI output (stdout only, no stderr):
|
||||
```
|
||||
'4/2' -> '2' OK (whole float -> int display)
|
||||
'9/3' -> '3' OK (whole float -> int display)
|
||||
'0/5' -> '0' OK (zero result -> int display)
|
||||
'7/2' -> '3.5' OK (non-whole)
|
||||
'1/3' -> '0.3333333333333333' OK (non-whole)
|
||||
'22/7' -> '3.142857142857143' OK (non-whole)
|
||||
```
|
||||
|
||||
Rule confirmed: `_fmt()` in `calc.py` calls `value.is_integer()` on floats; whole → cast to int for display.
|
||||
|
||||
`python -m unittest calc.test_evaluator.TestResultType -q` — 0 failures.
|
||||
|
||||
---
|
||||
|
||||
## D4 — CLI
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run — all DoD cases:
|
||||
```
|
||||
python calc.py "2+3*4" -> stdout='14' stderr='' exit=0 OK
|
||||
python calc.py "(2+3)*4" -> stdout='20' stderr='' exit=0 OK
|
||||
python calc.py "7/2" -> stdout='3.5' stderr='' exit=0 exit=0 OK
|
||||
python calc.py "4/2" -> stdout='2' stderr='' exit=0 OK
|
||||
python calc.py "1/0" -> stdout='' stderr='error: division by zero' exit=1 OK
|
||||
python calc.py "1 +" -> stdout='' stderr='error: unexpected token ...' exit=1 OK
|
||||
```
|
||||
|
||||
Additional probes:
|
||||
- No-arg: stderr='usage: calc.py <expression>', exit=1 OK
|
||||
- Empty string `""`: stderr='error: empty expression', exit=1 OK
|
||||
- No traceback in any error case (grepped for "Traceback" — not found) OK
|
||||
- Errors go to stderr, stdout is empty on error (verified via redirect) OK
|
||||
|
||||
---
|
||||
|
||||
## D5 — tests green + end-to-end
|
||||
**PASS @2026-06-15T00:54Z**
|
||||
|
||||
Cold run:
|
||||
```
|
||||
$ python -m unittest -q
|
||||
----------------------------------------------------------------------
|
||||
Ran 68 tests in 0.210s
|
||||
|
||||
OK
|
||||
```
|
||||
|
||||
Exit code 0. 68/68 pass (24 lex + 22 parse + 22 eval, including 6 CLI subprocess tests).
|
||||
|
||||
No regression in prior lex/parse tests.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
| Gate | Verdict |
|
||||
|------|---------|
|
||||
| D1 — arithmetic | **PASS** |
|
||||
| D2 — division | **PASS** |
|
||||
| D3 — result type | **PASS** |
|
||||
| D4 — CLI | **PASS** |
|
||||
| D5 — tests green | **PASS** |
|
||||
|
||||
All gates PASS. No findings. Builder may write "## DONE" to STATUS-eval.md.
|
||||
103
calculators/builder-adversary/run-02/machine-docs/REVIEW-lex.md
Normal file
103
calculators/builder-adversary/run-02/machine-docs/REVIEW-lex.md
Normal file
@ -0,0 +1,103 @@
|
||||
# REVIEW-lex — Adversary Verdicts
|
||||
|
||||
## Legend
|
||||
- PASS @<ts> — gate accepted, evidence below
|
||||
- FAIL — repro steps below, Builder must fix
|
||||
|
||||
---
|
||||
|
||||
## D1 — numbers
|
||||
**PASS @2026-06-15T00:36Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
python -c "...tokenize('42')..." → NUMBER(42, int), EOF — PASS
|
||||
python -c "...tokenize('3.14')..." → NUMBER(3.14, float), EOF — PASS
|
||||
python -c "...tokenize('.5')..." → NUMBER(0.5), EOF — PASS
|
||||
python -c "...tokenize('10.')..." → NUMBER(10.0), EOF — PASS
|
||||
```
|
||||
Type assertions: `isinstance(42, int)` ✓, `isinstance(3.14, float)` ✓
|
||||
|
||||
---
|
||||
|
||||
## D2 — operators & parens
|
||||
**PASS @2026-06-15T00:36Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
tokenize('1+2*3') kinds → ['NUMBER','PLUS','NUMBER','STAR','NUMBER','EOF'] ✓
|
||||
tokenize('3.5*(1-2)') → [('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)] ✓
|
||||
All 6 operators (PLUS MINUS STAR SLASH LPAREN RPAREN) individually tested ✓
|
||||
SLASH explicitly tested in test_lexer.py ✓
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## D3 — whitespace & errors
|
||||
**PASS @2026-06-15T00:36Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
tokenize(' 12 + 3 ') kinds → ['NUMBER','PLUS','NUMBER','EOF'] ✓
|
||||
tokenize('1 @ 2') → raises calc.lexer.LexError: unexpected character '@' at position 2
|
||||
'@' in message ✓, '2' (position) in message ✓
|
||||
Plan's verbatim command exits code 1 with correct traceback ✓
|
||||
```
|
||||
|
||||
Break-it probes run (see Adversary findings for non-blocking issues):
|
||||
- `$` raises LexError ✓
|
||||
- `x` (letter) raises LexError ✓
|
||||
- `\n` raises LexError (treated as invalid char, reasonable) ✓
|
||||
|
||||
---
|
||||
|
||||
## D4 — tests green
|
||||
**PASS @2026-06-15T00:36Z**
|
||||
|
||||
Cold run:
|
||||
```
|
||||
$ python -m unittest -q
|
||||
----------------------------------------------------------------------
|
||||
Ran 24 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
Exit code 0. 24/24 pass.
|
||||
|
||||
DoD-mandated test inputs confirmed present:
|
||||
- `" 12 + 3 "` — covered by test_spaces_between_tokens + test_padded_addition ✓
|
||||
- `"3.5*(1-2)"` — covered by test_complex_expression + test_complex_with_values ✓
|
||||
- `"1 @ 2"` raises LexError — covered by test_invalid_char_raises + test_lex_error_position ✓
|
||||
|
||||
---
|
||||
|
||||
## Non-blocking finding: unhandled ValueError for malformed number literals
|
||||
|
||||
**Severity: informational — does not fail any DoD gate**
|
||||
|
||||
`tokenize('1.2.3')`, `tokenize('.')`, `tokenize('..')` all raise Python's built-in
|
||||
`ValueError` ("could not convert string to float: ...") instead of `LexError`.
|
||||
The lexer greedily consumes digit/dot sequences then passes the raw string to
|
||||
`float()` without catching failure.
|
||||
|
||||
The DoD's D3 specifies "invalid character (e.g. @, $, a letter)" — not malformed
|
||||
number literals — so this does not block PASS. However, downstream parser/evaluator
|
||||
phases will see unexpected ValueError exceptions from edge-case inputs. The Builder
|
||||
should consider wrapping the `float(raw)` call in a try/except that re-raises as
|
||||
`LexError`.
|
||||
|
||||
This finding is noted only; the Builder may address it in a follow-up or the next phase.
|
||||
No VETO issued.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
| Gate | Verdict |
|
||||
|------|---------|
|
||||
| D1 — numbers | **PASS** |
|
||||
| D2 — operators & parens | **PASS** |
|
||||
| D3 — whitespace & errors | **PASS** |
|
||||
| D4 — tests green | **PASS** |
|
||||
|
||||
All gates PASS. Builder may write "## DONE" to STATUS-lex.md.
|
||||
@ -0,0 +1,126 @@
|
||||
# REVIEW-parse — Adversary Verdicts
|
||||
|
||||
## Legend
|
||||
- PASS @<ts> — gate accepted, evidence below
|
||||
- FAIL — repro steps below, Builder must fix
|
||||
|
||||
---
|
||||
|
||||
## D1 — precedence
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
parse(tokenize('1+2*3')) → BinOp('+', Num(1), BinOp('*', Num(2), Num(3))) ✓
|
||||
parse(tokenize('2*3+1')) → BinOp('+', BinOp('*', Num(2), Num(3)), Num(1)) ✓
|
||||
```
|
||||
Both match expected repr exactly. `*` binds tighter than `+` in both orderings.
|
||||
|
||||
Extra probe — complex chain `1+2+3*4-5`:
|
||||
→ `BinOp('-', BinOp('+', BinOp('+', Num(1), Num(2)), BinOp('*', Num(3), Num(4))), Num(5))` ✓
|
||||
`3*4` is correctly nested under addition/subtraction.
|
||||
|
||||
---
|
||||
|
||||
## D2 — left associativity
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
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)) ✓
|
||||
```
|
||||
|
||||
Extra probes:
|
||||
```
|
||||
parse(tokenize('2*3*4')) → BinOp('*', BinOp('*', Num(2), Num(3)), Num(4)) ✓
|
||||
parse(tokenize('1+2+3')) → BinOp('+', BinOp('+', Num(1), Num(2)), Num(3)) ✓
|
||||
```
|
||||
Explicit assertion `r == BinOp('+', BinOp('+', Num(1), Num(2)), Num(3))` passed.
|
||||
|
||||
---
|
||||
|
||||
## D3 — parentheses
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
parse(tokenize('(1+2)*3')) → BinOp('*', BinOp('+', Num(1), Num(2)), Num(3)) ✓
|
||||
parse(tokenize('(-3)*2')) → BinOp('*', Unary('-', Num(3)), Num(2)) ✓
|
||||
```
|
||||
Parens correctly place `+` sub-tree under `*`.
|
||||
|
||||
---
|
||||
|
||||
## D4 — unary minus
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run evidence:
|
||||
```
|
||||
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 probes:
|
||||
```
|
||||
parse(tokenize('--5')) → Unary('-', Unary('-', Num(5))) ✓ (recursive, correct)
|
||||
parse(tokenize('(-3)*2')) → BinOp('*', Unary('-', Num(3)), Num(2)) ✓
|
||||
```
|
||||
`_unary` is correctly recursive for double-negation.
|
||||
|
||||
---
|
||||
|
||||
## D5 — errors
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run — all five DoD-mandated cases:
|
||||
```
|
||||
'1 +' → ParseError: unexpected token 'EOF' (None) ✓
|
||||
'(1' → ParseError: expected RPAREN, got 'EOF' (None) ✓
|
||||
'1 2' → ParseError: unexpected token 'NUMBER' (2) after expression ✓
|
||||
')(' → ParseError: unexpected token 'RPAREN' (')') ✓
|
||||
'' → ParseError: empty expression ✓
|
||||
```
|
||||
All raise `ParseError` (not `ValueError`, `IndexError`, or other exceptions).
|
||||
|
||||
Extra break-it probes — all raise `ParseError` (not other exceptions):
|
||||
```
|
||||
'+5' → ParseError: unexpected token 'PLUS' ('+') ✓
|
||||
'1++2' → ParseError: unexpected token 'PLUS' ('+') ✓
|
||||
'()' → ParseError: unexpected token 'RPAREN' (')') ✓
|
||||
'1 /' → ParseError: unexpected token 'EOF' (None) ✓
|
||||
'* 2' → ParseError: unexpected token 'STAR' ('*') ✓
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## D6 — tests green
|
||||
**PASS @2026-06-15T00:50Z**
|
||||
|
||||
Cold run:
|
||||
```
|
||||
$ python -m unittest -q
|
||||
----------------------------------------------------------------------
|
||||
Ran 46 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
Exit code 0. 46/46 pass (24 lex + 22 parser).
|
||||
|
||||
DoD requires 0 failures — confirmed.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
| Gate | Verdict |
|
||||
|------|---------|
|
||||
| D1 — precedence | **PASS** |
|
||||
| D2 — left associativity | **PASS** |
|
||||
| D3 — parentheses | **PASS** |
|
||||
| D4 — unary minus | **PASS** |
|
||||
| D5 — errors | **PASS** |
|
||||
| D6 — tests green | **PASS** |
|
||||
|
||||
All gates PASS. No findings. Builder may write "## DONE" to STATUS-parse.md.
|
||||
111
calculators/builder-adversary/run-02/machine-docs/STATUS-eval.md
Normal file
111
calculators/builder-adversary/run-02/machine-docs/STATUS-eval.md
Normal file
@ -0,0 +1,111 @@
|
||||
# STATUS-eval — Builder
|
||||
|
||||
## DONE
|
||||
|
||||
## Current state
|
||||
|
||||
Gate: D1, D2, D3, D4, D5 — all PASS (Adversary verified @2026-06-15T00:54Z)
|
||||
|
||||
---
|
||||
|
||||
## Claims
|
||||
|
||||
### D1 — arithmetic
|
||||
|
||||
**What:** `evaluate(parse(tokenize(s)))` correct for `+ - * /`, precedence, parens, unary minus.
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python -m unittest calc.test_evaluator.TestArithmetic -q
|
||||
```
|
||||
|
||||
**Expected:** 0 failures. Specific cases:
|
||||
- `"2+3*4"` → 14
|
||||
- `"(2+3)*4"` → 20
|
||||
- `"8-3-2"` → 3
|
||||
- `"-2+5"` → 3
|
||||
- `"2*-3"` → -6
|
||||
|
||||
**Where:** `calc/evaluator.py` + `calc/test_evaluator.py`
|
||||
|
||||
---
|
||||
|
||||
### D2 — division
|
||||
|
||||
**What:** `/` is true division; `EvalError` raised on division by zero, not bare `ZeroDivisionError`.
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python -m unittest calc.test_evaluator.TestDivision -q
|
||||
```
|
||||
|
||||
**Expected:** 0 failures. Includes:
|
||||
- `"7/2"` → 3.5
|
||||
- `"1/0"` → `EvalError`
|
||||
- `"5/(3-3)"` → `EvalError`
|
||||
- No `ZeroDivisionError` escaping the API
|
||||
|
||||
**Where:** `calc/evaluator.py:18-21`
|
||||
|
||||
---
|
||||
|
||||
### D3 — result type
|
||||
|
||||
**What:** Whole-valued results print without `.0`; non-whole as float.
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python -m unittest calc.test_evaluator.TestResultType -q
|
||||
python calc.py "4/2" # should print: 2
|
||||
python calc.py "7/2" # should print: 3.5
|
||||
```
|
||||
|
||||
**Expected:** 0 failures; `4/2` → `2` (no dot), `7/2` → `3.5`.
|
||||
|
||||
**Rule:** The `_fmt()` function in `calc.py` checks `value.is_integer()` on floats; if true, casts to `int` for display.
|
||||
|
||||
**Where:** `calc.py:_fmt()`, `calc/test_evaluator.py:TestResultType`
|
||||
|
||||
---
|
||||
|
||||
### D4 — CLI
|
||||
|
||||
**What:** `python calc.py "2+3*4"` prints `14` and exits 0; invalid expression prints error to stderr and exits non-zero (no traceback).
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
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: ..., exit 1
|
||||
python calc.py "1 +" # stderr: error: ..., exit 1
|
||||
```
|
||||
|
||||
**Expected:** Exact outputs as above. All errors caught by `(LexError, ParseError, EvalError)` — no Python traceback.
|
||||
|
||||
**Where:** `calc.py`
|
||||
|
||||
---
|
||||
|
||||
### D5 — tests green + end-to-end
|
||||
|
||||
**What:** Full unittest suite (lex + parse + eval) passes with 0 failures; CLI checks cover D4.
|
||||
|
||||
**How to verify:**
|
||||
```bash
|
||||
python -m unittest -q
|
||||
```
|
||||
|
||||
**Expected:**
|
||||
```
|
||||
Ran 68 tests in ~0.2s
|
||||
|
||||
OK
|
||||
```
|
||||
|
||||
All 68 tests pass: 24 lex + 22 parse + 22 eval (including 6 CLI subprocess tests).
|
||||
|
||||
**Commit sha:** (see latest commit after push)
|
||||
|
||||
**Where:** `calc/test_lexer.py`, `calc/test_parser.py`, `calc/test_evaluator.py`
|
||||
103
calculators/builder-adversary/run-02/machine-docs/STATUS-lex.md
Normal file
103
calculators/builder-adversary/run-02/machine-docs/STATUS-lex.md
Normal file
@ -0,0 +1,103 @@
|
||||
# STATUS — phase lex (Builder)
|
||||
|
||||
## DONE
|
||||
|
||||
All DoD gates Adversary-verified PASS. Phase complete.
|
||||
|
||||
## Gates
|
||||
|
||||
| Gate | Status |
|
||||
|------|--------|
|
||||
| D1 — numbers | **PASS** (Adversary @2026-06-15T00:36Z) |
|
||||
| D2 — operators & parens | **PASS** (Adversary @2026-06-15T00:36Z) |
|
||||
| D3 — whitespace & errors | **PASS** (Adversary @2026-06-15T00:36Z) |
|
||||
| D4 — tests green | **PASS** (Adversary @2026-06-15T00:36Z) |
|
||||
|
||||
## Post-verification fix
|
||||
|
||||
**AF-01 addressed:** Wrapped `float(raw)` in `try/except ValueError` to re-raise as `LexError` for malformed number literals like `1.2.3`, `.`, `..`. 24 tests still pass.
|
||||
|
||||
---
|
||||
|
||||
## Claim: D1 — numbers
|
||||
|
||||
**WHAT:** `calc/lexer.py::tokenize` correctly tokenizes integers and floats to NUMBER tokens with numeric Python values (int for integers, float for floats). EOF is always the final token.
|
||||
|
||||
**HOW to verify:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; t=tokenize('42'); assert t[0].kind=='NUMBER' and t[0].value==42 and isinstance(t[0].value,int) and t[1].kind=='EOF', t"
|
||||
python -c "from calc.lexer import tokenize; t=tokenize('3.14'); assert t[0].kind=='NUMBER' and abs(t[0].value-3.14)<1e-9 and isinstance(t[0].value,float), t"
|
||||
python -c "from calc.lexer import tokenize; t=tokenize('.5'); assert t[0].value==0.5, t"
|
||||
python -c "from calc.lexer import tokenize; t=tokenize('10.'); assert t[0].value==10.0, t"
|
||||
```
|
||||
|
||||
**EXPECTED:** All assertions pass (exit 0).
|
||||
|
||||
**WHERE:** `calc/lexer.py`
|
||||
|
||||
---
|
||||
|
||||
## Claim: D2 — operators & parens
|
||||
|
||||
**WHAT:** `+`, `-`, `*`, `/`, `(`, `)` each tokenize to PLUS, MINUS, STAR, SLASH, LPAREN, RPAREN respectively. `tokenize("1+2*3")` → NUMBER PLUS NUMBER STAR NUMBER EOF.
|
||||
|
||||
**HOW to verify:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; k=[t.kind for t in tokenize('1+2*3')]; assert k==['NUMBER','PLUS','NUMBER','STAR','NUMBER','EOF'], k"
|
||||
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
|
||||
```
|
||||
|
||||
**EXPECTED:**
|
||||
- First command: exit 0 (assertion passes)
|
||||
- Second command prints: `[('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]`
|
||||
|
||||
**WHERE:** `calc/lexer.py`
|
||||
|
||||
---
|
||||
|
||||
## Claim: D3 — whitespace & errors
|
||||
|
||||
**WHAT:** Spaces and tabs between tokens are skipped. Invalid characters (letters, `@`, `$`, etc.) raise `LexError` with the offending char and its position in the message.
|
||||
|
||||
**HOW to verify:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; k=[t.kind for t in tokenize(' 12 + 3 ')]; assert k==['NUMBER','PLUS','NUMBER','EOF'], k"
|
||||
python -c "
|
||||
from calc.lexer import tokenize, LexError
|
||||
try:
|
||||
tokenize('1 @ 2')
|
||||
assert False, 'should have raised'
|
||||
except LexError as e:
|
||||
assert '@' in str(e), str(e)
|
||||
assert '2' in str(e), str(e)
|
||||
print('OK')
|
||||
"
|
||||
```
|
||||
|
||||
**EXPECTED:**
|
||||
- First command: exit 0
|
||||
- Second command prints: `OK`
|
||||
|
||||
**WHERE:** `calc/lexer.py`
|
||||
|
||||
---
|
||||
|
||||
## Claim: D4 — tests green
|
||||
|
||||
**WHAT:** `calc/test_lexer.py` passes under `python -m unittest` with 0 failures (24 tests).
|
||||
|
||||
**HOW to verify:**
|
||||
```bash
|
||||
python -m unittest -q
|
||||
```
|
||||
|
||||
**EXPECTED:**
|
||||
```
|
||||
----------------------------------------------------------------------
|
||||
Ran 24 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
Exit code 0.
|
||||
|
||||
**WHERE:** `calc/test_lexer.py`, `calc/lexer.py`
|
||||
@ -0,0 +1,114 @@
|
||||
# STATUS-parse — Builder
|
||||
|
||||
## DONE
|
||||
|
||||
All gates D1–D6 verified PASS by Adversary @2026-06-15T00:50Z. See REVIEW-parse.md.
|
||||
|
||||
---
|
||||
|
||||
## Gate: D1–D6 CLAIMED → PASS
|
||||
|
||||
All six gates are implemented and verified locally.
|
||||
|
||||
---
|
||||
|
||||
## Files
|
||||
|
||||
| File | Description |
|
||||
|---|---|
|
||||
| `calc/parser.py` | Recursive-descent parser exposing `parse(tokens) -> Node` |
|
||||
| `calc/test_parser.py` | unittest suite covering D1–D6 |
|
||||
|
||||
---
|
||||
|
||||
## AST Node Types (stable shape for evaluator)
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Any # int or float from lexer
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str # '+', '-', '*', '/'
|
||||
left: Any # Node
|
||||
right: Any # Node
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str # '-'
|
||||
operand: Any # Node
|
||||
```
|
||||
|
||||
All three are dataclasses with `__repr__` — equality comparison works via `==`.
|
||||
|
||||
---
|
||||
|
||||
## Verification commands (cold-runnable from any clone)
|
||||
|
||||
```bash
|
||||
# D6 — all tests green
|
||||
python -m unittest -q
|
||||
|
||||
# D1 — precedence: 1+2*3 => BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))
|
||||
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)))
|
||||
|
||||
# D1 — precedence: 2*3+1 => BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))
|
||||
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))
|
||||
|
||||
# D2 — left assoc subtraction: 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-3-2')))"
|
||||
# expected: BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))
|
||||
|
||||
# D2 — left assoc division: 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('8/4/2')))"
|
||||
# expected: BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))
|
||||
|
||||
# D3 — parens override: (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('(1+2)*3')))"
|
||||
# expected: BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))
|
||||
|
||||
# D4 — unary minus: -5 => Unary('-', Num(5))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5')))"
|
||||
# expected: Unary('-', Num(5))
|
||||
|
||||
# D4 — unary in paren: -(1+2) => Unary('-', BinOp('+', Num(1), Num(2)))
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-(1+2)')))"
|
||||
# expected: Unary('-', BinOp('+', Num(1), Num(2)))
|
||||
|
||||
# D4 — unary in mul: 3 * -2 => BinOp('*', Num(3), Unary('-', 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 — each raises ParseError (must NOT raise any other exception)
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
|
||||
for s in ['1 +', '(1', '1 2', ')(', '']:
|
||||
try: parse(tokenize(s)); print(f'FAIL no error for {s!r}')
|
||||
except ParseError as e: print(f'OK {s!r} => ParseError: {e}')
|
||||
except Exception as e: print(f'FAIL wrong exc for {s!r}: {type(e).__name__}: {e}')
|
||||
"
|
||||
# expected: 5 lines each starting "OK"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Expected outputs (exact)
|
||||
|
||||
| Gate | Expression | Expected repr |
|
||||
|---|---|---|
|
||||
| D1 | `1+2*3` | `BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))` |
|
||||
| D1 | `2*3+1` | `BinOp('+', BinOp('*', Num(2), Num(3)), Num(1))` |
|
||||
| D2 | `8-3-2` | `BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))` |
|
||||
| D2 | `8/4/2` | `BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))` |
|
||||
| D3 | `(1+2)*3` | `BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))` |
|
||||
| D4 | `-5` | `Unary('-', Num(5))` |
|
||||
| D4 | `-(1+2)` | `Unary('-', BinOp('+', Num(1), Num(2)))` |
|
||||
| D4 | `3 * -2` | `BinOp('*', Num(3), Unary('-', Num(2)))` |
|
||||
| D5 | `1 +` | `ParseError: unexpected token 'EOF' (None)` |
|
||||
| D5 | `(1` | `ParseError: expected RPAREN, got 'EOF' (None)` |
|
||||
| D5 | `1 2` | `ParseError: unexpected token 'NUMBER' (2) after expression` |
|
||||
| D5 | `)(` | `ParseError: unexpected token 'RPAREN' (')')` |
|
||||
| D5 | `` | `ParseError: empty expression` |
|
||||
| D6 | `python -m unittest -q` | `Ran 46 tests in …s OK` |
|
||||
Reference in New Issue
Block a user