-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbce.py
66 lines (52 loc) · 1.93 KB
/
bce.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import pcn
import urp
class BCE(object):
def __init__(self, inPath, outPath):
self.inPath = inPath
self.outPath = outPath
self.eqs = {}
self.operations = {
"r": self.read_pcn,
"!": self.do_not,
"+": self.do_or,
"&": self.do_and,
"p": self.write_pcn,
"xor": self.do_xor,
"dx": self.do_dx,
"cx": self.do_cx,
"sx": self.do_sx,
"q": self.quit
}
self.done = False
def process(self, commandFilePath):
with open(commandFilePath, "r") as f:
for line in f:
command, *args = line.split()
self.operations[command](*args)
if self.done:
return
def read_pcn(self, fNum):
_, self.eqs[fNum] = pcn.parse(os.path.join(self.inPath, fNum + ".pcn"))
def write_pcn(self, fNum):
pcn.write_pcn(os.path.join(
self.outPath, fNum + ".pcn"), self.eqs[fNum])
def do_not(self, resultNum, inNum):
self.eqs[resultNum] = urp.complement(self.eqs[inNum])
def do_or(self, resultNum, leftNum, rightNum):
self.eqs[resultNum] = urp.cubes_or(
self.eqs[leftNum], self.eqs[rightNum])
def do_and(self, resultNum, leftNum, rightNum):
self.eqs[resultNum] = urp.cubes_and(
self.eqs[leftNum], self.eqs[rightNum])
def do_xor(self, resultNum, leftNum, rightNum):
self.eqs[resultNum] = urp.cubes_xor(
self.eqs[leftNum], self.eqs[rightNum])
def do_dx(self, resultNum, inNum, x):
self.eqs[resultNum] = urp.boolDiff(self.eqs[inNum], int(x))
def do_cx(self, resultNum, inNum, x):
self.eqs[resultNum] = urp.consensus(self.eqs[inNum], int(x))
def do_sx(self, resultNum, inNum, x):
self.eqs[resultNum] = urp.smoothing(self.eqs[inNum], int(x))
def quit(self):
self.done = True