Quellcode durchsuchen

Added temporary fix to solve negated integral bounds syntax error.

Taddeus Kroes vor 13 Jahren
Ursprung
Commit
719fc68d7a
2 geänderte Dateien mit 18 neuen und 3 gelöschten Zeilen
  1. 15 3
      src/node.py
  2. 3 0
      tests/test_parser.py

+ 15 - 3
src/node.py

@@ -340,9 +340,21 @@ class ExpressionNode(Node, ExpressionBase):
         # Add bounds
         if len(self) > 2:
             lbnd, ubnd = self[2:]
-            lbnd = str(ExpressionNode(OP_SUBSCRIPT, lbnd))
-            ubnd = str(ExpressionNode(OP_POW, ubnd))
-            op += lbnd + ubnd
+
+            # FIXME: temporary fix: add parentheses around negated bounds to
+            # prevent a syntax error (solving the syntax error is better, but
+            # harder)
+            if lbnd.negated:
+                lbnds = '%s(%s)' % (OP_VALUE_MAP[OP_SUBSCRIPT], lbnd)
+            else:
+                lbnds = str(ExpressionNode(OP_SUBSCRIPT, lbnd))
+
+            if ubnd.negated:
+                ubnds = '%s(%s)' % (OP_VALUE_MAP[OP_POW], ubnd)
+            else:
+                ubnds = str(ExpressionNode(OP_POW, ubnd))
+
+            op += lbnds + ubnds
 
         # int x ^ 2      ->  int x ^ 2 dx
         # int x + 1      ->  int (x + 1) dx

+ 3 - 0
tests/test_parser.py

@@ -156,6 +156,9 @@ class TestParser(RulesTestCase):
         self.assertEqual(tree('int_(a^2)^b x ^ 2 + 1 dx'),
                          integral(x ** 2 + 1, x, a ** 2, b))
 
+        self.assertEqual(tree('int_(-a)^b x dx'), integral(x, x, -a, b))
+        # FIXME: self.assertEqual(tree('int_-a^b x dx'), integral(x, x, -a, b))
+
     def test_indefinite_integral(self):
         x2, a, b = tree('x ^ 2, a, b')