artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
3
calculators/builder-solo/run-05/.gitignore
vendored
Normal file
3
calculators/builder-solo/run-05/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
5
calculators/builder-solo/run-05/GIT-LOG.txt
Normal file
5
calculators/builder-solo/run-05/GIT-LOG.txt
Normal file
@ -0,0 +1,5 @@
|
||||
# git history (claim/review handshake), from the run's shared bare repo
|
||||
e563ab7 feat: implement recursive-descent parser with AST and unittest suite
|
||||
6243790 status: record lex phase DoD verification and add .gitignore
|
||||
70b71ca feat: implement calc lexer with tokenize() and unittest suite
|
||||
af94a34 seed
|
||||
1
calculators/builder-solo/run-05/README.md
Normal file
1
calculators/builder-solo/run-05/README.md
Normal file
@ -0,0 +1 @@
|
||||
# calc
|
||||
1
calculators/builder-solo/run-05/SOURCE.txt
Normal file
1
calculators/builder-solo/run-05/SOURCE.txt
Normal file
@ -0,0 +1 @@
|
||||
original path: /tmp/ao-solo-ssWwR6/r5
|
||||
31
calculators/builder-solo/run-05/calc.py
Normal file
31
calculators/builder-solo/run-05/calc.py
Normal file
@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python3
|
||||
"""calc.py — command-line arithmetic calculator.
|
||||
|
||||
Usage: python calc.py "<expression>"
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
from calc.evaluator import EvalError, evaluate
|
||||
from calc.lexer import LexError, tokenize
|
||||
from calc.parser import ParseError, parse
|
||||
|
||||
|
||||
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(result)
|
||||
except (LexError, ParseError, EvalError) as e:
|
||||
print(f"Error: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
0
calculators/builder-solo/run-05/calc/__init__.py
Normal file
0
calculators/builder-solo/run-05/calc/__init__.py
Normal file
53
calculators/builder-solo/run-05/calc/evaluator.py
Normal file
53
calculators/builder-solo/run-05/calc/evaluator.py
Normal file
@ -0,0 +1,53 @@
|
||||
"""Evaluator for the arithmetic AST produced by calc.parser.
|
||||
|
||||
evaluate(node) -> int | float
|
||||
|
||||
Result-type rule: if the mathematical result is whole-valued (no fractional
|
||||
part), it is returned as int; otherwise as float. This ensures '4/2' → 2
|
||||
and '7/2' → 3.5 without a trailing .0 on whole results.
|
||||
"""
|
||||
|
||||
from calc.parser import BinOp, Num, Unary
|
||||
|
||||
|
||||
class EvalError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def evaluate(node):
|
||||
"""Walk an AST node and return int or float.
|
||||
|
||||
Raises EvalError on semantic errors (e.g. division by zero).
|
||||
"""
|
||||
if isinstance(node, Num):
|
||||
return node.value
|
||||
|
||||
if isinstance(node, Unary):
|
||||
operand = evaluate(node.operand)
|
||||
if node.op == '-':
|
||||
return _normalize(-operand)
|
||||
raise EvalError(f"Unknown unary op: {node.op!r}")
|
||||
|
||||
if isinstance(node, BinOp):
|
||||
left = evaluate(node.left)
|
||||
right = evaluate(node.right)
|
||||
if node.op == '+':
|
||||
return _normalize(left + right)
|
||||
if node.op == '-':
|
||||
return _normalize(left - right)
|
||||
if node.op == '*':
|
||||
return _normalize(left * right)
|
||||
if node.op == '/':
|
||||
if right == 0:
|
||||
raise EvalError("Division by zero")
|
||||
return _normalize(left / right)
|
||||
raise EvalError(f"Unknown binary op: {node.op!r}")
|
||||
|
||||
raise EvalError(f"Unknown AST node: {node!r}")
|
||||
|
||||
|
||||
def _normalize(v):
|
||||
"""Return int if v is a whole-valued float, else return v unchanged."""
|
||||
if isinstance(v, float) and v == int(v):
|
||||
return int(v)
|
||||
return v
|
||||
65
calculators/builder-solo/run-05/calc/lexer.py
Normal file
65
calculators/builder-solo/run-05/calc/lexer.py
Normal file
@ -0,0 +1,65 @@
|
||||
"""Lexer for arithmetic expressions."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Union
|
||||
|
||||
|
||||
class LexError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Token:
|
||||
kind: str
|
||||
value: Union[int, float, None]
|
||||
|
||||
def __repr__(self):
|
||||
return f"{self.kind}({self.value!r})"
|
||||
|
||||
|
||||
_SINGLE = {
|
||||
'+': 'PLUS',
|
||||
'-': 'MINUS',
|
||||
'*': 'STAR',
|
||||
'/': 'SLASH',
|
||||
'(': 'LPAREN',
|
||||
')': 'RPAREN',
|
||||
}
|
||||
|
||||
EOF = Token('EOF', None)
|
||||
|
||||
|
||||
def tokenize(src: str) -> list:
|
||||
tokens = []
|
||||
i = 0
|
||||
while i < len(src):
|
||||
ch = src[i]
|
||||
|
||||
if ch in ' \t\r\n':
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if ch in _SINGLE:
|
||||
tokens.append(Token(_SINGLE[ch], ch))
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if ch.isdigit() or ch == '.':
|
||||
j = i
|
||||
has_dot = False
|
||||
while j < len(src) and (src[j].isdigit() or (src[j] == '.' and not has_dot)):
|
||||
if src[j] == '.':
|
||||
has_dot = True
|
||||
j += 1
|
||||
raw = src[i:j]
|
||||
if raw == '.':
|
||||
raise LexError(f"Invalid character '.' at position {i}")
|
||||
value = float(raw) if has_dot else int(raw)
|
||||
tokens.append(Token('NUMBER', value))
|
||||
i = j
|
||||
continue
|
||||
|
||||
raise LexError(f"Invalid character {ch!r} at position {i}")
|
||||
|
||||
tokens.append(EOF)
|
||||
return tokens
|
||||
118
calculators/builder-solo/run-05/calc/parser.py
Normal file
118
calculators/builder-solo/run-05/calc/parser.py
Normal file
@ -0,0 +1,118 @@
|
||||
"""Recursive-descent parser for arithmetic expressions.
|
||||
|
||||
Grammar (precedence low→high):
|
||||
expr ::= term ( ('+' | '-') term )*
|
||||
term ::= factor ( ('*' | '/') factor )*
|
||||
factor ::= NUMBER | '(' expr ')' | '-' factor
|
||||
|
||||
AST node shapes (all dataclasses):
|
||||
Num(value) — numeric leaf; value is int or float
|
||||
BinOp(op, left, right) — op in ('+', '-', '*', '/'), children are nodes
|
||||
Unary(op, operand) — op is '-', operand is a node
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from calc.lexer import Token, tokenize
|
||||
|
||||
|
||||
class ParseError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Num:
|
||||
value: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"Num(value={self.value!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class BinOp:
|
||||
op: str
|
||||
left: Any
|
||||
right: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"BinOp(op={self.op!r}, left={self.left!r}, right={self.right!r})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Unary:
|
||||
op: str
|
||||
operand: Any
|
||||
|
||||
def __repr__(self):
|
||||
return f"Unary(op={self.op!r}, operand={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) -> 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} but got {tok.kind!r}")
|
||||
return self._consume()
|
||||
|
||||
def parse(self):
|
||||
node = self._expr()
|
||||
if self._peek().kind != 'EOF':
|
||||
raise ParseError(f"Unexpected token {self._peek()!r}")
|
||||
return node
|
||||
|
||||
def _expr(self):
|
||||
node = self._term()
|
||||
while self._peek().kind in ('PLUS', 'MINUS'):
|
||||
op = self._consume().value
|
||||
right = self._term()
|
||||
node = BinOp(op, node, right)
|
||||
return node
|
||||
|
||||
def _term(self):
|
||||
node = self._factor()
|
||||
while self._peek().kind in ('STAR', 'SLASH'):
|
||||
op = self._consume().value
|
||||
right = self._factor()
|
||||
node = BinOp(op, node, right)
|
||||
return node
|
||||
|
||||
def _factor(self):
|
||||
tok = self._peek()
|
||||
|
||||
if tok.kind == 'NUMBER':
|
||||
self._consume()
|
||||
return Num(tok.value)
|
||||
|
||||
if tok.kind == 'LPAREN':
|
||||
self._consume()
|
||||
node = self._expr()
|
||||
self._expect('RPAREN')
|
||||
return node
|
||||
|
||||
if tok.kind == 'MINUS':
|
||||
self._consume()
|
||||
operand = self._factor()
|
||||
return Unary('-', operand)
|
||||
|
||||
if tok.kind == 'EOF':
|
||||
raise ParseError("Unexpected end of input")
|
||||
|
||||
raise ParseError(f"Unexpected token {tok!r}")
|
||||
|
||||
|
||||
def parse(tokens: list):
|
||||
"""Parse a token list (from tokenize()) into an AST. Raises ParseError on bad input."""
|
||||
return _Parser(tokens).parse()
|
||||
124
calculators/builder-solo/run-05/calc/test_evaluator.py
Normal file
124
calculators/builder-solo/run-05/calc/test_evaluator.py
Normal file
@ -0,0 +1,124 @@
|
||||
"""Tests for calc.evaluator — covers D1 (arithmetic), D2 (division), D3 (result type)."""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from calc.evaluator import EvalError, evaluate
|
||||
from calc.lexer import tokenize
|
||||
from calc.parser import parse
|
||||
|
||||
|
||||
def calc(s):
|
||||
return evaluate(parse(tokenize(s)))
|
||||
|
||||
|
||||
class TestArithmetic(unittest.TestCase):
|
||||
"""D1 — basic ops, precedence, parentheses, unary minus."""
|
||||
|
||||
def test_add_mul_precedence(self):
|
||||
self.assertEqual(calc("2+3*4"), 14)
|
||||
|
||||
def test_parens(self):
|
||||
self.assertEqual(calc("(2+3)*4"), 20)
|
||||
|
||||
def test_left_associative_subtraction(self):
|
||||
self.assertEqual(calc("8-3-2"), 3)
|
||||
|
||||
def test_unary_minus_leading(self):
|
||||
self.assertEqual(calc("-2+5"), 3)
|
||||
|
||||
def test_unary_minus_rhs(self):
|
||||
self.assertEqual(calc("2*-3"), -6)
|
||||
|
||||
|
||||
class TestDivision(unittest.TestCase):
|
||||
"""D2 — true division and EvalError on divide-by-zero."""
|
||||
|
||||
def test_true_division(self):
|
||||
self.assertEqual(calc("7/2"), 3.5)
|
||||
|
||||
def test_divide_by_zero_raises_eval_error(self):
|
||||
with self.assertRaises(EvalError):
|
||||
calc("1/0")
|
||||
|
||||
def test_divide_by_zero_expression_raises_eval_error(self):
|
||||
with self.assertRaises(EvalError):
|
||||
calc("5/(3-3)")
|
||||
|
||||
def test_no_bare_zero_division_error(self):
|
||||
"""ZeroDivisionError must not escape the API."""
|
||||
try:
|
||||
calc("1/0")
|
||||
except EvalError:
|
||||
pass
|
||||
except ZeroDivisionError:
|
||||
self.fail("ZeroDivisionError escaped the evaluate() API")
|
||||
|
||||
|
||||
class TestResultType(unittest.TestCase):
|
||||
"""D3 — whole-valued results return int, fractional return float."""
|
||||
|
||||
def test_whole_division_returns_int(self):
|
||||
result = calc("4/2")
|
||||
self.assertEqual(result, 2)
|
||||
self.assertIsInstance(result, int)
|
||||
|
||||
def test_fractional_division_returns_float(self):
|
||||
result = calc("7/2")
|
||||
self.assertEqual(result, 3.5)
|
||||
self.assertIsInstance(result, float)
|
||||
|
||||
def test_integer_add_returns_int(self):
|
||||
result = calc("2+3")
|
||||
self.assertIsInstance(result, int)
|
||||
|
||||
def test_integer_mul_returns_int(self):
|
||||
result = calc("3*4")
|
||||
self.assertIsInstance(result, int)
|
||||
|
||||
|
||||
class TestCLI(unittest.TestCase):
|
||||
"""D4 — CLI behaviour."""
|
||||
|
||||
def _run(self, expr):
|
||||
return subprocess.run(
|
||||
[sys.executable, "calc.py", expr],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=__file__.replace("/calc/test_evaluator.py", ""),
|
||||
)
|
||||
|
||||
def test_cli_basic(self):
|
||||
r = self._run("2+3*4")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "14")
|
||||
|
||||
def test_cli_parens(self):
|
||||
r = self._run("(2+3)*4")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "20")
|
||||
|
||||
def test_cli_float(self):
|
||||
r = self._run("7/2")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "3.5")
|
||||
|
||||
def test_cli_whole_division(self):
|
||||
r = self._run("4/2")
|
||||
self.assertEqual(r.returncode, 0)
|
||||
self.assertEqual(r.stdout.strip(), "2")
|
||||
|
||||
def test_cli_divide_by_zero_exits_nonzero(self):
|
||||
r = self._run("1/0")
|
||||
self.assertNotEqual(r.returncode, 0)
|
||||
self.assertGreater(len(r.stderr.strip()), 0)
|
||||
|
||||
def test_cli_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()
|
||||
107
calculators/builder-solo/run-05/calc/test_lexer.py
Normal file
107
calculators/builder-solo/run-05/calc/test_lexer.py
Normal file
@ -0,0 +1,107 @@
|
||||
import unittest
|
||||
from calc.lexer import tokenize, Token, LexError
|
||||
|
||||
|
||||
def kinds(src):
|
||||
return [t.kind for t in tokenize(src)]
|
||||
|
||||
|
||||
def toks(src):
|
||||
return [(t.kind, t.value) for t in tokenize(src)]
|
||||
|
||||
|
||||
class TestNumbers(unittest.TestCase):
|
||||
def test_integer(self):
|
||||
result = tokenize("42")
|
||||
self.assertEqual(result[0].kind, 'NUMBER')
|
||||
self.assertEqual(result[0].value, 42)
|
||||
self.assertIsInstance(result[0].value, int)
|
||||
self.assertEqual(result[1].kind, 'EOF')
|
||||
|
||||
def test_float(self):
|
||||
result = tokenize("3.14")
|
||||
self.assertEqual(result[0].kind, 'NUMBER')
|
||||
self.assertAlmostEqual(result[0].value, 3.14)
|
||||
self.assertIsInstance(result[0].value, float)
|
||||
|
||||
def test_float_leading_dot(self):
|
||||
result = tokenize(".5")
|
||||
self.assertEqual(result[0].kind, 'NUMBER')
|
||||
self.assertAlmostEqual(result[0].value, 0.5)
|
||||
self.assertIsInstance(result[0].value, float)
|
||||
|
||||
def test_float_trailing_dot(self):
|
||||
result = tokenize("10.")
|
||||
self.assertEqual(result[0].kind, 'NUMBER')
|
||||
self.assertAlmostEqual(result[0].value, 10.0)
|
||||
self.assertIsInstance(result[0].value, float)
|
||||
|
||||
def test_zero(self):
|
||||
result = tokenize("0")
|
||||
self.assertEqual(result[0].value, 0)
|
||||
|
||||
|
||||
class TestOperatorsAndParens(unittest.TestCase):
|
||||
def test_plus(self):
|
||||
self.assertIn('PLUS', kinds("+"))
|
||||
|
||||
def test_minus(self):
|
||||
self.assertIn('MINUS', kinds("-"))
|
||||
|
||||
def test_star(self):
|
||||
self.assertIn('STAR', kinds("*"))
|
||||
|
||||
def test_slash(self):
|
||||
self.assertIn('SLASH', kinds("/"))
|
||||
|
||||
def test_lparen(self):
|
||||
self.assertIn('LPAREN', kinds("("))
|
||||
|
||||
def test_rparen(self):
|
||||
self.assertIn('RPAREN', kinds(")"))
|
||||
|
||||
def test_arithmetic_expression(self):
|
||||
result = kinds("1+2*3")
|
||||
self.assertEqual(result, ['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF'])
|
||||
|
||||
|
||||
class TestWhitespaceAndErrors(unittest.TestCase):
|
||||
def test_spaces_skipped(self):
|
||||
result = toks(" 12 + 3 ")
|
||||
self.assertEqual(result, [('NUMBER', 12), ('PLUS', '+'), ('NUMBER', 3), ('EOF', None)])
|
||||
|
||||
def test_complex_expression(self):
|
||||
result = toks("3.5*(1-2)")
|
||||
self.assertEqual(result, [
|
||||
('NUMBER', 3.5),
|
||||
('STAR', '*'),
|
||||
('LPAREN', '('),
|
||||
('NUMBER', 1),
|
||||
('MINUS', '-'),
|
||||
('NUMBER', 2),
|
||||
('RPAREN', ')'),
|
||||
('EOF', None),
|
||||
])
|
||||
|
||||
def test_invalid_at_raises(self):
|
||||
with self.assertRaises(LexError) as ctx:
|
||||
tokenize("1 @ 2")
|
||||
self.assertIn('@', str(ctx.exception))
|
||||
|
||||
def test_invalid_dollar_raises(self):
|
||||
with self.assertRaises(LexError):
|
||||
tokenize("$5")
|
||||
|
||||
def test_letter_raises(self):
|
||||
with self.assertRaises(LexError):
|
||||
tokenize("1 + x")
|
||||
|
||||
def test_error_contains_position(self):
|
||||
with self.assertRaises(LexError) as ctx:
|
||||
tokenize("1 @ 2")
|
||||
msg = str(ctx.exception)
|
||||
self.assertIn('2', msg) # position 2
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
138
calculators/builder-solo/run-05/calc/test_parser.py
Normal file
138
calculators/builder-solo/run-05/calc/test_parser.py
Normal file
@ -0,0 +1,138 @@
|
||||
"""Tests for calc.parser — covers D1-D5 of the parse phase."""
|
||||
|
||||
import unittest
|
||||
|
||||
from calc.lexer import tokenize
|
||||
from calc.parser import BinOp, Num, ParseError, Unary, parse
|
||||
|
||||
|
||||
def p(src: str):
|
||||
"""Convenience: tokenize and parse in one call."""
|
||||
return parse(tokenize(src))
|
||||
|
||||
|
||||
class TestPrecedence(unittest.TestCase):
|
||||
"""D1 — * and / bind tighter than + and -."""
|
||||
|
||||
def test_mul_over_add(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_div_over_sub(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))))
|
||||
|
||||
def test_mul_over_sub_left(self):
|
||||
# 1+2*3+4 => BinOp('+', BinOp('+', Num(1), BinOp('*', Num(2), Num(3))), Num(4))
|
||||
tree = p('1+2*3+4')
|
||||
expected = BinOp('+', BinOp('+', Num(1), BinOp('*', Num(2), Num(3))), Num(4))
|
||||
self.assertEqual(tree, expected)
|
||||
|
||||
|
||||
class TestLeftAssociativity(unittest.TestCase):
|
||||
"""D2 — same-precedence ops 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 — parens override precedence."""
|
||||
|
||||
def test_paren_add_over_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_paren_nested(self):
|
||||
# ((2+3)) => BinOp is gone, we get Num-ish
|
||||
tree = p('((2+3))')
|
||||
self.assertEqual(tree, BinOp('+', Num(2), Num(3)))
|
||||
|
||||
def test_paren_right_side(self):
|
||||
# 3*(1+2) => BinOp('*', Num(3), BinOp('+', Num(1), Num(2)))
|
||||
tree = p('3*(1+2)')
|
||||
self.assertEqual(tree, BinOp('*', Num(3), BinOp('+', Num(1), 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_of_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_binop(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_adjacent_numbers(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_just_op(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p('*')
|
||||
|
||||
def test_mismatched_parens(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p('(1+2')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
23
calculators/builder-solo/run-05/machine-docs/JOURNAL-lex.md
Normal file
23
calculators/builder-solo/run-05/machine-docs/JOURNAL-lex.md
Normal file
@ -0,0 +1,23 @@
|
||||
# JOURNAL-lex
|
||||
|
||||
## 2026-06-15
|
||||
|
||||
### Build
|
||||
|
||||
Created `calc/__init__.py` (empty package marker) and `calc/lexer.py` implementing:
|
||||
- `Token` dataclass with `kind` (str) and `value` (int | float | None)
|
||||
- `LexError(Exception)` raised on invalid characters with offending char and position
|
||||
- `tokenize(src: str) -> list[Token]` covering numbers (int/float, leading/trailing dot), operators, parens, whitespace skipping, and error raising
|
||||
- `EOF` singleton token appended at end of every token list
|
||||
|
||||
Created `calc/test_lexer.py` with 18 unittest cases covering D1–D3 including the three mandatory inputs.
|
||||
|
||||
### Verification
|
||||
|
||||
All four DoD gates observed passing:
|
||||
- D1: integers, floats (3.14, .5, 10.) tokenize correctly with right Python type
|
||||
- D2: all six operator/paren kinds correct; `1+2*3` yields expected sequence
|
||||
- D3: whitespace skipped; `@` raises LexError with position 2 in message
|
||||
- D4: 18 tests, 0 failures
|
||||
|
||||
Commit: 70b71caacd1f7334e387ff15b007573201b524b6
|
||||
@ -0,0 +1,10 @@
|
||||
# JOURNAL-parse
|
||||
|
||||
## 2026-06-15 — Phase start
|
||||
|
||||
Reading plan. Building recursive-descent parser with:
|
||||
- Grammar: expr → term ((+|-) term)*; term → factor ((*|/) factor)*; factor → NUMBER | ( expr ) | - factor
|
||||
- AST nodes: Num, BinOp, Unary
|
||||
- ParseError exception
|
||||
|
||||
Plan: implement parser.py, test_parser.py, verify all D1–D6, then certify.
|
||||
76
calculators/builder-solo/run-05/machine-docs/STATUS-eval.md
Normal file
76
calculators/builder-solo/run-05/machine-docs/STATUS-eval.md
Normal file
@ -0,0 +1,76 @@
|
||||
# STATUS — eval phase
|
||||
|
||||
Commit: c6086a4
|
||||
|
||||
## Gate verification
|
||||
|
||||
### D1 — arithmetic (precedence, parens, unary minus)
|
||||
|
||||
Command: `python -m unittest calc.test_evaluator.TestArithmetic -v`
|
||||
Expected: 5 tests pass
|
||||
|
||||
Observed:
|
||||
```
|
||||
test_add_mul_precedence (calc.test_evaluator.TestArithmetic) ... ok # 2+3*4=14
|
||||
test_left_associative_subtraction (calc.test_evaluator.TestArithmetic) ... ok # 8-3-2=3
|
||||
test_parens (calc.test_evaluator.TestArithmetic) ... ok # (2+3)*4=20
|
||||
test_unary_minus_leading (calc.test_evaluator.TestArithmetic) ... ok # -2+5=3
|
||||
test_unary_minus_rhs (calc.test_evaluator.TestArithmetic) ... ok # 2*-3=-6
|
||||
PASS
|
||||
```
|
||||
|
||||
### D2 — division (true division + EvalError on zero)
|
||||
|
||||
Command: `python -m unittest calc.test_evaluator.TestDivision -v`
|
||||
Expected: 4 tests pass, ZeroDivisionError never escapes
|
||||
|
||||
Observed:
|
||||
```
|
||||
test_divide_by_zero_expression_raises_eval_error ... ok
|
||||
test_divide_by_zero_raises_eval_error ... ok
|
||||
test_no_bare_zero_division_error ... ok
|
||||
test_true_division ... ok # 7/2=3.5
|
||||
PASS
|
||||
```
|
||||
|
||||
### D3 — result type (whole → int, fractional → float)
|
||||
|
||||
Command: `python -m unittest calc.test_evaluator.TestResultType -v`
|
||||
Expected: 4 tests pass
|
||||
|
||||
Observed:
|
||||
```
|
||||
test_fractional_division_returns_float ... ok # 7/2 is float
|
||||
test_integer_add_returns_int ... ok
|
||||
test_integer_mul_returns_int ... ok
|
||||
test_whole_division_returns_int ... ok # 4/2 is int
|
||||
PASS
|
||||
```
|
||||
|
||||
### D4 — CLI
|
||||
|
||||
Commands and observed outputs:
|
||||
|
||||
| Command | Expected | Observed | Exit |
|
||||
|---|---|---|---|
|
||||
| `python calc.py "2+3*4"` | `14` | `14` | 0 |
|
||||
| `python calc.py "(2+3)*4"` | `20` | `20` | 0 |
|
||||
| `python calc.py "7/2"` | `3.5` | `3.5` | 0 |
|
||||
| `python calc.py "4/2"` | `2` | `2` | 0 |
|
||||
| `python calc.py "1/0"` | error→stderr, exit≠0 | `Error: Division by zero` (stderr), stdout empty | 1 |
|
||||
| `python calc.py "1 +"` | error→stderr, exit≠0 | `Error: Unexpected end of input` (stderr) | 1 |
|
||||
|
||||
### D5 — whole suite + no regression
|
||||
|
||||
Command: `python -m unittest -q`
|
||||
Expected: all tests pass, 0 failures
|
||||
|
||||
Observed:
|
||||
```
|
||||
Ran 59 tests in 0.231s
|
||||
OK
|
||||
```
|
||||
|
||||
Prior lex + parse tests: included in the 59, all pass.
|
||||
|
||||
## DONE
|
||||
112
calculators/builder-solo/run-05/machine-docs/STATUS-lex.md
Normal file
112
calculators/builder-solo/run-05/machine-docs/STATUS-lex.md
Normal file
@ -0,0 +1,112 @@
|
||||
# STATUS-lex
|
||||
|
||||
Commit: 70b71caacd1f7334e387ff15b007573201b524b6
|
||||
|
||||
## Gate Results
|
||||
|
||||
### D1 — numbers
|
||||
|
||||
**Check:** `tokenize("42")` → `[NUMBER(42), EOF]`; also `.5`, `3.14`, `10.` tokenize to float NUMBER.
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize('42')])"
|
||||
```
|
||||
|
||||
**Expected:** `[('NUMBER', 42), ('EOF', None)]`
|
||||
|
||||
**Observed:** `[('NUMBER', 42), ('EOF', None)]` ✓
|
||||
|
||||
Additional verified: `.5` → `NUMBER(0.5, float)`, `10.` → `NUMBER(10.0, float)`, `3.14` → `NUMBER(3.14, float)`
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### D2 — operators & parens
|
||||
|
||||
**Check:** `tokenize("1+2*3")` → `NUMBER PLUS NUMBER STAR NUMBER EOF`
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; print([t.kind for t in tokenize('1+2*3')])"
|
||||
```
|
||||
|
||||
**Expected:** `['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']`
|
||||
|
||||
**Observed:** `['NUMBER', 'PLUS', 'NUMBER', 'STAR', 'NUMBER', 'EOF']` ✓
|
||||
|
||||
All operator kinds verified: PLUS, MINUS, STAR, SLASH, LPAREN, RPAREN.
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### D3 — whitespace & errors
|
||||
|
||||
**Check 1 (whitespace):** `" 12 + 3 "` → `NUMBER PLUS NUMBER EOF`
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; print([(t.kind,t.value) for t in tokenize(' 12 + 3 ')])"
|
||||
```
|
||||
|
||||
**Expected:** `[('NUMBER', 12), ('PLUS', '+'), ('NUMBER', 3), ('EOF', None)]`
|
||||
|
||||
**Observed:** `[('NUMBER', 12), ('PLUS', '+'), ('NUMBER', 3), ('EOF', None)]` ✓
|
||||
|
||||
**Check 2 (LexError):** `"1 @ 2"` raises `LexError` with `@` and position in message.
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; tokenize('1 @ 2')"
|
||||
```
|
||||
|
||||
**Expected:** `LexError: Invalid character '@' at position 2`
|
||||
|
||||
**Observed:**
|
||||
```
|
||||
calc.lexer.LexError: Invalid character '@' at position 2
|
||||
```
|
||||
✓
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### D4 — tests green
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -m unittest -q
|
||||
```
|
||||
|
||||
**Expected:** 0 failures
|
||||
|
||||
**Observed:**
|
||||
```
|
||||
----------------------------------------------------------------------
|
||||
Ran 18 tests in 0.000s
|
||||
|
||||
OK
|
||||
```
|
||||
✓
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### Plan verification commands
|
||||
|
||||
```bash
|
||||
python -m unittest -q
|
||||
# Ran 18 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')"
|
||||
# calc.lexer.LexError: Invalid character '@' at position 2
|
||||
```
|
||||
|
||||
## DONE
|
||||
175
calculators/builder-solo/run-05/machine-docs/STATUS-parse.md
Normal file
175
calculators/builder-solo/run-05/machine-docs/STATUS-parse.md
Normal file
@ -0,0 +1,175 @@
|
||||
# STATUS-parse
|
||||
|
||||
Phase: parse
|
||||
|
||||
## AST Shape
|
||||
|
||||
Nodes are dataclasses from `calc.parser`:
|
||||
|
||||
- `Num(value)` — leaf; value is int or float
|
||||
- repr: `Num(value=42)`
|
||||
- `BinOp(op, left, right)` — binary operation; op in ('+', '-', '*', '/')
|
||||
- repr: `BinOp(op='+', left=Num(value=1), right=BinOp(op='*', left=Num(value=2), right=Num(value=3)))`
|
||||
- `Unary(op, operand)` — unary minus; op is '-'
|
||||
- repr: `Unary(op='-', operand=Num(value=5))`
|
||||
|
||||
## Gate Results
|
||||
|
||||
### D1 — precedence
|
||||
|
||||
**Check:** `1+2*3` parses as `1+(2*3)`, not `(1+2)*3`
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('1+2*3')))"
|
||||
```
|
||||
|
||||
**Expected:** `BinOp(op='+', left=Num(value=1), right=BinOp(op='*', left=Num(value=2), right=Num(value=3)))`
|
||||
|
||||
**Observed:** `BinOp(op='+', left=Num(value=1), right=BinOp(op='*', left=Num(value=2), right=Num(value=3)))` ✓
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### D2 — left associativity
|
||||
|
||||
**Check:** `8-3-2` parses as `(8-3)-2`; `8/4/2` as `(8/4)/2`
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('8-3-2'))); print(parse(tokenize('8/4/2')))"
|
||||
```
|
||||
|
||||
**Expected:**
|
||||
- `BinOp(op='-', left=BinOp(op='-', left=Num(value=8), right=Num(value=3)), right=Num(value=2))`
|
||||
- `BinOp(op='/', left=BinOp(op='/', left=Num(value=8), right=Num(value=4)), right=Num(value=2))`
|
||||
|
||||
**Observed:**
|
||||
```
|
||||
BinOp(op='-', left=BinOp(op='-', left=Num(value=8), right=Num(value=3)), right=Num(value=2))
|
||||
BinOp(op='/', left=BinOp(op='/', left=Num(value=8), right=Num(value=4)), right=Num(value=2))
|
||||
```
|
||||
✓
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### D3 — parentheses
|
||||
|
||||
**Check:** `(1+2)*3` parses with `+` under `*`
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('(1+2)*3')))"
|
||||
```
|
||||
|
||||
**Expected:** `BinOp(op='*', left=BinOp(op='+', left=Num(value=1), right=Num(value=2)), right=Num(value=3))`
|
||||
|
||||
**Observed:** `BinOp(op='*', left=BinOp(op='+', left=Num(value=1), right=Num(value=2)), right=Num(value=3))` ✓
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### D4 — unary minus
|
||||
|
||||
**Check:** `-5`, `-(1+2)`, `3 * -2` all parse correctly
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('-5'))); print(parse(tokenize('-(1+2)'))); print(parse(tokenize('3 * -2')))"
|
||||
```
|
||||
|
||||
**Expected:**
|
||||
- `Unary(op='-', operand=Num(value=5))`
|
||||
- `Unary(op='-', operand=BinOp(op='+', left=Num(value=1), right=Num(value=2)))`
|
||||
- `BinOp(op='*', left=Num(value=3), right=Unary(op='-', operand=Num(value=2)))`
|
||||
|
||||
**Observed:**
|
||||
```
|
||||
Unary(op='-', operand=Num(value=5))
|
||||
Unary(op='-', operand=BinOp(op='+', left=Num(value=1), right=Num(value=2)))
|
||||
BinOp(op='*', left=Num(value=3), right=Unary(op='-', operand=Num(value=2)))
|
||||
```
|
||||
✓
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### D5 — errors
|
||||
|
||||
**Check:** `"1 +"`, `"(1"`, `"1 2"`, `")("`, `""` each raise `ParseError`
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -c "
|
||||
from calc.lexer import tokenize
|
||||
from calc.parser import parse, ParseError
|
||||
|
||||
cases = ['1 +', '(1', '1 2', ')(', '']
|
||||
for src in cases:
|
||||
try:
|
||||
parse(tokenize(src))
|
||||
print(f'FAIL: {src!r} did not raise ParseError')
|
||||
except ParseError as e:
|
||||
print(f'PASS: {src!r} -> ParseError: {e}')
|
||||
except Exception as e:
|
||||
print(f'FAIL: {src!r} raised wrong exception {type(e).__name__}: {e}')
|
||||
"
|
||||
```
|
||||
|
||||
**Expected:** All 5 cases print PASS
|
||||
|
||||
**Observed:**
|
||||
```
|
||||
PASS: '1 +' -> ParseError: Unexpected end of input
|
||||
PASS: '(1' -> ParseError: Expected RPAREN but got 'EOF'
|
||||
PASS: '1 2' -> ParseError: Unexpected token NUMBER(2)
|
||||
PASS: ')(' -> ParseError: Unexpected token RPAREN(')')
|
||||
PASS: '' -> ParseError: Unexpected end of input
|
||||
```
|
||||
✓
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
### D6 — tests green
|
||||
|
||||
**Command:**
|
||||
```bash
|
||||
python -m unittest -q
|
||||
```
|
||||
|
||||
**Expected:** 0 failures
|
||||
|
||||
**Observed:**
|
||||
```
|
||||
----------------------------------------------------------------------
|
||||
Ran 40 tests in 0.001s
|
||||
|
||||
OK
|
||||
```
|
||||
✓ (40 = 18 from lex + 22 from parser)
|
||||
|
||||
**Result: PASS**
|
||||
|
||||
---
|
||||
|
||||
## Cold-verify commands (for independent re-run)
|
||||
|
||||
```bash
|
||||
python -m unittest -q
|
||||
# Expected: Ran 40 tests ... OK
|
||||
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; print(parse(tokenize('1+2*3')))"
|
||||
# Expected: BinOp(op='+', left=Num(value=1), right=BinOp(op='*', left=Num(value=2), right=Num(value=3)))
|
||||
|
||||
python -c "from calc.lexer import tokenize; from calc.parser import parse; parse(tokenize('1 +'))"
|
||||
# Expected: ParseError: Unexpected end of input
|
||||
```
|
||||
|
||||
## DONE
|
||||
Reference in New Issue
Block a user