-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvarAnalyzer.py
More file actions
355 lines (327 loc) · 11.8 KB
/
varAnalyzer.py
File metadata and controls
355 lines (327 loc) · 11.8 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import numpy as np
import argparse
import sys
from shutil import copyfile
import varlibs as vl
import hreader as hrd
import subprocess
"""
Currently, the analyzer does not work well with logical and character variables,
which may be regarded as either integers or real variables.
"""
def write_var(wfile,varlist,prefix=''):
"""
write list varlist to wfile
"""
kk=0
if varlist:
s=prefix
for c in varlist:
s=s+c
if len(s)>62:
wfile.write(s)
wfile.write('\n')
s=prefix
else:
s=s+','
if s and len(s) > len(prefix):
if s[-1]==',':
s=s[:-1]
wfile.write(s)
wfile.write('\n')
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--ff77', dest="fold", type=str, nargs=1, default=[""],
help='the fortran 77 file to be labeled')
parser.add_argument('--hlist',dest="hlist",type=str,nargs=1,default=[""],
help='list of header files to be loaded')
args = parser.parse_args()
f77f=args.fold[0]
hlist=args.hlist[0]
#create the list of head files to be loaded
hlfs=[]
subprocess.run(["mkdir -p work"])
with open(hlist,"r") as foldf:
line = foldf.readline()
hf= line.strip()
while (hf):
hlfs.append(hf)
line = foldf.readline()
hf=line.strip()
#real_list,int_list,char_list,bool_list=hrd.load_heads(hl)
#if len(real_list)>0:
# print('real:')
# for rl in real_list:
# print(rl)
#if len(char_list)>0:
# print('char')
# for rl in char_list:
# print(rl)
#if len(int_list)>0:
# print('int')
# for rl in int_list:
# print(rl)
#if len(bool_list)>0:
# print('bool')
# for rl in bool_list:
# print(rl)
real_list_loc_decl=[]
int_list_loc_decl=[]
char_list_loc_decl=[]
bool_list_loc_decl=[]
for j in range(26):
real_list_loc_decl.append([])
int_list_loc_decl.append([])
char_list_loc_decl.append([])
bool_list_loc_decl.append([])
real_list_loc_use=[]
int_list_loc_use=[]
for j in range(26):
real_list_loc_use.append([])
int_list_loc_use.append([])
print("process file "+f77f)
list_intents=[]
list_locdecl=[]
with open(f77f,"r") as foldf:
line = foldf.readline()
line0= line.rstrip()
start=0
hl=[]
var_lhst=[]
while line:
tline=line.rstrip()
if "include" in tline:
# store include lines
start=1
hl.append(tline.strip())
else:
if start==0:
#read lines before include
if (len(line.strip())==0) or vl.is_comment(line):
# an empty line or comment line
line = foldf.readline()
continue
if vl.is_continue(line):
# is a continuing line
line0=line0+line[6:].strip()
else:
#not a cotinuing line or comment line, last line has finished
if 'intent' in line0.lower():
line1=vl.rm_arr_indexl(line0)
list_intents.append(line1)
line0=line.rstrip()
line = foldf.readline()
continue
elif start==1:
#the intent below is to handel situations that there are
#subroutines inside a big subroutine
if 'intent' in line0.lower():
line00=line0.upper()
line1=vl.rm_arr_indexl(line00)
list_intents.append(line1)
for line1 in list_intents:
head,stem=vl.hline_break(line1.upper())
slist=vl.stem_break(stem)
nl=len(slist)
if 'CHARACTER' in head:
for jl in range(nl):
loc=ord(slist[jl][0])-ord('A')
char_list_loc_decl[loc].append(slist[jl])
elif 'REAL' in head:
for jl in range(nl):
loc=ord(slist[jl][0])-ord('A')
real_list_loc_decl[loc].append(slist[jl])
elif 'LOGICAL' in head:
for jl in range(nl):
loc=ord(slist[jl][0])-ord('A')
bool_list_loc_decl[loc].append(slist[jl])
elif 'INTEGER' in head:
for jl in range(nl):
loc=ord(slist[jl][0])-ord('A')
int_list_loc_decl[loc].append(slist[jl])
#list of head files is created, load them
hls=vl.get_include_files(hl,hlfs)
real_list_head,int_list_head,char_list_head,bool_list_head=hrd.load_heads(hls)
#now it is ready to sort the locally declared variables
start=2
if (len(line.strip())==0) or vl.is_comment(line):
line0=""
else:
line0=line.rstrip()
line=foldf.readline()
continue
#the the excutation section is read in
if start == 2:
if "execution begins here" in tline:
#turn on execution part
start=3
if '::' in line0:
line00=line0.upper()
if not 'PARAMETER' in line00:
line1=vl.rm_arr_indexl(line00)
else:
line1=line00
list_locdecl.append(line1)
for line1 in list_locdecl:
if not '::' in line1:
continue
head,stem=vl.hline_break(line1)
if 'PARAMETER' in line1:
var_lhs1,slist=vl.var_break(stem)
else:
slist=vl.stem_break(stem)
nl=len(slist)
if 'CHARACTER' in head:
for jl in range(nl):
loc=ord(slist[jl][0])-ord('A')
char_list_loc_decl[loc].append(slist[jl])
# vl.add_varlist(char_list,slist)
elif 'REAL' in head:
for jl in range(nl):
loc=ord(slist[jl][0])-ord('A')
real_list_loc_decl[loc].append(slist[jl])
# vl.add_varlist(real_list,stem)
elif 'LOGICAL' in head:
for jl in range(nl):
loc=ord(slist[jl][0])-ord('A')
bool_list_loc_decl[loc].append(slist[jl])
# vl.add_varlist(bool_list,stem)
elif 'INTEGER' in head:
for jl in range(nl):
loc=ord(slist[jl][0])-ord('A')
int_list_loc_decl[loc].append(slist[jl])
line = foldf.readline()
line0=''
continue
#declaration
if (len(line.strip())==0) or vl.is_comment(line):
# an empty line or comment line
line = foldf.readline()
continue
if vl.is_continue(line):
line0=line0+line[6:].strip()
else:
if line0.strip():
line00=line0.upper()
#add to the list of locally declared vars
line1=vl.rm_arr_indexl(line00)
list_locdecl.append(line1)
line0=line.rstrip()
elif start == 3:
if (len(line.strip())==0) or vl.is_comment(line):
# an empty line or
line = foldf.readline()
continue
if vl.is_continue(line):
line0=line0+line[6:].strip()
else:
#not a continuous line
if line0.strip():
line00=line0.upper().strip()
#analyze line0
label,line1=vl.rm_label(line00)
# if len(label.strip())>0:
# print(label)
if not vl.is_special(line1):
var_lhs,varlist=vl.var_break(line1)
if var_lhs:
if not var_lhs in var_lhst:
var_lhst.append(var_lhs)
for ss in varlist:
loc=ord(ss[0])-ord('A')
if (ord(ss[0])-ord('I'))>=0 and (ord(ss[0])-ord('N'))<=0:
if not ss in int_list_loc_use[loc]:
int_list_loc_use[loc].append(ss)
else:
if not ss in real_list_loc_use[loc]:
real_list_loc_use[loc].append(ss)
#this a labeled lin
#get a new line
line0=line.rstrip()
#break the line into list variables
line = foldf.readline()
"""
Identify variables that have not been declared
"""
real_undecl=[]
int_undecl=[]
for jj in range(26):
for ss in real_list_loc_use[jj]:
if ss not in real_list_loc_decl[jj] \
and ss not in real_list_head[jj] \
and ss not in bool_list_head[jj] \
and ss not in bool_list_loc_decl[jj] \
and ss not in char_list_head[jj] \
and ss not in char_list_loc_decl[jj]:
real_undecl.append(ss)
for ss in int_list_loc_use[jj]:
if ss not in int_list_loc_decl[jj] \
and ss not in int_list_head[jj] \
and ss not in bool_list_loc_decl[jj] \
and ss not in bool_list_head[jj] \
and ss not in char_list_loc_decl[jj] \
and ss not in char_list_head[jj]:
int_undecl.append(ss)
ff=f77f.split('/')
frpt=ff[-1]+'.var'
with open(frpt,"w") as frptf:
frptf.write('Summary of variables in '+f77f+'\n')
frptf.write('='*90+'\n')
frptf.write('Variables not declared\n')
frptf.write('-'*90+'\n')
frptf.write('REAL:\n')
frptf.write('-'*90+'\n')
write_var(frptf,real_undecl,' real(r8) :: ')
frptf.write('-'*90+'\n')
frptf.write('INTEGER:\n')
frptf.write('-'*90+'\n')
write_var(frptf,int_undecl,' integer :: ')
frptf.write('='*90+'\n')
frptf.write('Value changing variables\n')
write_var(frptf,var_lhst)
frptf.write('='*90+'\n')
frptf.write('head files included\n')
for hf in hlfs:
frptf.write(hf+'\n')
frptf.write('='*90+'\n')
#bool_list_head
frptf.write('Locally declared variables\n')
frptf.write('-'*90+'\n')
frptf.write('REAL:\n')
frptf.write('-'*90+'\n')
for l in range(26):
write_var(frptf,real_list_loc_decl[l])
frptf.write('-'*90+'\n')
frptf.write('INTEGER:\n')
frptf.write('-'*90+'\n')
kk=0
for l in range(26):
write_var(frptf,int_list_loc_decl[l])
frptf.write('-'*90+'\n')
frptf.write('CHARACTER:\n')
frptf.write('-'*90+'\n')
for l in range(26):
write_var(frptf,char_list_loc_decl[l])
#char_list_decl
frptf.write('-'*90+'\n')
frptf.write('LOGICAL:\n')
frptf.write('-'*90+'\n')
for l in range(26):
write_var(frptf,bool_list_loc_decl[l])
#bool_list_decl
frptf.write('='*90+'\n')
frptf.write('Variables used in the file\n')
frptf.write('-'*90+'\n')
frptf.write('REAL:\n')
frptf.write('-'*90+'\n')
for l in range(26):
# frptf.write(chr(ord('A')+l)+': ')
# if l == ord('K')-ord('A'):
# print(real_list_loc_use[l])
write_var(frptf,real_list_loc_use[l])
#real_list_loc_use
frptf.write('-'*90+'\n')
frptf.write('INTEGER:\n')
frptf.write('-'*90+'\n')
for l in range(26):
write_var(frptf,int_list_loc_decl[l])
#int_list_loc_use