Commit f9709555 authored by Taddeus Kroes's avatar Taddeus Kroes

Fixed powers parent parentheses.

parent 1911c3fe
......@@ -140,10 +140,13 @@ def generate_line(root):
# (-a) ^ b
if op == '^' and not i:
exp = '(' + exp + ')'
elif child_pred < node_pred \
or (i and child_pred == node_pred \
and (op != child.title() \
or (op == '+' and child[1].negated))):
elif child_pred < node_pred:
exp = '(' + exp + ')'
elif child_pred == node_pred:
if i and (op != child.title() \
or (op == '+' and child[1].negated)):
exp = '(' + exp + ')'
elif not i and op == '^':
exp = '(' + exp + ')'
e.append(exp)
......
......@@ -26,11 +26,18 @@ class TestLine(unittest.TestCase):
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)
l0, l1, l2, l3 = L(1), L(2), L(3), L(4)
plus = N('+', l1, l2)
minus = N('-', l0, plus)
self.assertEquals(generate_line(minus), '1 - (2 + 3)')
power = N('^', l0, N('^', l1, l2))
self.assertEquals(generate_line(power), '1 ^ 2 ^ 3')
power = N('^', N('^', l0, l1), l2)
self.assertEquals(generate_line(power), '(1 ^ 2) ^ 3')
power = N('^', l0, N('^', N('^', l1, l2), l3))
self.assertEquals(generate_line(power), '1 ^ (2 ^ 3) ^ 4')
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)
......
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