-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
210 lines (152 loc) · 5.26 KB
/
test.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import uspno9
print "Variable declaration tests\n====="
f = uspno9.VariableDecAST("f", uspno9.ValueAST(int, 10), int)
print f.to_sexpr()
g = uspno9.VariableDecAST("g", uspno9.ValueAST(int), int)
h = uspno9.VariableDecAST("h", vtype=int)
print g.to_sexpr()
print h.to_sexpr()
j = uspno9.VariableDecAST("j")
print j.to_sexpr()
print "\nif-form test\n====="
cond0 = uspno9.FunctionCallAST("<", [uspno9.VarRefAST("f", int),
uspno9.VarRefAST("g", int,
symbolic=True)],
bool)
then0 = uspno9.FunctionCallAST("print", [uspno9.ValueAST(str, "then")], str)
else0 = uspno9.FunctionCallAST("print", [uspno9.ValueAST(str, "else")], str)
if0 = uspno9.IfAST(cond0, then0, else0)
print if0.to_sexpr()
print "\ninteger logic tests\n======"
a = uspno9.ValueAST.new_integer(9)
s = uspno9.ValueAST.new_integer(10)
z = a < s
x = a > s
c = a <= s
v = a >= s
b = a != s
n = a == s
res = [z,x,c,v,b,n]
for r in res:
print r.value, r.trace, r.symbolic
print "\narithmatic tests\n====="
jj = uspno9.ValueAST.new_symbolic_integer()
cg = a + s
ch = a + cg
cj = jj + a
ck = cj + ch
print cg.value, cg.trace
print ch.value, ch.trace
print cj.value, cj.trace
print ck.value, ck.trace
from uspno9 import *
print "\nexecution tests\n====="
if0 = IfAST(ValueAST.new_bool(True), ValueAST.new_integer(10), ValueAST.new_integer(11))
if1 = IfAST(VarRefAST("foo"), ValueAST.new_integer(12), ValueAST.new_integer(13))
print "execute the following test cases:"
print if0.to_sexpr()
print if1.to_sexpr()
rootenv0 = EvalEnv({"foo": ValueAST.new_bool(True),
"bar": ValueAST.new_integer(10)})
rootenv1 = EvalEnv({"foo": ValueAST.new_symbolic_bool()})
# we don't affix the asts for now, because we're just
# going to microexecute them
vm0 = Eval(None, rootenv0)
vm1 = Eval(None, rootenv1)
results = [
vm0.microexecute(if0),
vm1.microexecute(if0),
vm0.microexecute(if1),
vm1.microexecute(if1)]
print "results: ", results
cond1 = uspno9.FunctionCallAST("<", [uspno9.ValueAST.new_integer(10),
uspno9.ValueAST.new_integer(11)],
bool)
print type(cond1)
res, stack, env = vm0.microexecute(cond1)
print res.value
print res.trace
cond2 = uspno9.FunctionCallAST("<=", [uspno9.ValueAST.new_integer(10),
uspno9.ValueAST.new_integer(11)],
bool)
print type(cond2)
print cond2.to_sexpr()
res, stack, env = vm0.microexecute(cond2)
print res.value
print res.trace
print "Concrete:"
cond3 = uspno9.FunctionCallAST("<=", [uspno9.VarRefAST("bar"),
uspno9.ValueAST.new_integer(11)],
bool)
print type(cond3)
print cond3.to_sexpr()
res = vm0.microexecute(cond3)
print res[0].value
print res[0].trace
print "Symbolic:"
cond4 = uspno9.FunctionCallAST("<=", [uspno9.VarRefAST("baz"),
uspno9.ValueAST.new_integer(11)],
bool)
print type(cond4)
print cond4.to_sexpr()
res, stack, env = vm0.microexecute(cond4)
print res.lhs.to_sexpr()
print res.rhs.to_sexpr()
print "\nwhile test\n====="
body = [
FunctionCallAST("print", [VarRefAST("bar")]),
SetValueAST("bar", FunctionCallAST("+",
[VarRefAST("bar"),
ValueAST.new_integer(1)]))]
while0 = WhileAST(cond3, BeginAST(body))
print while0.to_sexpr()
res, stack, env = vm0.microexecute(while0)
print res
print "\nfunction test\n====="
# Let's build a new function...
cond5 = FunctionCallAST("<=", [VarRefAST("bar"), VarRefAST("len")], bool)
while1 = WhileAST(cond5, BeginAST(body))
fnb0 = BeginAST([VariableDecAST("bar", ValueAST.new_integer(0)),
while1])
# need a way to represent ::void as a type...
fn0 = FunctionAST("boo", [VarRefAST("len", int)], fnb0, VoidAST)
print "Boo should not be in the environment before hand:"
print env.get_or_none("boo")
res, stack, env = vm0.microexecute(fn0)
print "Boo should be in the environment after execution:"
print env.get_or_none("boo")
print "\nBefore execution, these should be simple:"
print res
print stack
print env
(res,) = env.get_or_none("boo")
print "\nFunction name: {0}, single parameter: {1}".format(res.name, res.params)
print "function body:", res.body.to_sexpr()
print "\nLet's execute that function:"
fncall0 = FunctionCallAST("boo", [ValueAST.new_integer(1)], VoidAST)
res, stack, env = vm0.microexecute(fncall0)
print res
print stack
print env
env.walk()
print "\nANF testing...\n====="
fc0 = FunctionCallAST("*", [ValueAST.new_integer(10), ValueAST.new_integer(20)])
fc1 = FunctionCallAST("/", [ValueAST.new_integer(12), ValueAST.new_integer(4)])
fc2 = FunctionCallAST("+", [fc0, fc1])
fc3 = FunctionCallAST("-", [fc2, ValueAST.new_integer(1)])
print "original form:"
print fc2.to_sexpr()
print "\nANF variant:"
var, result = fc2.to_anf()
for v in var:
print v.to_sexpr()
print result.to_sexpr()
print "\noriginal form:"
print fc3.to_sexpr()
print "\nANF variant:"
var, result = fc3.to_anf()
for v in var:
print v.to_sexpr()
print result.to_sexpr()
cfg0 = ControlFlowGraph(None, rootenv0)
print cfg0.simple_cfg(fn0)