Commit b99a1f72 authored by Taddeus Kroes's avatar Taddeus Kroes

Fixed parentheses issue in expression that use both plus and minus.

parent 003a9e25
......@@ -93,7 +93,10 @@ def generate_line(root):
# Check if there is an precedence conflict.
# 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 + ')'
e.append(exp)
......
......@@ -23,6 +23,17 @@ class TestLine(unittest.TestCase):
times = Node('*', plus, plus)
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):
exp = Leaf('x')
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