Commit a1d7c084 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

[lib] Logic expressions: Purge shit code, see more...

- use magic methods instead of custom ones;
- ability to make sets of objects with __hash__ methods
parent 26369f30
......@@ -108,12 +108,18 @@ class SigExpression(object):
"""
raise TypeError(self.__class__.__name__ + ": Not implemented")
def test_equal(self, model):
def __eq__(self, model):
"""
test if the expression tree is equal to model tree (for TESTS)
"""
pass
def __ne__(self, model):
"""
test if the expression tree is not equal to model tree (for TESTS)
"""
pass
class SigIdentExpr(SigExpression):
"""
......@@ -168,11 +174,14 @@ class SigIdentExpr(SigExpression):
def accept(self, visitor):
return visitor.visit_sig_ident(self)
def test_equal(self, model):
if not isinstance(model, SigIdentExpr):
return False
else:
return self.name == model.name
def __eq__(self, item):
return isinstance(item, SigIdentExpr) and self.name == item.name
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.name)
class SigConstExpr(SigExpression):
......@@ -210,11 +219,14 @@ class SigConstExpr(SigExpression):
def accept(self, visitor):
return visitor.visit_sig_const(self)
def test_equal(self, model):
if not isinstance(model, SigConstExpr):
return False
else:
return self.value == model.value
def __eq__(self, item):
return isinstance(item, SigConstExpr) and self.value == item.value
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.value)
class SigBotExpr(SigExpression):
......@@ -237,8 +249,11 @@ class SigBotExpr(SigExpression):
def accept(self, visitor):
return visitor.visit_sig_bot(self)
def test_equal(self, model):
return isinstance(model, SigBotExpr)
def __eq__(self, item):
return isinstance(item, SigBotExpr)
def __ne__(self, item):
return not self == item
class SigBinExpr(SigExpression):
......@@ -289,13 +304,18 @@ class SigDefaultExpr(SigBinExpr):
def accept(self, visitor):
return visitor.visit_sig_default(self)
def test_equal(self, model):
if not isinstance(model, SigDefaultExpr):
return False
else:
cond = self.left_h.test_equal(model.left_h)
cond = cond and (self.right_h.test_equal(model.right_h))
return cond
def __eq__(self, item):
return (
isinstance(item, SigDefaultExpr)
and self.left_h == item.left_h
and self.right_h == item.right_h
)
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.left_h) ^ hash(self.right_h)
class SigWhenExpr(SigBinExpr):
......@@ -312,13 +332,18 @@ class SigWhenExpr(SigBinExpr):
def accept(self, visitor):
return visitor.visit_sig_when(self)
def test_equal(self, model):
if not isinstance(model, SigWhenExpr):
return False
else:
cond = self.left_h.test_equal(model.left_h)
cond = cond and (self.right_h.test_equal(model.right_h))
return cond
def __eq__(self, item):
return (
isinstance(item, SigWhenExpr)
and self.left_h == item.left_h
and self.right_h == item.right_h
)
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.left_h) ^ hash(self.right_h)
class SigEqualExpr(SigBinExpr):
......@@ -337,13 +362,18 @@ class SigEqualExpr(SigBinExpr):
def accept(self, visitor):
return visitor.visit_sig_equal(self)
def test_equal(self, model):
if not isinstance(model, SigEqualExpr):
return False
else:
cond = self.left_h.test_equal(model.left_h)
cond = cond and (self.right_h.test_equal(model.right_h))
return cond
def __eq__(self, item):
return (
isinstance(item, SigEqualExpr)
and self.left_h == item.left_h
and self.right_h == item.right_h
)
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.left_h) ^ hash(self.right_h)
class SigDiffExpr(SigBinExpr):
......@@ -360,13 +390,18 @@ class SigDiffExpr(SigBinExpr):
def accept(self, visitor):
return visitor.visit_sig_diff(self)
def test_equal(self, model):
if not isinstance(model, SigDiffExpr):
return False
else:
cond = self.left_h.test_equal(model.left_h)
cond = cond and (self.right_h.test_equal(model.right_h))
return cond
def __eq__(self, item):
return (
isinstance(item, SigDiffExpr)
and self.left_h == item.left_h
and self.right_h == item.right_h
)
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.left_h) ^ hash(self.right_h)
class SigNotExpr(SigExpression):
......@@ -395,11 +430,17 @@ class SigNotExpr(SigExpression):
def accept(self, visitor):
return visitor.visit_sig_not(self)
def test_equal(self, model):
if not isinstance(model, SigNotExpr):
return False
else:
return self.operand.test_equal(model.operand)
def __eq__(self, item):
return (
isinstance(item, SigNotExpr)
and self.operand == item.operand
)
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.operand)
class SigEventExpr(SigExpression):
......@@ -425,11 +466,17 @@ class SigEventExpr(SigExpression):
def accept(self, visitor):
return visitor.visit_sig_event(self)
def test_equal(self, model):
if not isinstance(model, SigEventExpr):
return False
else:
return self.operand.test_equal(model.operand)
def __eq__(self, item):
return (
isinstance(item, SigEventExpr)
and self.operand == item.operand
)
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.operand)
class SigSyncBinExpr(SigBinExpr):
......@@ -467,14 +514,19 @@ class SigSyncBinExpr(SigBinExpr):
def accept(self, visitor):
return visitor.visit_sig_sync(self)
def test_equal(self, model):
if not isinstance(model, SigSyncBinExpr):
return False
else:
cond = self.left_h.test_equal(model.left_h)
cond = cond and (self.right_h.test_equal(model.right_h))
cond = cond and self.operator == model.operator
return cond
def __eq__(self, item):
return (
isinstance(item, SigSyncBinExpr)
and self.left_h == item.left_h
and self.right_h == item.right_h
and self.operator == item.operator
)
def __ne__(self, item):
return not self == item
def __hash__(self):
return hash(self.left_h) ^ hash(self.right_h) ^ hash(self.operator)
# class SigPolyBinExpr(SigBinExpr):
......
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