Commit f9709555 authored by Taddeus Kroes's avatar Taddeus Kroes

Fixed powers parent parentheses.

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