Commit cccf4d63 authored by Taddeus Kroes's avatar Taddeus Kroes

Negated n-ary operators are now enclosed in parentheses by line generator.

parent d1e486ac
...@@ -82,7 +82,13 @@ def generate_line(root): ...@@ -82,7 +82,13 @@ def generate_line(root):
if is_operator(node): if is_operator(node):
if arity == 1: if arity == 1:
# Unary operator # Unary operator
result = op + traverse(node[0]) 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
sub_exp = '(' + sub_exp + ')'
result = op + sub_exp
else: else:
# N-ary operator # N-ary operator
node_pred = pred(node) node_pred = pred(node)
......
...@@ -153,3 +153,10 @@ class TestLine(unittest.TestCase): ...@@ -153,3 +153,10 @@ class TestLine(unittest.TestCase):
self.assertTrue(is_int(neg)) self.assertTrue(is_int(neg))
self.assertFalse(is_int(neg_a)) self.assertFalse(is_int(neg_a))
self.assertFalse(is_int(plus)) self.assertFalse(is_int(plus))
def test_negated_nary(self):
neg = N('-', N('+', L(1), L(2)))
self.assertEquals(generate_line(neg), '-(1 + 2)')
neg = N('-', N('-', L(1)))
self.assertEquals(generate_line(neg), '--1')
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