Use 'negated' attribute instead of a negation node.

Negation of node or leaf increases negated value.
parent 1c9969fd
...@@ -60,6 +60,10 @@ def to_expression(obj): ...@@ -60,6 +60,10 @@ def to_expression(obj):
class ExpressionBase(object): class ExpressionBase(object):
def __init__(self, *args, **kwargs):
self.negated = 0
def clone(self): def clone(self):
return copy.deepcopy(self) return copy.deepcopy(self)
...@@ -165,7 +169,8 @@ class ExpressionBase(object): ...@@ -165,7 +169,8 @@ class ExpressionBase(object):
return ExpressionNode('^', self, to_expression(other)) return ExpressionNode('^', self, to_expression(other))
def __neg__(self): def __neg__(self):
return ExpressionNode('-', self) self.negated += 1
return self
class ExpressionNode(Node, ExpressionBase): class ExpressionNode(Node, ExpressionBase):
...@@ -309,7 +314,6 @@ class ExpressionNode(Node, ExpressionBase): ...@@ -309,7 +314,6 @@ class ExpressionNode(Node, ExpressionBase):
class ExpressionLeaf(Leaf, ExpressionBase): class ExpressionLeaf(Leaf, ExpressionBase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ExpressionLeaf, self).__init__(*args, **kwargs) super(ExpressionLeaf, self).__init__(*args, **kwargs)
self.type = TYPE_MAP[type(args[0])] self.type = TYPE_MAP[type(args[0])]
def __eq__(self, other): def __eq__(self, other):
......
...@@ -343,7 +343,9 @@ class Parser(BisonParser): ...@@ -343,7 +343,9 @@ class Parser(BisonParser):
""" """
if option == 0: # rule: NEG exp 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".' raise BisonSyntaxError('Unsupported option %d in target "%s".'
% (option, target)) # pragma: nocover % (option, target)) # pragma: nocover
...@@ -361,11 +363,8 @@ class Parser(BisonParser): ...@@ -361,11 +363,8 @@ class Parser(BisonParser):
return Node(values[1], values[0], values[2]) return Node(values[1], values[0], values[2])
if option == 4: # rule: exp MINUS exp if option == 4: # rule: exp MINUS exp
# It is necessary to call the hook_handler here explicitly, since node = values[2]
# the minus operator is internally represented as two nodes (unary node.negated += 1
# negation and binary plus).
node = Node('-', values[2])
node = self.hook_handler(target, option, names, values, node)
return Node('+', values[0], node) return Node('+', values[0], node)
raise BisonSyntaxError('Unsupported option %d in target "%s".' raise BisonSyntaxError('Unsupported option %d in target "%s".'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment