Commit ade1a950 authored by Taddeus Kroes's avatar Taddeus Kroes

Modified parentheses when handling with negation due to precedence change.

parent ea4270eb
...@@ -146,7 +146,7 @@ def generate_line(root): ...@@ -146,7 +146,7 @@ def generate_line(root):
# (-a) * b # (-a) * b
# a * -b # a * -b
# (-a) / b # (-a) / b
if (node_pred > NEG_PRED and not i): if node_pred > NEG_PRED:
exp = '(' + exp + ')' exp = '(' + exp + ')'
elif child_pred < node_pred: elif child_pred < node_pred:
exp = '(' + exp + ')' exp = '(' + exp + ')'
......
...@@ -77,7 +77,7 @@ class TestLine(unittest.TestCase): ...@@ -77,7 +77,7 @@ class TestLine(unittest.TestCase):
def test_multiplication_identifiers(self): def test_multiplication_identifiers(self):
a, b = L('a'), L('b') a, b = L('a'), L('b')
self.assertEquals(generate_line(N('*', a, b)), 'ab') self.assertEquals(generate_line(N('*', a, b)), 'ab')
self.assertEquals(generate_line(N('*', a, -b)), 'a * -b') self.assertEquals(generate_line(N('*', a, -b)), 'a(-b)')
def test_multiplication_constant_identifier(self): def test_multiplication_constant_identifier(self):
l0, a = L(2), L('a') l0, a = L(2), L('a')
...@@ -219,7 +219,7 @@ class TestLine(unittest.TestCase): ...@@ -219,7 +219,7 @@ class TestLine(unittest.TestCase):
self.assertEquals(generate_line(neg), '-4a') self.assertEquals(generate_line(neg), '-4a')
neg = N('*', L(4), -L('a')) neg = N('*', L(4), -L('a'))
self.assertEquals(generate_line(neg), '4 * -a') self.assertEquals(generate_line(neg), '4(-a)')
neg = -N('*', L(4), L(5)) neg = -N('*', L(4), L(5))
self.assertEquals(generate_line(neg), '-4 * 5') self.assertEquals(generate_line(neg), '-4 * 5')
...@@ -234,7 +234,7 @@ class TestLine(unittest.TestCase): ...@@ -234,7 +234,7 @@ class TestLine(unittest.TestCase):
self.assertEquals(generate_line(plus), 'a / b - c / d') self.assertEquals(generate_line(plus), 'a / b - c / d')
mul = N('*', N('+', L('a'), L('b')), -N('+', L('c'), L('d'))) mul = N('*', N('+', L('a'), L('b')), -N('+', L('c'), L('d')))
self.assertEquals(generate_line(mul), '(a + b) * -(c + d)') self.assertEquals(generate_line(mul), '(a + b)(-(c + d))')
def test_double_negation(self): def test_double_negation(self):
neg = --L(1) neg = --L(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