reduce: improved removing of code blocks. loop until nothing is removed.

This commit is contained in:
Daniel Marjamäki 2016-01-25 13:41:27 +01:00
parent 0fb9ab7b4a
commit 620c6e8f1c
1 changed files with 89 additions and 29 deletions

View File

@ -84,6 +84,21 @@ def replaceandrun2(what, filedata, i, line1, line2):
filedata[i] = bak1
filedata[i+1] = bak2
def clearandrun(what, filedata, i1, i2):
print(what + ' ' + str(i1+1) + '/' + str(len(filedata)) + '..')
filedata2 = list(filedata)
i = i1
while i <= i2 and i < len(filedata2):
filedata2[i] = ''
i = i + 1
writefile(FILE, filedata2)
if runtool() == True:
print('pass')
writefile(BACKUPFILE, filedata2)
return filedata2
print('fail')
return filedata
def removecomments(filedata):
for i in range(len(filedata)):
line = filedata[i]
@ -184,6 +199,43 @@ def removeemptyblocks(filedata):
if checkpar(fd1) and fd1.endswith('{') and fd2 == '}':
replaceandrun2('remove block', filedata, i, '', '')
def removeblocks(filedata):
if len(filedata) < 3:
return filedata
for i in range(len(filedata)):
strippedline = filedata[i].strip()
if len(strippedline)==0:
continue
if strippedline[-1] != '}' and strippedline[-1] != ';':
continue
i1 = i + 1
while i1 < len(filedata) and filedata[i1].startswith('#'):
i1 = i1 + 1
i2 = i1
indent = 0
while i2 < len(filedata):
for c in filedata[i2]:
if c == '}':
indent = indent - 1
if indent <= 0:
break
elif c == '{':
indent = indent + 1
i2 = i2 + 1
if i2 == i1 or i2 >= len(filedata):
continue
if filedata[i2].strip() != '}' and filedata[i2].strip() != '};':
continue
if indent < 0:
i2 = i2 - 1
filedata = clearandrun('remove codeblock', filedata, i1, i2)
return filedata
def removenamespaces(filedata):
if len(filedata) < 3:
return filedata
@ -208,26 +260,20 @@ def removenamespaces(filedata):
if strippedline.find('}') < 0 and strippedline.find('{') == len(strippedline)-1:
i2 = i + 1
level = 1
while i2 < len(filedata) and level > 0:
indent = 1
while i2 < len(filedata) and indent > 0:
#print(str(i2)+':'+str(level)+':'+filedata[i2])
for c in filedata[i2]:
if c == '}':
level = level - 1
if level <= 0:
indent = indent - 1
if indent <= 0:
break
elif c == '{':
level = level + 1
indent = indent + 1
i2 = i2 + 1
if level == 0 and (filedata[i2-1].strip().endswith('}') or filedata[i2-1].strip().endswith('};')):
if indent == 0 and (filedata[i2-1].strip().endswith('}') or filedata[i2-1].strip().endswith('};')):
#print(str(i)+';'+str(i2))
filedata2 = list(filedata)
ii = i
while ii < i2:
filedata2[ii] = ''
ii = ii + 1
if replaceandrun('remove codeblock', filedata2, i, ''):
filedata = filedata2
filedata = clearandrun('remove codeblock', filedata, i, i2-1)
return filedata
# reduce..
@ -242,23 +288,37 @@ f.close()
writefile(BACKUPFILE, filedata)
print('remove comments...')
removecomments(filedata)
print('combine lines..')
combinelines(filedata)
print('remove namespaces...')
filedata = removenamespaces(filedata)
print('remove includes...')
removeincludes(filedata)
print('remove line...')
removeline(filedata)
print('remove includes...')
removeincludes(filedata)
print('remove empty blocks...')
removeemptyblocks(filedata)
print('remove namespaces...')
filedata = removenamespaces(filedata)
while True:
filedata1 = list(filedata)
print('remove comments...')
removecomments(filedata)
print('remove includes...')
removeincludes(filedata)
print('remove empty blocks...')
removeemptyblocks(filedata)
print('remove blocks...')
filedata = removeblocks(filedata)
print('combine lines..')
combinelines(filedata)
print('remove namespaces...')
filedata = removenamespaces(filedata)
print('remove line...')
removeline(filedata)
# if filedata and filedata2 are identical then stop
if len(filedata1) == len(filedata):
i = 0
while i < len(filedata1):
if filedata[i] != filedata1[i]:
break
if i == len(filedata1):
break
writefile(FILE, filedata)