Commit b63b5130 authored by Taddeus Kroes's avatar Taddeus Kroes

Added negation counter to strict equivalence check and added 'replace' function to Scope class.

parent 105cd269
...@@ -170,10 +170,8 @@ class ExpressionNode(Node, ExpressionBase): ...@@ -170,10 +170,8 @@ class ExpressionNode(Node, ExpressionBase):
""" """
Check strict equivalence. Check strict equivalence.
""" """
if isinstance(other, ExpressionNode): return isinstance(other, ExpressionNode) and self.op == other.op \
return self.op == other.op and self.nodes == other.nodes and self.negated == other.negated and self.nodes == other.nodes
return False
def substitute(self, old_child, new_child): def substitute(self, old_child, new_child):
self.nodes[self.nodes.index(old_child)] = new_child self.nodes[self.nodes.index(old_child)] = new_child
...@@ -267,7 +265,7 @@ class ExpressionNode(Node, ExpressionBase): ...@@ -267,7 +265,7 @@ class ExpressionNode(Node, ExpressionBase):
s0 = Scope(self) s0 = Scope(self)
s1 = set(Scope(other)) s1 = set(Scope(other))
# Scopes sould be of equal size # Scopes should be of equal size
if len(s0) != len(s1): if len(s0) != len(s1):
return False return False
...@@ -308,7 +306,8 @@ class ExpressionLeaf(Leaf, ExpressionBase): ...@@ -308,7 +306,8 @@ class ExpressionLeaf(Leaf, ExpressionBase):
if other_type in TYPE_MAP: if other_type in TYPE_MAP:
return TYPE_MAP[other_type] == self.type and self.value == other return TYPE_MAP[other_type] == self.type and self.value == other
return other.type == self.type and self.value == other.value return self.negated == other.negated and self.type == other.type \
and self.value == other.value
def equals(self, other): def equals(self, other):
""" """
...@@ -373,6 +372,9 @@ class Scope(object): ...@@ -373,6 +372,9 @@ class Scope(object):
raise ValueError('Node "%s" is not in the scope of "%s".' raise ValueError('Node "%s" is not in the scope of "%s".'
% (node, self.node)) % (node, self.node))
def replace(self, node, replacement):
self.remove(node, replacement=replacement)
def as_nary_node(self): def as_nary_node(self):
return nary_node(self.node.value, self.nodes) return nary_node(self.node.value, self.nodes)
......
...@@ -30,22 +30,10 @@ class TestNode(RulesTestCase): ...@@ -30,22 +30,10 @@ class TestNode(RulesTestCase):
self.assertTrue(N('+', *self.l[:2]).is_op(OP_ADD)) self.assertTrue(N('+', *self.l[:2]).is_op(OP_ADD))
self.assertFalse(N('-', *self.l[:2]).is_op(OP_ADD)) self.assertFalse(N('-', *self.l[:2]).is_op(OP_ADD))
def test_is_op_or_negated(self):
self.assertTrue(N('+', *self.l[:2]).is_op_or_negated(OP_ADD))
self.assertTrue(N('-', N('+', *self.l[:2])).is_op_or_negated(OP_ADD))
self.assertFalse(N('-', *self.l[:2]).is_op_or_negated(OP_ADD))
self.assertFalse(self.l[0].is_op_or_negated(OP_ADD))
def test_is_leaf(self): def test_is_leaf(self):
self.assertTrue(L(2).is_leaf) self.assertTrue(L(2).is_leaf)
self.assertFalse(N('+', *self.l[:2]).is_leaf) self.assertFalse(N('+', *self.l[:2]).is_leaf)
def test_is_leaf_or_negated(self):
self.assertTrue(L(2).is_leaf_or_negated())
self.assertTrue(N('-', L(2)).is_leaf_or_negated())
self.assertFalse(N('+', *self.l[:2]).is_leaf_or_negated())
self.assertFalse(N('-', N('+', *self.l[:2])).is_leaf_or_negated())
def test_is_power(self): def test_is_power(self):
self.assertTrue(N('^', *self.l[:2]).is_power()) self.assertTrue(N('^', *self.l[:2]).is_power())
self.assertFalse(N('+', *self.l[:2]).is_power()) self.assertFalse(N('+', *self.l[:2]).is_power())
...@@ -185,14 +173,14 @@ class TestNode(RulesTestCase): ...@@ -185,14 +173,14 @@ class TestNode(RulesTestCase):
self.scope.remove(self.cd) self.scope.remove(self.cd)
self.assertEqual(self.scope.nodes, [self.a, self.b]) self.assertEqual(self.scope.nodes, [self.a, self.b])
def test_scope_remove_replace(self):
self.scope.remove(self.cd, self.f)
self.assertEqual(self.scope.nodes, [self.a, self.b, self.f])
def test_scope_remove_error(self): def test_scope_remove_error(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.scope.remove(self.f) self.scope.remove(self.f)
def test_scope_replace(self):
self.scope.replace(self.cd, self.f)
self.assertEqual(self.scope.nodes, [self.a, self.b, self.f])
def test_nary_node(self): def test_nary_node(self):
a, b, c, d = tree('a,b,c,d') a, b, c, d = tree('a,b,c,d')
......
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