Skip to content

Commit

Permalink
added xor support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Dec 16, 2022
1 parent ba3e982 commit 9821f6b
Showing 1 changed file with 166 additions and 1 deletion.
167 changes: 166 additions & 1 deletion logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def __str__(self):
return "*"


class _XOR:
def __str__(self):
return "^"


class _NEG:
def __str__(self):
return "~"
Expand All @@ -28,6 +33,7 @@ def __str__(self):

ADD = _ADD()
MUL = _MUL()
XOR = _XOR()
NEG = _NEG()
OB = _OB()
CB = _CB()
Expand Down Expand Up @@ -59,6 +65,10 @@ def __add__(self, other):
eq.eq.append(OB)
eq.eq.extend(other.eq)
eq.eq.append(CB)

eq.sub_eqs.extend(other.sub_eqs)
eq.sub_eqs.append(other)

return eq

raise TypeError("...")
Expand All @@ -85,6 +95,10 @@ def __radd__(self, other):
eq.eq.append(CB)
eq.eq.append(ADD)
eq.eq.append(self)

eq.sub_eqs.extend(other.sub_eqs)
eq.sub_eqs.append(other)

return eq

raise TypeError("...")
Expand All @@ -111,6 +125,10 @@ def __mul__(self, other):
eq.eq.append(OB)
eq.eq.extend(other.eq)
eq.eq.append(CB)

eq.sub_eqs.extend(other.sub_eqs)
eq.sub_eqs.append(other)

return eq

raise TypeError("...")
Expand All @@ -137,6 +155,70 @@ def __rmul__(self, other):
eq.eq.append(CB)
eq.eq.append(ADD)
eq.eq.append(self)

eq.sub_eqs.extend(other.sub_eqs)
eq.sub_eqs.append(other)

return eq

raise TypeError("...")

def __xor__(self, other):
# self * other
eq = Equation()

if isinstance(other, int):
eq.eq.append(self)
eq.eq.append(XOR)
eq.eq.append(1 if other else 0)
return eq

if isinstance(other, Variable):
eq.eq.append(self)
eq.eq.append(XOR)
eq.eq.append(other)
return eq

if isinstance(other, Equation):
eq.eq.append(self)
eq.eq.append(XOR)
eq.eq.append(OB)
eq.eq.extend(other.eq)
eq.eq.append(CB)

eq.sub_eqs.extend(other.sub_eqs)
eq.sub_eqs.append(other)

return eq

raise TypeError("...")

def __rxor__(self, other):
# other * self
eq = Equation()

if isinstance(other, int):
eq.eq.append(1 if other else 0)
eq.eq.append(XOR)
eq.eq.append(self)
return eq

if isinstance(other, Variable):
eq.eq.append(other)
eq.eq.append(MUL)
eq.eq.append(self)
return eq

if isinstance(other, Equation):
eq.eq.append(OB)
eq.eq.extend(other.eq)
eq.eq.append(CB)
eq.eq.append(ADD)
eq.eq.append(self)

eq.sub_eqs.extend(other.sub_eqs)
eq.sub_eqs.append(other)

return eq

raise TypeError("...")
Expand Down Expand Up @@ -218,6 +300,7 @@ def generate_truth_table(self):
eval_string.replace("+", "or")
.replace("*", "and")
.replace("~", "not")
.replace("^", "!=")
)
else 0
)
Expand All @@ -240,6 +323,7 @@ def generate_truth_table(self):
eval_string.replace("+", "or")
.replace("*", "and")
.replace("~", "not")
.replace("^", "!=")
)
else 0
)
Expand Down Expand Up @@ -433,6 +517,86 @@ def __rmul__(self, other):

raise TypeError("...")

def __xor__(self, other):
# self + other
eq = Equation()

if isinstance(other, int):
eq.eq.extend(self.eq)
eq.eq.append(XOR)
eq.eq.append(1 if other else 0)
return eq

if isinstance(other, Variable):
eq.eq.extend(self.eq)
eq.eq.append(XOR)
eq.eq.append(other)
return eq

if isinstance(other, Equation):
# adding existing operations
eq.eq.append(OB)
eq.eq.extend(self.eq)
eq.eq.append(CB)

# add operator
eq.eq.append(XOR)

# adding other's operations
eq.eq.append(OB)
eq.eq.extend(other.eq)
eq.eq.append(CB)

# adding sub equations
eq.sub_eqs.append(self)
eq.sub_eqs.extend(self.sub_eqs)
eq.sub_eqs.append(other)
eq.sub_eqs.extend(other.sub_eqs)

return eq

raise TypeError("...")

def __rxor__(self, other):
# other + self
eq = Equation()

if isinstance(other, int):
eq.eq.append(1 if other else 0)
eq.eq.append(XOR)
eq.eq.extend(self.eq)
return eq

if isinstance(other, Variable):
eq.eq.append(other)
eq.eq.append(XOR)
eq.eq.extend(self.eq)
return eq

if isinstance(other, Equation):
# adding other's operations
eq.eq.append(OB)
eq.eq.extend(other.eq)
eq.eq.append(CB)

# add operator
eq.eq.append(XOR)

# adding existing operations
eq.eq.append(OB)
eq.eq.extend(self.eq)
eq.eq.append(CB)

# adding sub equations
eq.sub_eqs.append(self)
eq.sub_eqs.extend(self.sub_eqs)
eq.sub_eqs.append(other)
eq.sub_eqs.extend(other.sub_eqs)

return eq

raise TypeError("...")

def __invert__(self):
# ~self
eq = Equation()
Expand Down Expand Up @@ -461,5 +625,6 @@ def __str__(self):
y = Variable("y")
z = Variable("z")

eq = (y * z) + (x * y)
eq = (y * z) ^ (x * y)
print(eq)
eq.display_table()

0 comments on commit 9821f6b

Please sign in to comment.