lep_objects.py
'''Logical Expression Parser Objects.'''
class term (object):
'''Terminal Expression Object (i.e. a value of some kind)'''
def __init__ (self, name, value=False):
self.n = name
self.v = value
def __str__ (self):
return self.n
def __repr__ (self):
return str(self.v)
def __call__ (self):
return self.v
def visit (self, function):
function(self)
class expression (term):
'''Expression Object'''
def __init__ (self, oper, term1, term2):
super(self.__class__,self).__init__('@')
self.t1 = term1
self.t2 = term2
self.oper = oper
def __str__ (self):
if self.t2:
s = '(%s %s %s)'
t = (self.t1, self.oper, self.t2)
else:
s = 'NOT %s'
t = self.t1
return s % t
def __call__ (self):
self.v = self.oper(self.t1, self.t2)
return self.v
def visit (self, function):
self.t1.visit(function)
self.t2.visit(function)
function(self)
class operator (object):
'''Operator base class.'''
def __init__ (self):
self.v = None
def __str__ (self):
return 'operator'
def __call__ (self, t1, t2):
return False
class oper_not (object):
'''NOT Operator class.'''
def __init__ (self):
super(self.__class__,self).__init__()
def __str__ (self):
return 'NOT'
def __call__ (self, t1, t2):
return (not t1())
class oper_or (object):
'''OR Operator class.'''
def __init__ (self):
super(self.__class__,self).__init__()
def __str__ (self):
return 'OR'
def __call__ (self, t1, t2):
return (t1() or t2())
class oper_and (object):
'''AND Operator class.'''
def __init__ (self):
super(self.__class__,self).__init__()
def __str__ (self):
return 'AND'
def __call__ (self, t1, t2):
return (t1() and t2())
class oper_xor (object):
'''XOR Operator class.'''
def __init__ (self):
super(self.__class__,self).__init__()
def __str__ (self):
return 'XOR'
def __call__ (self, t1, t2):
v1 = t1()
v2 = t2()
return (v1 or v2) and not (v1 and v2)
class oper_nor (object):
'''NOR Operator class.'''
def __init__ (self):
super(self.__class__,self).__init__()
def __str__ (self):
return 'NOR'
def __call__ (self, t1, t2):
return not (t1() or t2())
class oper_nand (object):
'''NAND Operator class.'''
def __init__ (self):
super(self.__class__,self).__init__()
def __str__ (self):
return 'NAND'
def __call__ (self, t1, t2):
return not (t1() and t2())
'''eof'''