Commit 01cf1342 authored by Taddeus Kroes's avatar Taddeus Kroes

Improved multiplication sign omission.

parent bb3c5d23
...@@ -166,16 +166,11 @@ def generate_line(root): ...@@ -166,16 +166,11 @@ def generate_line(root):
# (a) * b -> (a)b # (a) * b -> (a)b
# (a) * (b) -> (a)(b) # (a) * (b) -> (a)(b)
# 2 * a -> 2a # 2 * a -> 2a
left_paren = e[0][-1] == ')' l = e[0][-1]
right_paren = e[1][0] == '(' r = e[1][0]
left_suited = left_paren or is_id(left) or is_int(left) if r == '(' or (l == ')' and r != '-') \
right_suited = right_paren or ((is_id(left) or is_int(left)) and r.isalpha()):
if not right_suited and not right.negated:
right_suited = is_id(right) or is_power(right)
if left_suited and right_suited:
sep = '' sep = ''
exp = sep.join(e) exp = sep.join(e)
......
...@@ -139,7 +139,7 @@ class TestLine(unittest.TestCase): ...@@ -139,7 +139,7 @@ class TestLine(unittest.TestCase):
mul = N('*', N('+', a, b), c) mul = N('*', N('+', a, b), c)
self.assertEquals(generate_line(mul), '(a + b)c') self.assertEquals(generate_line(mul), '(a + b)c')
mul = N('*', N('+', a, b), l2) mul = N('*', N('+', a, b), l2)
self.assertEquals(generate_line(mul), '(a + b) * 2') self.assertEquals(generate_line(mul), '(a + b)2')
mul = N('*', N('+', a, b), N('+', c, l2)) mul = N('*', N('+', a, b), N('+', c, l2))
self.assertEquals(generate_line(mul), '(a + b)(c + 2)') self.assertEquals(generate_line(mul), '(a + b)(c + 2)')
...@@ -152,6 +152,9 @@ class TestLine(unittest.TestCase): ...@@ -152,6 +152,9 @@ class TestLine(unittest.TestCase):
mul = N('*', l2, N('^', a, l2)) mul = N('*', l2, N('^', a, l2))
self.assertEquals(generate_line(mul), '2a ^ 2') self.assertEquals(generate_line(mul), '2a ^ 2')
mul = N('*', l2, N('^', l2, a))
self.assertEquals(generate_line(mul), '2 * 2 ^ a')
def test_plus_to_minus(self): def test_plus_to_minus(self):
plus = N('+', L(1), -L(2)) plus = N('+', L(1), -L(2))
self.assertEquals(generate_line(plus), '1 - 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