Commit 1855030e authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen

Merge branch 'master' of kompiler.org:python/graph_drawing

parents c4fd4af7 b99a1f72
...@@ -93,7 +93,10 @@ def generate_line(root): ...@@ -93,7 +93,10 @@ def generate_line(root):
# Check if there is an precedence conflict. # Check if there is an precedence conflict.
# If so, add parentheses # If so, add parentheses
if pred(child) < node_pred: child_pred = pred(child)
if child_pred < node_pred or \
(child_pred == node_pred and s != child.title()):
exp = '(' + exp + ')' exp = '(' + exp + ')'
e.append(exp) e.append(exp)
......
...@@ -23,6 +23,17 @@ class TestLine(unittest.TestCase): ...@@ -23,6 +23,17 @@ class TestLine(unittest.TestCase):
times = Node('*', plus, plus) times = Node('*', plus, plus)
self.assertEquals(generate_line(times), '(1 + 2) * (1 + 2)') self.assertEquals(generate_line(times), '(1 + 2) * (1 + 2)')
def test_parentheses_equal_precedence(self):
l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
plus = Node('+', l1, l2)
minus = Node('-', l0, plus)
self.assertEquals(generate_line(minus), '1 - (2 + 3)')
def test_parentheses_nary(self):
l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
plus = Node('+', Node('+', l0, l1), l2)
self.assertEquals(generate_line(plus), '1 + 2 + 3')
def test_function(self): def test_function(self):
exp = Leaf('x') exp = Leaf('x')
inf = Leaf('oo') inf = Leaf('oo')
......
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