Преглед изворни кода

Moved n-ary operator definition to a constant list.

Taddeus Kroes пре 14 година
родитељ
комит
358e632e61
2 измењених фајлова са 8 додато и 4 уклоњено
  1. 5 2
      src/node.py
  2. 3 2
      src/strategy.py

+ 5 - 2
src/node.py

@@ -31,6 +31,9 @@ OP_SUBSCRIPT = 8
 OP_AND = 9
 OP_OR = 10
 
+# Binary operators that are considered n-ary
+NARY_OPERATORS = [OP_ADD, OP_SUB, OP_MUL]
+
 # N-ary (functions)
 OP_INT = 11
 OP_INT_INDEF = 12
@@ -200,7 +203,7 @@ class ExpressionBase(object):
         return exponent == None or self[1] == exponent
 
     def is_nary(self):
-        return not self.is_leaf and self.op in [OP_ADD, OP_SUB, OP_MUL]
+        return not self.is_leaf and self.op in NARY_OPERATORS
 
     def is_identifier(self, identifier=None):
         return self.type == TYPE_IDENTIFIER \
@@ -457,7 +460,7 @@ class ExpressionNode(Node, ExpressionBase):
         if not isinstance(other, ExpressionNode) or other.op != self.op:
             return False
 
-        if self.op in (OP_ADD, OP_MUL):
+        if self.op in NARY_OPERATORS:
             s0 = Scope(self)
             s1 = set(Scope(other))
 

+ 3 - 2
src/strategy.py

@@ -1,4 +1,4 @@
-from node import OP_NEG
+from node import OP_NEG, NARY_OPERATORS
 from rules import RULES
 from rules.precedences import HIGH, LOW, RELATIVE
 
@@ -70,7 +70,8 @@ def depth_possibilities(node, depth=0, parent_op=None):
         # Add operator-specific handlers. Prevent duplicate possibilities in
         # n-ary nodes by only executing the handlers on the outermost node of
         # related nodes with the same operator
-        if node.op != parent_op and node.op in RULES:
+        if node.op in RULES and (node.op != parent_op \
+                                 or node.op not in NARY_OPERATORS):
             handlers += RULES[node.op]
 
     # Add negation handlers after operator-specific handlers to obtain an