Przeglądaj źródła

Use 'negated' attribute instead of a negation node.

Negation of node or leaf increases negated value.
Sander Mathijs van Veen 14 lat temu
rodzic
commit
7b75040e7a
2 zmienionych plików z 11 dodań i 8 usunięć
  1. 6 2
      src/node.py
  2. 5 6
      src/parser.py

+ 6 - 2
src/node.py

@@ -60,6 +60,10 @@ def to_expression(obj):
 
 
 class ExpressionBase(object):
+
+    def __init__(self, *args, **kwargs):
+        self.negated = 0
+
     def clone(self):
         return copy.deepcopy(self)
 
@@ -165,7 +169,8 @@ class ExpressionBase(object):
         return ExpressionNode('^', self, to_expression(other))
 
     def __neg__(self):
-        return ExpressionNode('-', self)
+        self.negated += 1
+        return self
 
 
 class ExpressionNode(Node, ExpressionBase):
@@ -309,7 +314,6 @@ class ExpressionNode(Node, ExpressionBase):
 class ExpressionLeaf(Leaf, ExpressionBase):
     def __init__(self, *args, **kwargs):
         super(ExpressionLeaf, self).__init__(*args, **kwargs)
-
         self.type = TYPE_MAP[type(args[0])]
 
     def __eq__(self, other):

+ 5 - 6
src/parser.py

@@ -343,7 +343,9 @@ class Parser(BisonParser):
         """
 
         if option == 0:  # rule: NEG exp
-            return Node('-', values[1])
+            node = values[1]
+            node.negated += 1
+            return node
 
         raise BisonSyntaxError('Unsupported option %d in target "%s".'
                                % (option, target))  # pragma: nocover
@@ -361,11 +363,8 @@ class Parser(BisonParser):
             return Node(values[1], values[0], values[2])
 
         if option == 4:  # rule: exp MINUS exp
-            # It is necessary to call the hook_handler here explicitly, since
-            # the minus operator is internally represented as two nodes (unary
-            # negation and binary plus).
-            node = Node('-', values[2])
-            node = self.hook_handler(target, option, names, values, node)
+            node = values[2]
+            node.negated += 1
             return Node('+', values[0], node)
 
         raise BisonSyntaxError('Unsupported option %d in target "%s".'