-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculatorInterface.py
103 lines (92 loc) · 2.52 KB
/
CalculatorInterface.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sys
ans = 0;
msglist = []
tm = "\n\ttransMonitor "
rinr = tm + "(Recv Inr)"
sinr = tm + "(Send Inr)"
rinl = tm + "(Recv Inl)"
sinl = tm + "(Send Inl)"
rend = tm + "(Recv End)"
send = tm + "(Send End)"
def recv1nat(n=None):
if n is None:
sys.stdout.write("\nEnter a number ")
n = int(raw_input())
for i in xrange(n):
msglist.append(rinr)
msglist.append(rinl)
msglist.append(rend)
return n
def send1nat(n):
for i in xrange(n):
msglist.append(sinr)
msglist.append(sinl)
msglist.append(send)
func_code = """
witness_calc :: State (Maybe St) Bool
witness_calc = do
\tinitMonitor calculator"""
terminate = False
while(not terminate):
print "\nmenu"
b = int(raw_input())
if (b==0):
msglist.append(rinr)
msglist.append(send)
terminate = True
else:
msglist.append(rinl)
recv1nat(b)
if (b==1): #add1
msglist.append(sinl)
msglist.append(sinl)
msglist.append(sinl)
n1 = recv1nat()
ans = ans + n1
send1nat(ans)
elif (b==2): #add2
msglist.append(rinl)
msglist.append(sinl)
msglist.append(sinl)
msglist.append(sinr)
n1 = recv1nat()
n2 = recv1nat()
ans = n1 + n2
send1nat(ans)
elif (b==3): #mul1
msglist.append(rinl)
msglist.append(sinl)
msglist.append(sinr)
msglist.append(sinl)
n1 = recv1nat()
ans = ans * n1
send1nat(ans)
elif (b==4): #mul2
msglist.append(rinl)
msglist.append(sinl)
msglist.append(sinr)
msglist.append(sinr)
n1 = recv1nat()
n2 = recv1nat()
ans = n1 * n2
send1nat(ans)
elif (b==5): #exp1
msglist.append(rinl)
msglist.append(sinr)
msglist.append(sinl)
n1 = recv1nat()
ans = pow(ans,n1)
send1nat(ans)
elif (b==6): #exp2
msglist.append(rinl)
msglist.append(sinr)
msglist.append(sinr)
n1 = recv1nat()
n2 = recv1nat()
ans = pow(n1,n2)
send1nat(ans)
else:
sys.exit(1) #Eventually move this to the Haskell part.
print ("ANSWER = " + str(ans))
func_code = func_code + "".join(msglist) + "\n\nsuccess_calc = witness_calc >> isSuccess"
print "\n\n" + func_code