Commit 86921137 authored by Taddeus Kroes's avatar Taddeus Kroes

generate_line now rewrites 'a + -b' to 'a - b'.

parent 4fd27cc2
...@@ -103,8 +103,8 @@ def generate_line(root): ...@@ -103,8 +103,8 @@ def generate_line(root):
e.append(exp) e.append(exp)
# Check if a multiplication sign is nessecary
if op == '*': if op == '*':
# Check if an explicit multiplication sign is nessecary
left, right = node left, right = node
# Get the previous multiplication element if the arity is # Get the previous multiplication element if the arity is
...@@ -127,6 +127,12 @@ def generate_line(root): ...@@ -127,6 +127,12 @@ def generate_line(root):
if (left_id or left_paren or left_int) \ if (left_id or left_paren or left_int) \
and (right_id or right_paren): and (right_id or right_paren):
sep = '' sep = ''
elif op == '+':
# An addition with negation can be written as a
# subtraction, e.g. 1 + -2 == 1 - 2
if node[1].title() == '-' and len(node[1]) == 1:
sep = ' - '
e[1] = e[1][1:]
result += sep.join(e) result += sep.join(e)
else: else:
......
...@@ -122,3 +122,7 @@ class TestLine(unittest.TestCase): ...@@ -122,3 +122,7 @@ class TestLine(unittest.TestCase):
self.assertEquals(generate_line(mul), '2a') self.assertEquals(generate_line(mul), '2a')
mul = N('*', a, l2) mul = N('*', a, l2)
self.assertEquals(generate_line(mul), 'a * 2') self.assertEquals(generate_line(mul), 'a * 2')
def test_plus_to_minus(self):
plus = N('+', L(1), N('-', L(2)))
self.assertEquals(generate_line(plus), '1 - 2')
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