artifacts: add calculators/ — the 30 built calculators (5/variant) + machine-docs + git logs
This commit is contained in:
@ -0,0 +1,107 @@
|
||||
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):
|
||||
def test_mul_tighter_than_add(self):
|
||||
# 1+2*3 → PLUS(1, STAR(2, 3)) NOT STAR(PLUS(1,2), 3)
|
||||
self.assertEqual(p('1+2*3'), BinOp('PLUS', Num(1), BinOp('STAR', Num(2), Num(3))))
|
||||
|
||||
def test_mul_tighter_than_sub(self):
|
||||
# 2*3-1 → MINUS(STAR(2,3), 1)
|
||||
self.assertEqual(p('2*3-1'), BinOp('MINUS', BinOp('STAR', Num(2), Num(3)), Num(1)))
|
||||
|
||||
def test_div_tighter_than_add(self):
|
||||
# 4/2+1 → PLUS(SLASH(4,2), 1)
|
||||
self.assertEqual(p('4/2+1'), BinOp('PLUS', BinOp('SLASH', Num(4), Num(2)), Num(1)))
|
||||
|
||||
def test_div_tighter_than_sub(self):
|
||||
# 6-4/2 → MINUS(6, SLASH(4,2))
|
||||
self.assertEqual(p('6-4/2'), BinOp('MINUS', Num(6), BinOp('SLASH', Num(4), Num(2))))
|
||||
|
||||
|
||||
class TestLeftAssociativity(unittest.TestCase):
|
||||
def test_sub_left(self):
|
||||
# 8-3-2 → MINUS(MINUS(8,3), 2)
|
||||
self.assertEqual(p('8-3-2'), BinOp('MINUS', BinOp('MINUS', Num(8), Num(3)), Num(2)))
|
||||
|
||||
def test_div_left(self):
|
||||
# 8/4/2 → SLASH(SLASH(8,4), 2)
|
||||
self.assertEqual(p('8/4/2'), BinOp('SLASH', BinOp('SLASH', Num(8), Num(4)), Num(2)))
|
||||
|
||||
def test_add_left(self):
|
||||
# 1+2+3 → PLUS(PLUS(1,2), 3)
|
||||
self.assertEqual(p('1+2+3'), BinOp('PLUS', BinOp('PLUS', Num(1), Num(2)), Num(3)))
|
||||
|
||||
def test_mul_left(self):
|
||||
# 2*3*4 → STAR(STAR(2,3), 4)
|
||||
self.assertEqual(p('2*3*4'), BinOp('STAR', BinOp('STAR', Num(2), Num(3)), Num(4)))
|
||||
|
||||
|
||||
class TestParentheses(unittest.TestCase):
|
||||
def test_paren_overrides_precedence(self):
|
||||
# (1+2)*3 → STAR(PLUS(1,2), 3) — plus is UNDER star
|
||||
self.assertEqual(p('(1+2)*3'), BinOp('STAR', BinOp('PLUS', Num(1), Num(2)), Num(3)))
|
||||
|
||||
def test_paren_on_right(self):
|
||||
# 3*(1+2) → STAR(3, PLUS(1,2))
|
||||
self.assertEqual(p('3*(1+2)'), BinOp('STAR', Num(3), BinOp('PLUS', Num(1), Num(2))))
|
||||
|
||||
def test_nested_parens(self):
|
||||
# ((2+3)) → PLUS(2,3)
|
||||
self.assertEqual(p('((2+3))'), BinOp('PLUS', Num(2), Num(3)))
|
||||
|
||||
def test_single_number_in_parens(self):
|
||||
self.assertEqual(p('(42)'), Num(42))
|
||||
|
||||
|
||||
class TestUnaryMinus(unittest.TestCase):
|
||||
def test_leading_unary(self):
|
||||
self.assertEqual(p('-5'), Unary('MINUS', Num(5)))
|
||||
|
||||
def test_unary_on_paren(self):
|
||||
# -(1+2) → UNARY(-, PLUS(1,2))
|
||||
self.assertEqual(p('-(1+2)'), Unary('MINUS', BinOp('PLUS', Num(1), Num(2))))
|
||||
|
||||
def test_unary_in_mul(self):
|
||||
# 3 * -2 → STAR(3, UNARY(-,2))
|
||||
self.assertEqual(p('3 * -2'), BinOp('STAR', Num(3), Unary('MINUS', Num(2))))
|
||||
|
||||
def test_double_unary(self):
|
||||
# --5 → UNARY(-,UNARY(-,5))
|
||||
self.assertEqual(p('--5'), Unary('MINUS', Unary('MINUS', Num(5))))
|
||||
|
||||
def test_unary_in_add(self):
|
||||
# 1 + -2 → PLUS(1, UNARY(-,2))
|
||||
self.assertEqual(p('1 + -2'), BinOp('PLUS', Num(1), Unary('MINUS', Num(2))))
|
||||
|
||||
|
||||
class TestErrors(unittest.TestCase):
|
||||
def test_trailing_operator(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p('1 +')
|
||||
|
||||
def test_unclosed_paren(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p('(1')
|
||||
|
||||
def test_consecutive_numbers(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p('1 2')
|
||||
|
||||
def test_mismatched_parens_close_first(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p(')(')
|
||||
|
||||
def test_empty_string(self):
|
||||
with self.assertRaises(ParseError):
|
||||
p('')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user