Commit c8ecf708 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Multiplication sign is not omitted anymore for multiplication of equal identifiers

parent 0e6c5e55
...@@ -173,6 +173,7 @@ def generate_line(root): ...@@ -173,6 +173,7 @@ def generate_line(root):
left = rightmost_node(left) left = rightmost_node(left)
# a * b -> ab # a * b -> ab
# a * a -> a * a
# a * 2 -> a * 2 # a * 2 -> a * 2
# a * (b) -> a(b) # a * (b) -> a(b)
# (a) * b -> (a)b # (a) * b -> (a)b
...@@ -181,18 +182,21 @@ def generate_line(root): ...@@ -181,18 +182,21 @@ def generate_line(root):
# a * sin(b) -> a sin(b) # a * sin(b) -> a sin(b)
left_char = content[left][-1] left_char = content[left][-1]
right_char = content[right][0] right_char = content[right][0]
left_paren = lparens or left_char in ')]}'
right_paren = rparens or right_char in '([{' if lparens or rparens or left_char != right_char:
right_alpha = right_char.isalpha() left_paren = lparens or left_char in ')]}'
left_simple = is_id(left) or is_int(left) right_paren = rparens or right_char in '([{'
left_alpha = left_char.isalpha()
if left_paren or (right_paren and left_simple) \ right_alpha = right_char.isalpha()
or (is_id(left) and is_id(right)) \ left_simple = is_id(left) or is_int(left)
or (is_int(left) and right_alpha):
return '' if left_paren or (right_paren and left_simple) \
or (is_id(left) and is_id(right)) \
if is_id(left) and right_alpha: or (is_int(left) and right_alpha):
return ' ' return ''
if is_id(left) and right_alpha:
return ' '
return ' * ' return ' * '
......
...@@ -138,6 +138,11 @@ class TestLine(unittest.TestCase): ...@@ -138,6 +138,11 @@ class TestLine(unittest.TestCase):
mul = N('*', N('*', a, l2), b) mul = N('*', N('*', a, l2), b)
self.assertEquals(generate_line(mul), 'a * 2b') self.assertEquals(generate_line(mul), 'a * 2b')
mul = N('*', a, a)
self.assertEquals(generate_line(mul), 'a * a')
mul = N('*', N('+', a, b), N('+', b, a))
self.assertEquals(generate_line(mul), '(a + b)(b + a)')
mul = -N('*', N('*', a, b), c) mul = -N('*', N('*', a, b), c)
self.assertEquals(generate_line(mul), '-abc') self.assertEquals(generate_line(mul), '-abc')
mul = N('*', N('*', -a, b), c) mul = N('*', N('*', -a, b), c)
......
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