Răsfoiți Sursa

Division by negated multiplication is now printed correctly by line printer.

Taddeus Kroes 13 ani în urmă
părinte
comite
fe4e092cec
2 a modificat fișierele cu 12 adăugiri și 2 ștergeri
  1. 2 2
      line.py
  2. 10 0
      tests/test_line.py

+ 2 - 2
line.py

@@ -247,14 +247,14 @@ def generate_line(root):
             lparens = is_right_assoc(left.title()) or is_right_assoc(op)
 
         if rpred < op_pred:
-            rparens = not unary_right
+            rparens = not unary_right or len(right[0]) > 1
         elif rpred == op_pred and len(right) > 1:
             if right.title() == op:
                 rparens = not is_right_assoc(op)
             elif is_left_assoc(right.title()):
                 rparens = True
 
-        # Check if multiplication sig is necessary
+        # Check if multiplication sign is necessary
         if op == '*' and not unary_right:
             sep = mult_sign(left, right, lparens, rparens)
 

+ 10 - 0
tests/test_line.py

@@ -338,3 +338,13 @@ class TestLine(unittest.TestCase):
         def addbrackets(self): self[0] = N('[]', self[0])
         root.preprocess_str_exp = new.instancemethod(addbrackets, root)
         self.assertEquals(generate_line(root), '-[1]')
+
+    def test_division_by_multiplication(self):
+        root = N('/', L(1), N('*', L('a'), L('b')))
+        self.assertEquals(generate_line(root), '1 / (ab)')
+
+        root = N('/', L(1), -N('*', L('a'), L('b')))
+        self.assertEquals(generate_line(root), '1 / (-ab)')
+
+        root = N('/', L(1), N('*', -L('a'), L('b')))
+        self.assertEquals(generate_line(root), '1 / ((-a)b)')