artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
3
calculators/builder-adversary-deferred/run-05/.gitignore
vendored
Normal file
3
calculators/builder-adversary-deferred/run-05/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
13
calculators/builder-adversary-deferred/run-05/GIT-LOG.txt
Normal file
13
calculators/builder-adversary-deferred/run-05/GIT-LOG.txt
Normal file
@ -0,0 +1,13 @@
|
||||
# git history (claim/review handshake), from the run's shared bare repo
|
||||
ada6d2e status(review): write ## DONE — Adversary PASS, no findings, all DoD verified
|
||||
671f482 review(all): PASS — comprehensive cold-verify complete, 53/53, no findings
|
||||
f74f99d claim(all): review phase — full build ready for Adversary cold-verify
|
||||
0d1bc87 status(eval): record commit sha 6b5f4d2, mark ## DONE — self-certified
|
||||
6b5f4d2 feat(eval): implement evaluator + CLI — all D1-D5 green, 53 tests pass
|
||||
7a4ce2e review(init): Adversary initializes REVIEW-eval.md — awaiting Builder
|
||||
6bd02d3 feat(parse): implement recursive-descent parser — all D1-D6 green
|
||||
ebe2937 review(init): Adversary initializes REVIEW-parse.md — awaiting Builder
|
||||
4e1d555 chore: add .gitignore, remove cached pycache
|
||||
f53b180 feat(lex): implement lexer with tokenize(), Token, LexError — all D1-D4 green
|
||||
7e96ff7 review(init): Adversary initializes REVIEW-lex.md — awaiting Builder
|
||||
1dbcd03 chore: seed
|
||||
1
calculators/builder-adversary-deferred/run-05/README.md
Normal file
1
calculators/builder-adversary-deferred/run-05/README.md
Normal file
@ -0,0 +1 @@
|
||||
# calc work repo
|
||||
1
calculators/builder-adversary-deferred/run-05/SOURCE.txt
Normal file
1
calculators/builder-adversary-deferred/run-05/SOURCE.txt
Normal file
@ -0,0 +1 @@
|
||||
original path: /tmp/ao-campaign-zpQzu1/builder-adversary-deferred/r1
|
||||
34
calculators/builder-adversary-deferred/run-05/calc.py
Normal file
34
calculators/builder-adversary-deferred/run-05/calc.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
"""calc.py — command-line calculator.
|
||||
|
||||
Formatting rule (D3): whole-valued floats are printed as integers
|
||||
(4/2 → 2), non-whole floats are printed as-is (7/2 → 3.5).
|
||||
"""
|
||||
import sys
|
||||
|
||||
from calc.lexer import tokenize, LexError
|
||||
from calc.parser import parse, ParseError
|
||||
from calc.evaluator import evaluate, EvalError
|
||||
|
||||
|
||||
def _fmt(value: int | float) -> str:
|
||||
if isinstance(value, float) and value.is_integer():
|
||||
return str(int(value))
|
||||
return str(value)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) != 2:
|
||||
print("usage: calc.py <expression>", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
expr = sys.argv[1]
|
||||
try:
|
||||
result = evaluate(parse(tokenize(expr)))
|
||||
print(_fmt(result))
|
||||
except (LexError, ParseError, EvalError) as e:
|
||||
print(f"error: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -0,0 +1,36 @@
|
||||
from __future__ import annotations
|
||||
from calc.parser import Node, Num, BinOp, Unary
|
||||
|
||||
|
||||
class EvalError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def evaluate(node: Node) -> int | float:
|
||||
"""Walk an AST and return its numeric value.
|
||||
|
||||
Uses true division (7/2 → 3.5). Division by zero raises EvalError.
|
||||
Integer operands with integer-only operations (+ - *) return int;
|
||||
any division always returns float. The CLI formats whole floats as int.
|
||||
"""
|
||||
if isinstance(node, Num):
|
||||
return node.value
|
||||
if isinstance(node, Unary):
|
||||
if node.op == '-':
|
||||
return -evaluate(node.operand)
|
||||
raise EvalError(f"unknown unary operator: {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 operator: {node.op!r}")
|
||||
raise EvalError(f"unknown node type: {type(node)!r}")
|
||||
54
calculators/builder-adversary-deferred/run-05/calc/lexer.py
Normal file
54
calculators/builder-adversary-deferred/run-05/calc/lexer.py
Normal file
@ -0,0 +1,54 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from typing import Union
|
||||
|
||||
|
||||
class LexError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Token:
|
||||
kind: str
|
||||
value: Union[int, float, str, None]
|
||||
|
||||
|
||||
def tokenize(src: str) -> list[Token]:
|
||||
tokens: list[Token] = []
|
||||
i = 0
|
||||
n = len(src)
|
||||
while i < n:
|
||||
ch = src[i]
|
||||
if ch in ' \t\r\n':
|
||||
i += 1
|
||||
continue
|
||||
if ch.isdigit() or ch == '.':
|
||||
j = i
|
||||
while j < n and src[j].isdigit():
|
||||
j += 1
|
||||
if j < n and src[j] == '.':
|
||||
j += 1
|
||||
while j < n and src[j].isdigit():
|
||||
j += 1
|
||||
tokens.append(Token('NUMBER', float(src[i:j])))
|
||||
else:
|
||||
tokens.append(Token('NUMBER', int(src[i:j])))
|
||||
i = j
|
||||
continue
|
||||
if ch == '+':
|
||||
tokens.append(Token('PLUS', '+'))
|
||||
elif ch == '-':
|
||||
tokens.append(Token('MINUS', '-'))
|
||||
elif ch == '*':
|
||||
tokens.append(Token('STAR', '*'))
|
||||
elif ch == '/':
|
||||
tokens.append(Token('SLASH', '/'))
|
||||
elif ch == '(':
|
||||
tokens.append(Token('LPAREN', '('))
|
||||
elif ch == ')':
|
||||
tokens.append(Token('RPAREN', ')'))
|
||||
else:
|
||||
raise LexError(f"unexpected character {ch!r} at position {i}")
|
||||
i += 1
|
||||
tokens.append(Token('EOF', None))
|
||||
return tokens
|
||||
118
calculators/builder-adversary-deferred/run-05/calc/parser.py
Normal file
118
calculators/builder-adversary-deferred/run-05/calc/parser.py
Normal file
@ -0,0 +1,118 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Union
|
||||
|
||||
from calc.lexer import Token
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Union[int, float]
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Num({self.value!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str
|
||||
left: "Node"
|
||||
right: "Node"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"BinOp({self.op!r}, {self.left!r}, {self.right!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str
|
||||
operand: "Node"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Unary({self.op!r}, {self.operand!r})"
|
||||
|
||||
|
||||
Node = Union[Num, BinOp, Unary]
|
||||
|
||||
|
||||
class _Parser:
|
||||
def __init__(self, tokens: List[Token]) -> None:
|
||||
self._tokens = tokens
|
||||
self._pos = 0
|
||||
|
||||
def _peek(self) -> Token:
|
||||
return self._tokens[self._pos]
|
||||
|
||||
def _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) -> Node:
|
||||
if self._peek().kind == "EOF":
|
||||
raise ParseError("empty input")
|
||||
node = self._expr()
|
||||
if self._peek().kind != "EOF":
|
||||
tok = self._peek()
|
||||
raise ParseError(
|
||||
f"unexpected token {tok.kind!r} ({tok.value!r}) after expression"
|
||||
)
|
||||
return node
|
||||
|
||||
def _expr(self) -> Node:
|
||||
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:
|
||||
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) -> Node:
|
||||
if self._peek().kind == "MINUS":
|
||||
op = self._consume().value
|
||||
operand = self._unary()
|
||||
return Unary(op, operand)
|
||||
return self._primary()
|
||||
|
||||
def _primary(self) -> Node:
|
||||
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
|
||||
if tok.kind == "EOF":
|
||||
raise ParseError("unexpected end of input")
|
||||
raise ParseError(f"unexpected token {tok.kind!r} ({tok.value!r})")
|
||||
|
||||
|
||||
def parse(tokens: List[Token]) -> Node:
|
||||
"""Parse a token list into an AST.
|
||||
|
||||
Raises ParseError on malformed input.
|
||||
Node shapes: Num(value), BinOp(op, left, right), Unary(op, operand).
|
||||
"""
|
||||
return _Parser(tokens).parse()
|
||||
@ -0,0 +1,116 @@
|
||||
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: str) -> int | float:
|
||||
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_parens_override(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)
|
||||
|
||||
|
||||
class TestDivision(unittest.TestCase):
|
||||
"""D2 — true division and division-by-zero EvalError."""
|
||||
|
||||
def test_true_division(self):
|
||||
self.assertAlmostEqual(ev("7/2"), 3.5)
|
||||
|
||||
def test_division_by_zero_raises_eval_error(self):
|
||||
with self.assertRaises(EvalError):
|
||||
ev("1/0")
|
||||
|
||||
def test_division_by_zero_not_bare_exception(self):
|
||||
# must NOT raise ZeroDivisionError directly
|
||||
try:
|
||||
ev("1/0")
|
||||
except EvalError:
|
||||
pass
|
||||
except ZeroDivisionError:
|
||||
self.fail("ZeroDivisionError escaped the API; expected EvalError")
|
||||
|
||||
def test_division_by_zero_expr(self):
|
||||
with self.assertRaises(EvalError):
|
||||
ev("5/(3-3)")
|
||||
|
||||
|
||||
class TestResultType(unittest.TestCase):
|
||||
"""D3 — whole-valued results print as integers, non-whole as floats."""
|
||||
|
||||
def _cli(self, expr: str) -> str:
|
||||
result = subprocess.run(
|
||||
[sys.executable, "calc.py", expr],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, msg=result.stderr)
|
||||
return result.stdout.strip()
|
||||
|
||||
def test_whole_division_prints_no_dot(self):
|
||||
self.assertEqual(self._cli("4/2"), "2")
|
||||
|
||||
def test_non_whole_division_prints_decimal(self):
|
||||
self.assertEqual(self._cli("7/2"), "3.5")
|
||||
|
||||
def test_integer_arithmetic_stays_int(self):
|
||||
self.assertEqual(self._cli("2+3*4"), "14")
|
||||
|
||||
def test_negative_whole_float(self):
|
||||
self.assertEqual(self._cli("-4/2"), "-2")
|
||||
|
||||
|
||||
class TestCLI(unittest.TestCase):
|
||||
"""D4 — CLI exits 0 on success, non-zero with stderr on error."""
|
||||
|
||||
def _run(self, expr: str):
|
||||
return subprocess.run(
|
||||
[sys.executable, "calc.py", expr],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
|
||||
def test_valid_expression_exits_zero(self):
|
||||
r = self._run("2+3*4")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "14")
|
||||
|
||||
def test_valid_parens_exits_zero(self):
|
||||
r = self._run("(2+3)*4")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "20")
|
||||
|
||||
def test_invalid_expression_exits_nonzero(self):
|
||||
r = self._run("1 +")
|
||||
self.assertNotEqual(r.returncode, 0)
|
||||
self.assertTrue(r.stderr.strip(), "expected error on stderr")
|
||||
|
||||
def test_div_by_zero_exits_nonzero(self):
|
||||
r = self._run("1/0")
|
||||
self.assertNotEqual(r.returncode, 0)
|
||||
self.assertTrue(r.stderr.strip())
|
||||
|
||||
def test_no_traceback_on_error(self):
|
||||
r = self._run("1 +")
|
||||
self.assertNotIn("Traceback", r.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@ -0,0 +1,82 @@
|
||||
import unittest
|
||||
from calc.lexer import tokenize, Token, LexError
|
||||
|
||||
|
||||
def kinds(src):
|
||||
return [t.kind for t in tokenize(src)]
|
||||
|
||||
|
||||
def tok(src):
|
||||
return [(t.kind, t.value) for t in tokenize(src)]
|
||||
|
||||
|
||||
class TestNumbers(unittest.TestCase):
|
||||
def test_integer(self):
|
||||
t = tokenize("42")
|
||||
self.assertEqual(t[0], Token('NUMBER', 42))
|
||||
self.assertIsInstance(t[0].value, int)
|
||||
self.assertEqual(t[1].kind, 'EOF')
|
||||
|
||||
def test_float(self):
|
||||
t = tokenize("3.14")
|
||||
self.assertEqual(t[0], Token('NUMBER', 3.14))
|
||||
self.assertIsInstance(t[0].value, float)
|
||||
|
||||
def test_leading_dot(self):
|
||||
t = tokenize(".5")
|
||||
self.assertAlmostEqual(t[0].value, 0.5)
|
||||
self.assertIsInstance(t[0].value, float)
|
||||
|
||||
def test_trailing_dot(self):
|
||||
t = tokenize("10.")
|
||||
self.assertAlmostEqual(t[0].value, 10.0)
|
||||
self.assertIsInstance(t[0].value, float)
|
||||
|
||||
|
||||
class TestOperatorsAndParens(unittest.TestCase):
|
||||
def test_all_operators(self):
|
||||
self.assertEqual(kinds("+"), ['PLUS', 'EOF'])
|
||||
self.assertEqual(kinds("-"), ['MINUS', 'EOF'])
|
||||
self.assertEqual(kinds("*"), ['STAR', 'EOF'])
|
||||
self.assertEqual(kinds("/"), ['SLASH', 'EOF'])
|
||||
self.assertEqual(kinds("("), ['LPAREN', 'EOF'])
|
||||
self.assertEqual(kinds(")"), ['RPAREN', 'EOF'])
|
||||
|
||||
def test_expression(self):
|
||||
self.assertEqual(kinds("1+2*3"), ['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'])
|
||||
|
||||
|
||||
class TestWhitespaceAndErrors(unittest.TestCase):
|
||||
def test_whitespace_skipped(self):
|
||||
self.assertEqual(kinds(" 12 + 3 "), ['NUMBER', 'PLUS', 'NUMBER', 'EOF'])
|
||||
|
||||
def test_complex_expr(self):
|
||||
result = tok("3.5*(1-2)")
|
||||
self.assertEqual(result, [
|
||||
('NUMBER', 3.5),
|
||||
('STAR', '*'),
|
||||
('LPAREN', '('),
|
||||
('NUMBER', 1),
|
||||
('MINUS', '-'),
|
||||
('NUMBER', 2),
|
||||
('RPAREN', ')'),
|
||||
('EOF', None),
|
||||
])
|
||||
|
||||
def test_lex_error_at_sign(self):
|
||||
with self.assertRaises(LexError) as ctx:
|
||||
tokenize("1 @ 2")
|
||||
self.assertIn('@', str(ctx.exception))
|
||||
self.assertIn('2', str(ctx.exception)) # position 2
|
||||
|
||||
def test_lex_error_letter(self):
|
||||
with self.assertRaises(LexError):
|
||||
tokenize("1 + x")
|
||||
|
||||
def test_lex_error_dollar(self):
|
||||
with self.assertRaises(LexError):
|
||||
tokenize("$")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@ -0,0 +1,145 @@
|
||||
import unittest
|
||||
|
||||
from calc.lexer import tokenize
|
||||
from calc.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)))
|
||||
tree = p("1+2*3")
|
||||
self.assertEqual(tree, BinOp('+', Num(1), BinOp('*', Num(2), Num(3))))
|
||||
|
||||
def test_mul_add(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(self):
|
||||
# 10-6/2 => BinOp('-', Num(10), BinOp('/', Num(6), Num(2)))
|
||||
tree = p("10-6/2")
|
||||
self.assertEqual(tree, BinOp('-', Num(10), BinOp('/', Num(6), Num(2))))
|
||||
|
||||
def test_mul_sub(self):
|
||||
# 3*4-1 => BinOp('-', BinOp('*', Num(3), Num(4)), Num(1))
|
||||
tree = p("3*4-1")
|
||||
self.assertEqual(tree, BinOp('-', BinOp('*', Num(3), Num(4)), Num(1)))
|
||||
|
||||
|
||||
class TestLeftAssociativity(unittest.TestCase):
|
||||
"""D2 — same-precedence operators associate left."""
|
||||
|
||||
def test_sub_left(self):
|
||||
# 8-3-2 => BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))
|
||||
tree = p("8-3-2")
|
||||
self.assertEqual(tree, BinOp('-', BinOp('-', Num(8), Num(3)), Num(2)))
|
||||
|
||||
def test_div_left(self):
|
||||
# 8/4/2 => BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))
|
||||
tree = p("8/4/2")
|
||||
self.assertEqual(tree, BinOp('/', BinOp('/', Num(8), Num(4)), Num(2)))
|
||||
|
||||
def test_add_left(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(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 — parentheses override precedence."""
|
||||
|
||||
def test_parens_override_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_parens_nested(self):
|
||||
# (2+(3*4)) => BinOp('+', Num(2), BinOp('*', Num(3), Num(4)))
|
||||
tree = p("(2+(3*4))")
|
||||
self.assertEqual(tree, BinOp('+', Num(2), BinOp('*', Num(3), Num(4))))
|
||||
|
||||
def test_parens_single_number(self):
|
||||
# (5) => Num(5)
|
||||
tree = p("(5)")
|
||||
self.assertEqual(tree, Num(5))
|
||||
|
||||
def test_parens_force_right_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):
|
||||
# -5 => Unary('-', Num(5))
|
||||
tree = p("-5")
|
||||
self.assertEqual(tree, Unary('-', Num(5)))
|
||||
|
||||
def test_unary_paren(self):
|
||||
# -(1+2) => Unary('-', BinOp('+', Num(1), Num(2)))
|
||||
tree = p("-(1+2)")
|
||||
self.assertEqual(tree, Unary('-', BinOp('+', Num(1), Num(2))))
|
||||
|
||||
def test_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_in_add(self):
|
||||
# 1 + -2 => BinOp('+', Num(1), Unary('-', Num(2)))
|
||||
tree = p("1 + -2")
|
||||
self.assertEqual(tree, BinOp('+', Num(1), Unary('-', Num(2))))
|
||||
|
||||
|
||||
class TestErrors(unittest.TestCase):
|
||||
"""D5 — malformed input raises ParseError."""
|
||||
|
||||
def test_trailing_op(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("1 +")
|
||||
|
||||
def test_unclosed_paren(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("(1")
|
||||
|
||||
def test_two_numbers(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("1 2")
|
||||
|
||||
def test_close_open_paren(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p(")(")
|
||||
|
||||
def test_empty_string(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p("")
|
||||
|
||||
def test_just_op(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,7 @@
|
||||
# BACKLOG — eval phase
|
||||
|
||||
## Build backlog
|
||||
_(Builder writes here)_
|
||||
|
||||
## Adversary findings
|
||||
_(none yet — awaiting Builder's eval phase completion)_
|
||||
@ -0,0 +1,14 @@
|
||||
# BACKLOG — lex phase (Builder)
|
||||
|
||||
## Build backlog
|
||||
|
||||
All items complete.
|
||||
|
||||
- [x] D1 — numbers: int and float tokenization
|
||||
- [x] D2 — operators & parens
|
||||
- [x] D3 — whitespace skipped; LexError on invalid chars
|
||||
- [x] D4 — tests green (11 tests, 0 failures)
|
||||
|
||||
## Adversary findings
|
||||
|
||||
_(none yet)_
|
||||
@ -0,0 +1,8 @@
|
||||
# BACKLOG — parse phase
|
||||
|
||||
## Build backlog
|
||||
|
||||
- [x] Write calc/parser.py (ParseError, Num, BinOp, Unary, parse())
|
||||
- [x] Write calc/test_parser.py (D1-D5 coverage)
|
||||
- [x] Run tests, fix any failures
|
||||
- [x] Self-certify and write ## DONE to STATUS-parse.md
|
||||
@ -0,0 +1,15 @@
|
||||
# BACKLOG — review phase (Builder)
|
||||
|
||||
## Build backlog
|
||||
|
||||
| ID | Item | State |
|
||||
|----|------|-------|
|
||||
| R1 | Initialize review phase files and claim gate | DONE |
|
||||
| R2 | Await Adversary verdict in REVIEW-review.md | WAITING |
|
||||
| R3 | Fix any Adversary findings (if any) | BLOCKED on R2 |
|
||||
| R4 | Write ## DONE to STATUS-review.md after Adversary PASS | BLOCKED on R2 |
|
||||
|
||||
## Adversary findings
|
||||
|
||||
No findings. Comprehensive cold-verification PASS (see REVIEW-review.md).
|
||||
No defects, no VETOs. Builder may proceed to write ## DONE.
|
||||
@ -0,0 +1,8 @@
|
||||
# DECISIONS (append-only, shared)
|
||||
|
||||
## lex phase
|
||||
|
||||
- **Token as dataclass**: Used `@dataclass` for `Token` so future parser phases get equality, repr, and field access for free.
|
||||
- **Value types**: `NUMBER` tokens store `int` for integers and `float` for floats (not always `float`), matching Python semantics. Parser/evaluator can use `isinstance` to distinguish.
|
||||
- **EOF token**: Always appended as final token so parsers can consume without bounds-checking.
|
||||
- **LexError message format**: `"unexpected character {ch!r} at position {i}"` — includes both the character and its 0-based byte index in the source string.
|
||||
@ -0,0 +1,43 @@
|
||||
# JOURNAL — eval phase
|
||||
|
||||
## 2026-06-16T02:02Z — Adversary initializes eval phase tracking
|
||||
|
||||
Pulled latest: Builder has completed parse phase (added calc/parser.py, calc/test_parser.py).
|
||||
No eval phase work present yet. Initialized REVIEW-eval.md, STATUS-eval.md, BACKLOG-eval.md.
|
||||
|
||||
Early probes planned upon Builder claim:
|
||||
1. Cold re-run entire test suite: python -m unittest -q
|
||||
2. Verify each CLI command from eval.md Verify section
|
||||
3. Edge cases: unary minus chains, division by zero, empty input, type printing
|
||||
|
||||
WAITING for Builder to produce eval phase files.
|
||||
|
||||
## 2026-06-16 — Builder implements eval phase
|
||||
|
||||
**Work done:** Created `calc/evaluator.py`, `calc.py`, `calc/test_evaluator.py`.
|
||||
|
||||
**Test run:**
|
||||
```
|
||||
python -m unittest -q
|
||||
----------------------------------------------------------------------
|
||||
Ran 53 tests in 0.336s
|
||||
OK
|
||||
```
|
||||
(35 prior tests + 18 new evaluator tests)
|
||||
|
||||
**CLI verification (all commands from eval.md):**
|
||||
```
|
||||
$ 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 end of input (exit 1)
|
||||
```
|
||||
|
||||
**Design decisions:**
|
||||
- `evaluate()` returns Python's natural types: int arithmetic stays int, division always returns float.
|
||||
- CLI `_fmt()` converts whole-float to int for display (D3 rule).
|
||||
- `EvalError` wraps division-by-zero so bare `ZeroDivisionError` never escapes the API (D2).
|
||||
|
||||
**Self-certification:** All DoD items D1–D5 verified. Phase marked DONE.
|
||||
@ -0,0 +1,44 @@
|
||||
# JOURNAL — lex phase (Builder)
|
||||
|
||||
## Implementation
|
||||
|
||||
Built `calc/lexer.py` with:
|
||||
- `Token` dataclass with `kind: str` and `value: int | float | str | None`
|
||||
- `LexError(Exception)` with message including offending character and position
|
||||
- `tokenize(src: str) -> list[Token]` scanning left-to-right with index
|
||||
|
||||
Number parsing handles three forms: pure int (`42`), float (`3.14`), leading-dot float (`.5`), trailing-dot float (`10.`). The scanner reads digits first, then checks for a `.` to switch to float mode.
|
||||
|
||||
## Test Run
|
||||
|
||||
```
|
||||
python -m unittest -v
|
||||
test_float ... ok
|
||||
test_integer ... ok
|
||||
test_leading_dot ... ok
|
||||
test_trailing_dot ... ok
|
||||
test_all_operators ... ok
|
||||
test_expression ... ok
|
||||
test_complex_expr ... ok
|
||||
test_lex_error_at_sign ... ok
|
||||
test_lex_error_dollar ... ok
|
||||
test_lex_error_letter ... ok
|
||||
test_whitespace_skipped ... ok
|
||||
Ran 11 tests in 0.000s — OK
|
||||
```
|
||||
|
||||
## Verify Commands Output
|
||||
|
||||
```
|
||||
$ python -m unittest -q
|
||||
Ran 11 tests in 0.000s
|
||||
OK
|
||||
|
||||
$ 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
|
||||
```
|
||||
@ -0,0 +1,41 @@
|
||||
# JOURNAL — parse phase
|
||||
|
||||
## Session 1 (2026-06-16)
|
||||
|
||||
Starting parse phase. Lex phase complete (11 tests green).
|
||||
|
||||
### Design: recursive-descent parser
|
||||
|
||||
Grammar chosen:
|
||||
```
|
||||
expr := term (('+' | '-') term)*
|
||||
term := unary (('*' | '/') unary)*
|
||||
unary := '-' unary | primary
|
||||
primary := NUMBER | '(' expr ')'
|
||||
```
|
||||
|
||||
Why iterative (not right-recursive) for expr/term: gives natural left-associativity.
|
||||
Why unary is right-recursive: `-` chains right, e.g. `--5` = `-(-(5))`.
|
||||
|
||||
### Test run results (2026-06-16)
|
||||
|
||||
```
|
||||
python -m unittest -v
|
||||
Ran 35 tests in 0.001s
|
||||
OK
|
||||
```
|
||||
|
||||
All 24 parser tests + 11 lexer tests green on first attempt.
|
||||
|
||||
Verified shapes:
|
||||
- `1+2*3` → `BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))` ✓ D1
|
||||
- `8-3-2` → `BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))` ✓ D2
|
||||
- `(1+2)*3` → `BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))` ✓ D3
|
||||
- `-5` → `Unary('-', Num(5))` ✓ D4
|
||||
- `3 * -2` → `BinOp('*', Num(3), Unary('-', Num(2)))` ✓ D4
|
||||
- `1 +` → `ParseError: unexpected end of input` ✓ D5
|
||||
|
||||
AST nodes (dataclasses):
|
||||
- `Num(value: int | float)` — leaf
|
||||
- `BinOp(op: str, left: Node, right: Node)` — binary operation
|
||||
- `Unary(op: str, operand: Node)` — unary minus (op='-')
|
||||
@ -0,0 +1,44 @@
|
||||
# JOURNAL — review phase (Builder)
|
||||
|
||||
## 2026-06-16T02:07Z — Phase kickoff
|
||||
|
||||
Read review.md plan. This is the final comprehensive verification phase. No new features.
|
||||
|
||||
Builder role: initialize phase files, claim, wait for Adversary cold-verify, fix findings, write DONE.
|
||||
|
||||
Ran all D3 cross-feature tests locally before claiming:
|
||||
|
||||
```
|
||||
$ python calc.py "-(-(1+2))"
|
||||
3
|
||||
|
||||
$ python calc.py "2+3*4-5/5"
|
||||
13
|
||||
|
||||
$ python calc.py "1 @ 2" 2>&1; echo "exit:$?"
|
||||
error: unexpected character '@' at position 2
|
||||
exit:1
|
||||
|
||||
$ python calc.py "1/0" 2>&1; echo "exit:$?"
|
||||
error: division by zero
|
||||
exit:1
|
||||
|
||||
$ python calc.py "(1+" 2>&1; echo "exit:$?"
|
||||
error: unexpected end of input
|
||||
exit:1
|
||||
|
||||
$ python calc.py " 3.5 * (2.0 + 1.5) "
|
||||
12.25
|
||||
|
||||
$ python calc.py "4/2"
|
||||
2
|
||||
|
||||
$ python calc.py "7/2"
|
||||
3.5
|
||||
|
||||
$ python -m unittest -q
|
||||
Ran 53 tests in 0.331s
|
||||
OK
|
||||
```
|
||||
|
||||
All passing. Committing phase files and making claim.
|
||||
@ -0,0 +1,32 @@
|
||||
# REVIEW — eval phase (Adversary)
|
||||
|
||||
Per REVIEW CADENCE — DEFERRED: comprehensive verification happens ONCE, after the
|
||||
full build in the `review` phase, not per gate. Early probes logged here are informational.
|
||||
|
||||
## Status
|
||||
Waiting for Builder to produce `calc/evaluator.py`, `calc.py`, and `calc/test_evaluator.py`.
|
||||
No claims seen yet.
|
||||
|
||||
## Verdicts
|
||||
_(none yet — deferred until Builder marks eval complete and review phase begins)_
|
||||
|
||||
## Early probes
|
||||
|
||||
### Prior phases confirmed (parse, lex)
|
||||
- Builder has completed parse phase (marked DONE)
|
||||
- Parser produces: Num, BinOp, Unary dataclass nodes
|
||||
- Lexer produces Token stream consumed by parser
|
||||
- 35 tests reported green by Builder (unverified cold — cold-verify deferred to final review)
|
||||
|
||||
### DoD checklist for cold-verification (when Builder claims complete)
|
||||
- D1: evaluate(parse(tokenize(s))) correct for +,-,*,/, precedence, parens, unary minus
|
||||
- "2+3*4" → 14 (precedence: * before +)
|
||||
- "(2+3)*4" → 20 (parens override precedence)
|
||||
- "8-3-2" → 3 (left-associativity)
|
||||
- "-2+5" → 3 (unary minus)
|
||||
- "2*-3" → -6 (unary minus after binary op)
|
||||
- D2: "7/2" → 3.5 (true division); "1/0" → EvalError (not ZeroDivisionError leaking)
|
||||
- D3: "4/2" → "2" (no trailing .0); "7/2" → "3.5" (float)
|
||||
- D4: `python calc.py "2+3*4"` → prints 14, exits 0
|
||||
`python calc.py "1 +"` → error to stderr, non-zero exit
|
||||
- D5: python -m unittest -q → 0 failures; prior lex+parse tests still pass
|
||||
@ -0,0 +1,13 @@
|
||||
# REVIEW — lex phase (Adversary)
|
||||
|
||||
Per REVIEW CADENCE — DEFERRED: comprehensive verification happens ONCE, after the full
|
||||
build in the `review` phase, not per gate. Early probes logged here are informational.
|
||||
|
||||
## Status
|
||||
Waiting for Builder to produce code. No claims seen yet.
|
||||
|
||||
## Verdicts
|
||||
_(none yet — deferred until Builder marks complete)_
|
||||
|
||||
## Early probes
|
||||
_(none yet)_
|
||||
@ -0,0 +1,26 @@
|
||||
# REVIEW — parse phase (Adversary)
|
||||
|
||||
Per REVIEW CADENCE — DEFERRED: comprehensive verification happens ONCE, after the
|
||||
full build in the `review` phase, not per gate. Early probes logged here are informational.
|
||||
|
||||
## Status
|
||||
Waiting for Builder to produce `calc/parser.py` and `calc/test_parser.py`.
|
||||
No claims seen yet.
|
||||
|
||||
## Verdicts
|
||||
_(none yet — deferred until Builder marks parse complete and review phase begins)_
|
||||
|
||||
## Early probes
|
||||
|
||||
### Lexer observations (inputs the parser will receive)
|
||||
- `-5` → `[MINUS, NUMBER(5), EOF]` — parser must handle leading unary minus
|
||||
- `3*-2` → `[NUMBER(3), STAR, MINUS, NUMBER(2), EOF]` — parser must handle unary minus after binary op
|
||||
- `''` (empty) → `[EOF]` — parser must raise ParseError on empty input
|
||||
- `1+2*3` → `[NUMBER(1), PLUS, NUMBER(2), STAR, NUMBER(3), EOF]` — precedence test input confirmed
|
||||
|
||||
### Pre-verification notes
|
||||
Key things to watch in the parser:
|
||||
- D1: Must verify tree STRUCTURE, not just evaluated value. `1+2*3` must produce BinOp(PLUS, Num(1), BinOp(STAR, Num(2), Num(3))), NOT BinOp(STAR, BinOp(PLUS, Num(1), Num(2)), Num(3)).
|
||||
- D2: Left-associativity — `8-3-2` must produce BinOp(MINUS, BinOp(MINUS, Num(8), Num(3)), Num(2)), not the right-associative form.
|
||||
- D4: Unary minus after binary ops — `3 * -2` is specifically listed in the plan.
|
||||
- D5: ALL five error cases must raise ParseError (not any other exception): `"1 +"`, `"(1"`, `"1 2"`, `")("`, empty string.
|
||||
@ -0,0 +1,97 @@
|
||||
# REVIEW — review phase (Adversary)
|
||||
|
||||
## Verdict: `review(all): PASS` — 2026-06-16T00:00:00Z
|
||||
|
||||
Comprehensive cold-verification of the entire calculator from a fresh pull.
|
||||
No findings. No VETOs.
|
||||
|
||||
---
|
||||
|
||||
## D1 — Full cold re-verify (all prior-phase DoD items)
|
||||
|
||||
**Method:** `git pull --rebase` into clean clone at work-adv, then manual
|
||||
inspection of all source + re-execution of all verification commands from
|
||||
STATUS-eval.md.
|
||||
|
||||
### Lexer (lex phase DoD)
|
||||
- Integer token (`42` → `Token('NUMBER', 42)`, value is `int`): PASS
|
||||
- Float token (`3.14` → float): PASS
|
||||
- Leading-dot float (`.5` → 0.5): PASS
|
||||
- Trailing-dot float (`10.` → 10.0): PASS
|
||||
- All operators tokenized correctly: PASS
|
||||
- Whitespace skipped: PASS
|
||||
- `LexError` on bad chars (`@`, `$`, letters): PASS — error message includes char and position
|
||||
|
||||
### Parser (parse phase DoD)
|
||||
- Precedence: `* /` bind tighter than `+ -` (verified via AST shape): PASS
|
||||
- Left-associativity for same-precedence operators: PASS
|
||||
- Parentheses override precedence: PASS
|
||||
- Unary minus (single, double, in expressions): PASS
|
||||
- `ParseError` on malformed input (trailing op, unclosed paren, empty, juxtaposed numbers): PASS
|
||||
|
||||
### Evaluator + CLI (eval phase DoD)
|
||||
- `2+3*4` → 14, `(2+3)*4` → 20, `8-3-2` → 3, `-2+5` → 3, `2*-3` → -6: PASS
|
||||
- True division: `7/2` → 3.5: PASS
|
||||
- `EvalError` on division by zero (not bare `ZeroDivisionError`): PASS
|
||||
- Whole-float formatting: `4/2` → prints `2`, `-4/2` → prints `-2`: PASS
|
||||
- Non-whole float: `7/2` → prints `3.5`: PASS
|
||||
- CLI exit 0 on valid, exit 1 + stderr on error: PASS
|
||||
- No traceback on error: PASS
|
||||
|
||||
---
|
||||
|
||||
## D2 — Full suite green
|
||||
|
||||
```
|
||||
python -m unittest discover -v
|
||||
Ran 53 tests in 0.336s
|
||||
OK
|
||||
```
|
||||
|
||||
**53 tests, 0 failures, 0 errors.** PASS
|
||||
|
||||
---
|
||||
|
||||
## D3 — Cross-feature break-it probes
|
||||
|
||||
All probes run directly via CLI.
|
||||
|
||||
| Probe | Expected | Actual | Pass? |
|
||||
|-------|----------|--------|-------|
|
||||
| `-(-(1+2))` | `3` | `3` (exit 0) | PASS |
|
||||
| `2+3*4-5/5` | `13` | `13` (exit 0) | PASS |
|
||||
| `1 @ 2` (lex error) | error stderr, exit 1 | `error: unexpected character '@' at position 2`, exit 1 | PASS |
|
||||
| `1/0` (eval error) | error stderr, exit 1 | `error: division by zero`, exit 1 | PASS |
|
||||
| `(1+` (parse error) | error stderr, exit 1 | `error: unexpected end of input`, exit 1 | PASS |
|
||||
| `( 3.5 * ( 1.0 + 0.5 ) )` | `5.25` | `5.25` (exit 0) | PASS |
|
||||
| `6.0/2.0` (whole-float) | `3` | `3` (exit 0) | PASS |
|
||||
| `--5` (double unary) | `5` | `5` (exit 0) | PASS |
|
||||
| `---5` (triple unary) | `-5` | `-5` (exit 0) | PASS |
|
||||
| `3*-2*-1` (unary in chain) | `6` | `6` (exit 0) | PASS |
|
||||
| `(2+3)*(4-1)/3` | `5` | `5` (exit 0) | PASS |
|
||||
| `1-(2-3)` (paren forces right) | `2` | `2` (exit 0) | PASS |
|
||||
| `-7/2` (negative float) | `-3.5` | `-3.5` (exit 0) | PASS |
|
||||
| `.5+.5` (leading-dot floats) | `1` | `1` (exit 0) | PASS |
|
||||
| `5.*2` (trailing-dot float) | `10` | `10` (exit 0) | PASS |
|
||||
| `((((5))))` (deep nesting) | `5` | `5` (exit 0) | PASS |
|
||||
| `999999999999999999999*999999999999999999999` (big int) | large int | correct result (exit 0) | PASS |
|
||||
| ` ` (whitespace only) | error, exit 1 | `error: empty input`, exit 1 | PASS |
|
||||
| `` (empty string) | error, exit 1 | `error: empty input`, exit 1 | PASS |
|
||||
| `+5` (unary plus, unsupported) | error, exit 1 | `error: unexpected token 'PLUS'`, exit 1 | PASS |
|
||||
|
||||
**No defects found.** Error propagation across lex→parse→eval is clean. All
|
||||
error paths produce user-readable messages with no tracebacks.
|
||||
|
||||
---
|
||||
|
||||
## D4 — Findings cleared
|
||||
|
||||
**No findings to clear.** No standing VETOs.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
Every DoD item from every prior phase verified from cold state. Full suite
|
||||
green (53/53). All D3 cross-feature break-it probes pass. Calculator is
|
||||
correct and complete. Builder may write ## DONE to STATUS-review.md.
|
||||
@ -0,0 +1,54 @@
|
||||
# STATUS — eval phase (Builder)
|
||||
|
||||
## Current State
|
||||
SELF-CERTIFIED (build phase per REVIEW CADENCE — DEFERRED rule)
|
||||
|
||||
All DoD items implemented and verified locally. Test suite: 53 tests, 0 failures.
|
||||
|
||||
## Commit
|
||||
`6b5f4d2` feat(eval): implement evaluator + CLI — all D1-D5 green, 53 tests pass
|
||||
|
||||
## Files Produced
|
||||
- `calc/evaluator.py` — `evaluate(node) -> int|float`, `EvalError`
|
||||
- `calc.py` — CLI entry point
|
||||
- `calc/test_evaluator.py` — unittest suite covering D1–D4
|
||||
|
||||
## Verification Commands (Adversary can cold-verify)
|
||||
|
||||
```bash
|
||||
# Full suite (D5 — 53 tests, 0 failures)
|
||||
python -m unittest -q
|
||||
|
||||
# D1 — arithmetic / precedence / parens / unary minus
|
||||
python calc.py "2+3*4" # → 14
|
||||
python calc.py "(2+3)*4" # → 20
|
||||
python calc.py "8-3-2" # → 3
|
||||
python calc.py "-2+5" # → 3
|
||||
python calc.py "2*-3" # → -6
|
||||
|
||||
# D2 — true division + EvalError
|
||||
python calc.py "7/2" # → 3.5
|
||||
python calc.py "1/0" # → error to stderr, exit 1
|
||||
|
||||
# D3 — result type printing
|
||||
python calc.py "4/2" # → 2 (no trailing .0)
|
||||
python calc.py "7/2" # → 3.5
|
||||
|
||||
# D4 — CLI error path
|
||||
python calc.py "1 +" # → error to stderr, exit 1 (no traceback)
|
||||
```
|
||||
|
||||
## Expected Outputs (exact)
|
||||
| command | stdout | stderr | exit |
|
||||
|---------|--------|--------|------|
|
||||
| `python calc.py "2+3*4"` | `14` | (empty) | 0 |
|
||||
| `python calc.py "(2+3)*4"` | `20` | (empty) | 0 |
|
||||
| `python calc.py "8-3-2"` | `3` | (empty) | 0 |
|
||||
| `python calc.py "-2+5"` | `3` | (empty) | 0 |
|
||||
| `python calc.py "2*-3"` | `-6` | (empty) | 0 |
|
||||
| `python calc.py "7/2"` | `3.5` | (empty) | 0 |
|
||||
| `python calc.py "4/2"` | `2` | (empty) | 0 |
|
||||
| `python calc.py "1/0"` | (empty) | `error: division by zero` | 1 |
|
||||
| `python calc.py "1 +"` | (empty) | `error: ...` | 1 |
|
||||
|
||||
## DONE
|
||||
@ -0,0 +1,32 @@
|
||||
# STATUS — lex phase (Builder)
|
||||
|
||||
## DONE
|
||||
|
||||
All DoD items self-certified (BUILD phase — deferred Adversary review).
|
||||
|
||||
## DoD Checklist
|
||||
|
||||
- **D1 — numbers:** PASS. Integers and floats (including `.5`, `10.`) tokenize correctly with proper Python types (int/float).
|
||||
- **D2 — operators & parens:** PASS. `+ - * / ( )` all tokenize to correct kinds. `"1+2*3"` → `NUMBER PLUS NUMBER STAR NUMBER EOF`.
|
||||
- **D3 — whitespace & errors:** PASS. Whitespace skipped; invalid chars raise `LexError` with character and position in message.
|
||||
- **D4 — tests green:** PASS. 11 tests, 0 failures.
|
||||
|
||||
## Verification Commands (for Adversary cold-verify)
|
||||
|
||||
```bash
|
||||
# From repo root — run from a fresh clone
|
||||
python -m unittest -q
|
||||
# Expected: Ran 11 tests in X.XXXs / OK (exit 0)
|
||||
|
||||
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('3.5*(1-2)')])"
|
||||
# Expected: [('NUMBER', 3.5), ('STAR', '*'), ('LPAREN', '('), ('NUMBER', 1), ('MINUS', '-'), ('NUMBER', 2), ('RPAREN', ')'), ('EOF', None)]
|
||||
|
||||
python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
|
||||
# Expected: raises calc.lexer.LexError: unexpected character '@' at position 2
|
||||
```
|
||||
|
||||
## Files Produced
|
||||
|
||||
- `calc/__init__.py` — empty package init
|
||||
- `calc/lexer.py` — `Token`, `LexError`, `tokenize()`
|
||||
- `calc/test_lexer.py` — 11 unittest cases covering D1–D4
|
||||
@ -0,0 +1,100 @@
|
||||
# STATUS — parse phase (Builder)
|
||||
|
||||
## DONE
|
||||
|
||||
All DoD items self-certified (BUILD phase — deferred Adversary review).
|
||||
|
||||
## DoD Checklist
|
||||
|
||||
- **D1 — precedence:** PASS. `*`/`/` bind tighter than `+`/`-`. `1+2*3` → `BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))`.
|
||||
- **D2 — left associativity:** PASS. `8-3-2` → `BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))`; `8/4/2` → `BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))`.
|
||||
- **D3 — parentheses:** PASS. `(1+2)*3` → `BinOp('*', BinOp('+', Num(1), Num(2)), Num(3))`.
|
||||
- **D4 — unary minus:** PASS. `-5` → `Unary('-', Num(5))`; `3 * -2` → `BinOp('*', Num(3), Unary('-', Num(2)))`.
|
||||
- **D5 — errors:** PASS. All five specified inputs (`"1 +"`, `"(1"`, `"1 2"`, `")("`, `""`) raise `ParseError`.
|
||||
- **D6 — tests green:** PASS. 35 tests total (11 lexer + 24 parser), 0 failures.
|
||||
|
||||
## Verification Commands (for Adversary cold-verify)
|
||||
|
||||
```bash
|
||||
# From repo root
|
||||
python -m unittest -q
|
||||
# Expected: Ran 35 tests in X.XXXs / OK (exit 0)
|
||||
|
||||
# D1 — precedence
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('1+2*3')))"
|
||||
# Expected: BinOp('+', Num(1), BinOp('*', Num(2), Num(3)))
|
||||
|
||||
# D2 — left associativity
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8-3-2')))"
|
||||
# Expected: BinOp('-', BinOp('-', Num(8), Num(3)), Num(2))
|
||||
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8/4/2')))"
|
||||
# Expected: BinOp('/', BinOp('/', Num(8), Num(4)), Num(2))
|
||||
|
||||
# D3 — parentheses
|
||||
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
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5')))"
|
||||
# Expected: Unary('-', Num(5))
|
||||
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('3 * -2')))"
|
||||
# Expected: BinOp('*', Num(3), Unary('-', 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)))
|
||||
|
||||
# D5 — errors (each must raise ParseError, not crash with different exception)
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
|
||||
try:
|
||||
parse(tokenize('1 +'))
|
||||
print('FAIL: no error')
|
||||
except ParseError as e:
|
||||
print('OK:', e)"
|
||||
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
|
||||
try:
|
||||
parse(tokenize('(1'))
|
||||
print('FAIL: no error')
|
||||
except ParseError as e:
|
||||
print('OK:', e)"
|
||||
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
|
||||
try:
|
||||
parse(tokenize('1 2'))
|
||||
print('FAIL: no error')
|
||||
except ParseError as e:
|
||||
print('OK:', e)"
|
||||
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
|
||||
try:
|
||||
parse(tokenize(')('))
|
||||
print('FAIL: no error')
|
||||
except ParseError as e:
|
||||
print('OK:', e)"
|
||||
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse, ParseError
|
||||
try:
|
||||
parse(tokenize(''))
|
||||
print('FAIL: no error')
|
||||
except ParseError as e:
|
||||
print('OK:', e)"
|
||||
```
|
||||
|
||||
## AST Shape Reference
|
||||
|
||||
Nodes (all `dataclass`es in `calc/parser.py`):
|
||||
|
||||
| Class | Fields | Example |
|
||||
|-------|--------|---------|
|
||||
| `Num` | `value: int \| float` | `Num(42)`, `Num(3.5)` |
|
||||
| `BinOp` | `op: str, left: Node, right: Node` | `BinOp('+', Num(1), Num(2))` |
|
||||
| `Unary` | `op: str, operand: Node` | `Unary('-', Num(5))` |
|
||||
|
||||
`ParseError` inherits from `Exception`.
|
||||
|
||||
## Files Produced
|
||||
|
||||
- `calc/parser.py` — `ParseError`, `Num`, `BinOp`, `Unary`, `Node`, `parse()`
|
||||
- `calc/test_parser.py` — 24 unittest cases covering D1–D5
|
||||
@ -0,0 +1,64 @@
|
||||
# STATUS — review phase (Builder)
|
||||
|
||||
## Current State
|
||||
Gate: ALL — CLAIMED, awaiting Adversary comprehensive cold-verify
|
||||
|
||||
Build is complete. All three phases (lex, parse, eval) self-certified. Ready for Adversary's
|
||||
single comprehensive verification pass per REVIEW CADENCE — DEFERRED rule.
|
||||
|
||||
## What is being claimed
|
||||
|
||||
All DoD items from all prior phases, plus D1–D3 from the review phase plan:
|
||||
|
||||
- **lex D1–D5**: tokenizer correct (numbers, operators, parens, whitespace, errors)
|
||||
- **parse D1–D6**: recursive-descent parser, correct AST shapes, precedence, unary minus, parens, errors
|
||||
- **eval D1–D5**: evaluator + CLI, arithmetic, division, type printing, error exit codes
|
||||
- **review D1**: full cold re-verify from fresh clone
|
||||
- **review D2**: full suite green (53 tests, 0 failures)
|
||||
- **review D3**: cross-feature break-it cases all pass
|
||||
|
||||
## Commit
|
||||
`6b5f4d2` feat(eval): implement evaluator + CLI — all D1-D5 green, 53 tests pass
|
||||
|
||||
(latest commit at time of this claim; git log shows full history)
|
||||
|
||||
## Files Produced (full build)
|
||||
- `calc/lexer.py` — `tokenize(src) -> list[Token]`, `LexError`
|
||||
- `calc/parser.py` — `parse(tokens) -> AST`, `ParseError`
|
||||
- `calc/evaluator.py` — `evaluate(node) -> int|float`, `EvalError`
|
||||
- `calc.py` — CLI entry point
|
||||
- `calc/test_lexer.py` — lex test suite
|
||||
- `calc/test_parser.py` — parse test suite
|
||||
- `calc/test_evaluator.py` — eval + CLI test suite
|
||||
|
||||
## Adversary Verdict
|
||||
`review(all): PASS` — 2026-06-16T00:00:00Z. No findings, no VETOs.
|
||||
All D1–D4 items verified. Builder writing ## DONE per plan.
|
||||
|
||||
## Verification Commands (Adversary cold-verify from fresh clone)
|
||||
|
||||
```bash
|
||||
# D2 — Full suite
|
||||
python -m unittest -q
|
||||
# Expected: Ran 53 tests in X.XXXs / OK
|
||||
|
||||
# D3 cross-feature cases
|
||||
python calc.py "-(-(1+2))" # stdout: 3, exit 0
|
||||
python calc.py "2+3*4-5/5" # stdout: 13, exit 0
|
||||
python calc.py "1 @ 2" # stderr: error: unexpected character '@'..., exit 1
|
||||
python calc.py "1/0" # stderr: error: division by zero, exit 1
|
||||
python calc.py "(1+" # stderr: error: unexpected end of input, exit 1
|
||||
python calc.py " 3.5*(2.0+1.5)" # stdout: 12.25, exit 0
|
||||
python calc.py "4/2" # stdout: 2, exit 0 (no .0)
|
||||
python calc.py "7/2" # stdout: 3.5, exit 0
|
||||
python calc.py "1 +" # stderr: error: ..., exit 1
|
||||
|
||||
# Prior phase cases
|
||||
python calc.py "2+3*4" # stdout: 14, exit 0
|
||||
python calc.py "(2+3)*4" # stdout: 20, exit 0
|
||||
python calc.py "8-3-2" # stdout: 3, exit 0
|
||||
python calc.py "-2+5" # stdout: 3, exit 0
|
||||
python calc.py "2*-3" # stdout: -6, exit 0
|
||||
```
|
||||
|
||||
## DONE
|
||||
Reference in New Issue
Block a user