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):
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):
......
......@@ -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".'
......
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