Commit 591a3e65 authored by Taddeus Kroes's avatar Taddeus Kroes

Parentheses around the left child are now left out when children of binary...

Parentheses around the left child are now left out when children of binary operators have equal precedences.
parent 0bbbb5e6
......@@ -90,7 +90,7 @@ def generate_line(root):
sep = ' ' + op + ' '
e = []
for child in node:
for i, child in enumerate(node):
exp = traverse(child)
# Check if there is a precedence conflict
......@@ -98,7 +98,7 @@ def generate_line(root):
child_pred = pred(child)
if child_pred < node_pred or \
(child_pred == node_pred and op != child.title()):
(i and child_pred == node_pred and op != child.title()):
exp = '(' + exp + ')'
e.append(exp)
......
......@@ -20,12 +20,17 @@ class TestLine(unittest.TestCase):
times = N('*', plus, plus)
self.assertEquals(generate_line(times), '(1 + 2) * (1 + 2)')
def test_parentheses_equal_precedence(self):
def test_parentheses_equal_precedence_right(self):
l0, l1, l2 = L(1), L(2), L(3)
plus = N('+', l1, l2)
minus = N('-', l0, plus)
self.assertEquals(generate_line(minus), '1 - (2 + 3)')
def test_parentheses_equal_precedence_left(self):
a, b, c, d = L('a'), L('b'), L('c'), L('d')
exp = N('*', N('/', N('*', a, b), c), d)
self.assertEquals(generate_line(exp), 'ab / c * d')
def test_parentheses_nary(self):
l0, l1, l2 = L(1), L(2), L(3)
plus = N('+', N('+', l0, l1), l2)
......
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