Browse Source

Instead of to the outside, negations in multiplications are now brought to the left.

Taddeus Kroes 14 năm trước cách đây
mục cha
commit
28644d89bd
2 tập tin đã thay đổi với 10 bổ sung10 xóa
  1. 5 4
      src/rules/negation.py
  2. 5 6
      tests/test_rules_negation.py

+ 5 - 4
src/rules/negation.py

@@ -6,7 +6,7 @@ from ..translate import _
 def match_negated_factor(node):
     """
     This rule assures that negations in the scope of a multiplication are
-    brought `outside', to the multiplication itself.
+    brought to the most left node in the multiplication's scope.
 
     Example:
     a * -b  ->  -(ab)
@@ -19,7 +19,7 @@ def match_negated_factor(node):
     # FIXME: The negation that is brought outside is assigned to the first
     # element in the scope during the next parsing step:
     # -ab -> -(ab), but -(ab) is printed as -ab
-    for factor in scope:
+    for factor in scope[1:]:
         if factor.negated:
             p.append(P(node, negated_factor, (scope, factor)))
 
@@ -28,12 +28,13 @@ def match_negated_factor(node):
 
 def negated_factor(root, args):
     """
-    a * -b  ->  -(ab)
+    a * -b  ->  -ab
     """
     scope, factor = args
+    scope[0] = -scope[0]
     scope.replace(factor, +factor)
 
-    return -scope.as_nary_node()
+    return scope.as_nary_node()
 
 
 MESSAGES[negated_factor] = \

+ 5 - 6
tests/test_rules_negation.py

@@ -23,14 +23,13 @@ class TestRulesNegation(RulesTestCase):
     def test_negated_factor(self):
         a, b = root = tree('a * -b')
         self.assertEqualNodes(negated_factor(root, (Scope(root), b)),
-                              -(a * +b))
+                              -a * +b)
 
         (a, b), c = root = tree('a * -b * -c')
-        scope = Scope(root)
-        self.assertEqualNodes(negated_factor(root, (scope, b)),
-                              -(a * +b * c))
-        self.assertEqualNodes(negated_factor(root, (scope, c)),
-                              -(a * b * +c))
+        self.assertEqualNodes(negated_factor(root, (Scope(root), b)),
+                              -a * +b * c)
+        self.assertEqualNodes(negated_factor(root, (Scope(root), c)),
+                              -a * b * +c)
 
     def test_match_negate_polynome(self):
         root = tree('--a')