-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckin2.py
95 lines (66 loc) · 2.27 KB
/
checkin2.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
import sys
import minic.minic_ast as mc
import minic.c_ast_to_minic as ctomc
from pycparser import parse_file
from pycparser.c_ast import *
class AllVariables(NodeVisitor):
def __init__(self):
self.var_all = []
self.var_written = []
def visit_ID(self, Id):
if not Id.name in self.var_all:
self.var_all.append(Id.name)
def getAllVariables(self):
return self.var_all
def getWrittenVariables(self):
return self.var_written
class WrittenVariables(NodeVisitor):
def __init__(self):
self.var_all = []
self.var_written = []
def visit_Assignment(self, assignment):
if isinstance(assignment.lvalue, mc.ID):
self.var_written.append(assignment.lvalue.name)
if isinstance(assignment.lvalue, mc.ArrayRef):
self.var_written.append(assignment.lvalue.name.name)
self.generic_visit(assignment)
def getWrittenVariables(self):
return self.var_written
class FunctionPrototype():
def __init__(self, written, undef):
self.wrt_list = written
self.undef_list = undef
def __str__(self):
arg = ", ".join(self.wrt_list)
out = ", ".join(self.undef_list)
fp = "fun block_function(" + arg + ") " + "returns " + out
return fp
if __name__=="__main__":
filename = './project3inputs/p3_input4'
#filename = sys.argv[1]
txt = ''
with open(filename) as f:
txt = f.read()
txt = 'void wrapper(){ \n' + txt + '\n}'
c_filename = filename + '.c'
c_file = open(c_filename, 'w')
c_file.write(txt)
c_file.close()
ast = parse_file(c_filename)
mc_ast = ctomc.transform(ast)
allvar = AllVariables()
allvar.visit(mc_ast)
allvar_list = allvar.getAllVariables()
print "All:", allvar_list
writevar = WrittenVariables()
writevar.visit(mc_ast)
wrtvar_list = writevar.getWrittenVariables()
print "Written:", wrtvar_list
undef_list = []
for var in allvar_list:
if not var in wrtvar_list:
undef_list.append(var)
print "Undefined", undef_list
print
fp = FunctionPrototype(wrtvar_list, undef_list)
print fp