Explorar o código

Moved division negation is now supported by fraction negation rules.

Taddeus Kroes %!s(int64=14) %!d(string=hai) anos
pai
achega
a15f233f62
Modificáronse 2 ficheiros con 8 adicións e 17 borrados
  1. 4 7
      src/rules/negation.py
  2. 4 10
      tests/test_rules_negation.py

+ 4 - 7
src/rules/negation.py

@@ -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] = \

+ 4 - 10
tests/test_rules_negation.py

@@ -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')