Commit 84d65757 authored by Taddeus Kroes's avatar Taddeus Kroes

Negated sub-expressions now have to contain spaces to be enclosed in parentheses.

parent cccf4d63
...@@ -84,8 +84,9 @@ def generate_line(root): ...@@ -84,8 +84,9 @@ def generate_line(root):
# Unary operator # Unary operator
sub_exp = traverse(node[0]) sub_exp = traverse(node[0])
if not isinstance(node[0], Leaf) and len(node[0]) > 1: if ' ' in sub_exp:
# Negated n-ary operators should be enclosed in parentheses # Negated sub-expressions with spaces in them should be
# enclosed in parentheses
sub_exp = '(' + sub_exp + ')' sub_exp = '(' + sub_exp + ')'
result = op + sub_exp result = op + sub_exp
......
...@@ -158,5 +158,8 @@ class TestLine(unittest.TestCase): ...@@ -158,5 +158,8 @@ class TestLine(unittest.TestCase):
neg = N('-', N('+', L(1), L(2))) neg = N('-', N('+', L(1), L(2)))
self.assertEquals(generate_line(neg), '-(1 + 2)') self.assertEquals(generate_line(neg), '-(1 + 2)')
neg = N('-', N('*', L(4), L('a')))
self.assertEquals(generate_line(neg), '-4a')
neg = N('-', N('-', L(1))) neg = N('-', N('-', L(1)))
self.assertEquals(generate_line(neg), '--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