加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
auto_unfold.py 5.93 KB
一键复制 编辑 原始数据 按行查看 历史
swe21 提交于 2024-12-03 02:48 . add auto_unfold.py.
#!/usr/bin/python3
#-*-coding:utf-8-*-
###############################################################
## Name : auto_unfold.py
## Author : gaojiaming
## Date : Tue Jan 10 17:28:59 CST 2023
## Description:
##
##
###############################################################
import os
import sys
import re
import argparse
def input_args_proc():#{{{
parser = argparse.ArgumentParser(description="argparse info")
parser.add_argument('-o', action='store_true', default=False, help='open this script')
parser.add_argument('-d', action='store_true', default=False, help='del auto generate')
parser.add_argument('-f', help='input name')
result = parser.parse_args()
if result.o == True:
os.system("gvim %s" % __file__)
sys.exit(0)
del_gen = 0
if result.d == True:
del_gen = 1
if not result.f:
raise ValueError("No input file")
inp_file = result.f
return inp_file, del_gen
pass#}}}
def gen_unfold_main(para_list, loop_list, gen_list):#{{{
return_list = []
#print(para_list)
#print(loop_list)
#print(gen_list)
for line in gen_list:
#print(line)
re_if = re.search(r"IF\(#(.*?)#\)", line)
if_power = 0
if_str = ""
if(re_if):
if_power = 1
if_str = re_if.group(1)
line = re.sub(r"IF\(#(.*?)#\)", "", line)
i = 0
for para in para_list:
if_str = re.sub(para, str(loop_list[i]), if_str)
i += 1
#print(if_str)
if eval(if_str) == False:
#print("FALSE")
continue
while re.search(r"(.*)#(.*?)#(.*)", line):
res = re.search(r"(.*)#(.*?)#(.*)", line)
bf = res.group(1)
rp = res.group(2)
af = res.group(3)
i = 0
for para in para_list:
#print(para, loop_list[i])
rp = re.sub(para, str(loop_list[i]), rp)
#print(rp)
i += 1
if not re.search(r"^\d+$", rp):
rp = str(eval(rp))
line = bf + rp + af
return_list.append(line)
return return_list
pass#}}}
def gen_unfold(para_list, min_list, max_list, gen_list):#{{{
upfold_list = []
loop_list = min_list.copy()
zero_en = 0
zero_pos = 0
max_flag = 1
if max_list[-1] < min_list[-1]:
max_flag = 0
while True:
if max_list[-1] >= min_list[-1] and loop_list[-1] > max_list[-1]:
break
if max_list[-1] < min_list[-1] and loop_list[-1] < max_list[-1]:
break
upfold_list.extend(gen_unfold_main(para_list, loop_list, gen_list))
if zero_en != 0:
for i in range(0, zero_pos):
loop_list[i] = min_list[i]
loop_list[zero_pos] = loop_list[zero_pos]+1 if max_flag == 1 else loop_list[zero_pos]-1
zero_en = 0
zero_pos = 0
else:
loop_list[0] = loop_list[zero_pos]+1 if max_flag == 1 else loop_list[zero_pos]-1
#print("loop_list: {0}".format(loop_list))
for loop in range(1, len(loop_list)): #i j k next value [3 2 2]
loc_zero_pos = loop
loc_zero_en = 1
for sub in range(0, loop):
if max_flag == 1 and max_list[sub] - loop_list[sub] >= 1:
loc_zero_en = 0
break
if max_flag == 0 and loop_list[sub] - max_list[sub] >= 1:
loc_zero_en = 0
break
if loc_zero_en == 0:
break
else:
zero_en = loc_zero_en
zero_pos = loc_zero_pos
return upfold_list
pass#}}}
def sys_unfold(inp_file):#{{{
para_list = []
min_list = []
max_list = []
gen_list = []
upfold_list = []
return_list = []
handle = del_gen_code(inp_file)
auto_unfold_power = 0
for line in handle:
return_list.append(line.rstrip())
res_s = re.match(r"\s*/\*\s*AUTO_UNFOLD", line) #Æ¥Åä /* AUTO_UNFOLD
res_e = re.match(r"\s*\s*END\s*\*/", line) #Æ¥Åä END */
res_f = re.match(r"\s*\#for\s+(\w+)\s+(\d+)\.\.(\d+)", line) #Æ¥Åä #for i 0..6
if res_s:
auto_unfold_power = 1
elif res_e:
auto_unfold_power = 0
upfold_list.append("//AUTO_UNFOLD_START")
upfold_list.extend(gen_unfold(para_list, min_list, max_list, gen_list))
upfold_list.append("//AUTO_UNFOLD_END")
return_list.extend(upfold_list)
upfold_list = []
para_list = []
min_list = []
max_list = []
gen_list = []
elif auto_unfold_power == 1 and res_f:
para_list.append(res_f.group(1))
min_list.append(int(res_f.group(2)))
max_list.append(int(res_f.group(3)))
elif auto_unfold_power == 1:
gen_list.append(line.rstrip())
return return_list
pass#}}}
def del_gen_code(inp_file):#{{{
tmp_list = []
shot_en = 0
with open(inp_file, "r") as hd:
handle = hd.readlines()
for line in handle:
line = line.rstrip()
if re.search(r"//AUTO_UNFOLD_START", line):
shot_en = 1
if shot_en == 0:
tmp_list.append(line)
if re.search(r"//AUTO_UNFOLD_END", line):
shot_en = 0
return tmp_list
pass#}}}
def main():
return_list = []
inp_file, del_gen = input_args_proc()
if del_gen == 1:
return_list = del_gen_code(inp_file)
else:
return_list = sys_unfold(inp_file)
for line in return_list:
print(line)
pass
pass
if __name__ == "__main__":
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化