Skip to content
Snippets Groups Projects
Commit a15f233f authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Moved division negation is now supported by fraction negation rules.

parent 60155fdc
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@ def match_negated_factor(node):
brought to the most left node in the multiplication's scope.
Example:
a * -b -> -(ab)
a * -b -> -ab
"""
assert node.is_op(OP_MUL)
......@@ -115,24 +115,21 @@ def match_negated_division(node):
if a.negated and b.negated:
return [P(node, double_negated_division, ())]
elif a.negated:
return [P(node, single_negated_division, (+a, b))]
elif b.negated:
return [P(node, single_negated_division, (a, +b))]
return [P(node, single_negated_division, (a, b))]
return []
def single_negated_division(root, args):
"""
-a / b -> -(a / b)
a / -b -> -(a / b)
a / -b -> -a / b
"""
a, b = args
# FIXME: "-a/b" results in "-(a/b)", which will cause a loop.
return -(a / b)
return -a / +b
MESSAGES[single_negated_division] = \
......
......@@ -56,14 +56,12 @@ class TestRulesNegation(RulesTestCase):
def test_match_negated_division_single(self):
l1, l2 = root = tree('-1 / 2')
possibilities = match_negated_division(root)
self.assertEqualPos(possibilities,
[P(root, single_negated_division, (+l1, l2))])
self.assertEqualPos(match_negated_division(root), [])
l1, l2 = root = tree('1 / -2')
possibilities = match_negated_division(root)
self.assertEqualPos(possibilities,
[P(root, single_negated_division, (l1, +l2))])
[P(root, single_negated_division, (l1, l2))])
def test_match_negated_division_double(self):
root = tree('-1 / -2')
......@@ -73,13 +71,9 @@ class TestRulesNegation(RulesTestCase):
[P(root, double_negated_division, ())])
def test_single_negated_division(self):
l1, l2 = root = tree('-1 / 2')
self.assertEqualNodes(single_negated_division(root, (+l1, l2)),
-(+l1 / l2))
l1, l2 = root = tree('1 / -2')
self.assertEqualNodes(single_negated_division(root, (l1, +l2)),
-(l1 / +l2))
self.assertEqualNodes(single_negated_division(root, (l1, l2)),
-l1 / +l2)
def test_double_negated_division(self):
l1, l2 = root = tree('-1 / -2')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment