Commit ec0a7e59 authored by Taddeus Kroes's avatar Taddeus Kroes

Fixed issue that caused parentheses not to be added.

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