Pārlūkot izejas kodu

Negated sub-expressions now have to contain spaces to be enclosed in parentheses.

Taddeus Kroes 14 gadi atpakaļ
vecāks
revīzija
84d65757bd
2 mainītis faili ar 6 papildinājumiem un 2 dzēšanām
  1. 3 2
      line.py
  2. 3 0
      tests/test_line.py

+ 3 - 2
line.py

@@ -84,8 +84,9 @@ def generate_line(root):
                 # Unary operator
                 sub_exp = traverse(node[0])
 
-                if not isinstance(node[0], Leaf) and len(node[0]) > 1:
-                    # Negated n-ary operators should be enclosed in parentheses
+                if ' ' in sub_exp:
+                    # Negated sub-expressions with spaces in them should be
+                    # enclosed in parentheses
                     sub_exp = '(' + sub_exp + ')'
 
                 result = op + sub_exp

+ 3 - 0
tests/test_line.py

@@ -158,5 +158,8 @@ class TestLine(unittest.TestCase):
         neg = N('-', N('+', L(1), L(2)))
         self.assertEquals(generate_line(neg), '-(1 + 2)')
 
+        neg = N('-', N('*', L(4), L('a')))
+        self.assertEquals(generate_line(neg), '-4a')
+
         neg = N('-', N('-', L(1)))
         self.assertEquals(generate_line(neg), '--1')