-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparserBool.py
38 lines (31 loc) · 1.02 KB
/
parserBool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import math
import re
class BoolEquation:
def __init__(self,eqn):
self.eqn = eqn
self.literals = None
self.convFunc()
self.exc = self.execFunc()
def convFunc(self):
mateqn = re.sub(r"([A-Z])",r"(\1)",self.eqn)
mateqn = re.sub(r"\(([A-Z])\)'",r"(not \1)",mateqn)
mateqn = re.sub(r"\+",r") or (",mateqn)
mateqn = re.sub(r"[\)][\(]" , r") and (",mateqn)
mateqn = '(' + mateqn + ')'
self.operation = mateqn
# return mateqn
def execFunc(self):
lits = re.findall(r"[A-Z]",self.eqn)
lits = list(set(lits))
lits.sort()
self.literals = lits
lits = ','.join(lits)
exc = 'def boolCir(' + lits +'):'+'return '+f'int({self.operation})'
d = {}
exec(exc,d)
return d['boolCir']
# eq1 = BoolEquation("ABC")
# func = eq1.exc