Commit bf0b2e54 authored by Taddeus Kroes's avatar Taddeus Kroes

Improved test cases for multiplication sign omission in line generator.

parent 591a3e65
......@@ -115,9 +115,17 @@ def generate_line(root):
# a * b -> ab
# a * 2 -> a * 2
# a * (b) -> a(b)
# (a) * b -> (a)b
# (a) * (b) -> (a)(b)
# 2 * a -> 2a
if (is_id(left) and (is_id(right) or e[1][0] == '(')) \
or (is_int(left) and is_id(right)):
left_id = is_id(left)
right_id = is_id(right)
left_paren = e[0][-1] == ')'
right_paren = e[1][0] == '('
left_int = is_int(left)
if (left_id or left_paren or left_int) \
and (right_id or right_paren):
sep = ''
result += sep.join(e)
......
......@@ -18,7 +18,7 @@ class TestLine(unittest.TestCase):
l0, l1 = L(1), L(2)
plus = N('+', l0, l1)
times = N('*', plus, plus)
self.assertEquals(generate_line(times), '(1 + 2) * (1 + 2)')
self.assertEquals(generate_line(times), '(1 + 2)(1 + 2)')
def test_parentheses_equal_precedence_right(self):
l0, l1, l2 = L(1), L(2), L(3)
......@@ -107,6 +107,12 @@ class TestLine(unittest.TestCase):
mul = N('*', a, N('-', b, c))
self.assertEquals(generate_line(mul), 'a(b - c)')
mul = N('*', N('+', a, b), c)
self.assertEquals(generate_line(mul), '(a + b)c')
mul = N('*', N('+', a, b), N('+', c, l2))
self.assertEquals(generate_line(mul), '(a + b)(c + 2)')
mul = N('*', l2, a)
self.assertEquals(generate_line(mul), '2a')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment