Prechádzať zdrojové kódy

Fixed powers parent parentheses.

Taddeus Kroes 14 rokov pred
rodič
commit
f97095557f
2 zmenil súbory, kde vykonal 15 pridanie a 5 odobranie
  1. 7 4
      line.py
  2. 8 1
      tests/test_line.py

+ 7 - 4
line.py

@@ -140,11 +140,14 @@ 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)
 

+ 8 - 1
tests/test_line.py

@@ -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)