Kaynağa Gözat

Fixed issue that caused parentheses not to be added.

Taddeus Kroes 14 yıl önce
ebeveyn
işleme
ec0a7e59d3
1 değiştirilmiş dosya ile 6 ekleme ve 2 silme
  1. 6 2
      line.py

+ 6 - 2
line.py

@@ -1,3 +1,5 @@
+from node import Leaf
+
 def generate_line(root):
     """
     Print an expression tree in a single text line. Where needed, add
@@ -48,13 +50,15 @@ def generate_line(root):
         """
         Get the associativity of an operator node.
         """
-        if not node.nodes and len(node) > 1:
+        # Check binary and n-ary operators
+        if not isinstance(node, Leaf) and len(node) > 1:
             op = node.title()
 
             for i, group in enumerate(operators):
                 if op in group:
                     return i
 
+        # Unary operator and leaves have highest precedence
         return max_assoc
 
     def traverse(node):
@@ -85,7 +89,7 @@ def generate_line(root):
                 for child in node:
                     exp = traverse(child)
 
-                    # Check if there is an assiociativity conflict.
+                    # Check if there is an precedence conflict.
                     # If so, add parentheses
                     if assoc(child) < node_assoc:
                         exp = '(' + exp + ')'