-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·97 lines (85 loc) · 3.47 KB
/
main.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
from lexer_rules import lexer
from parser_rules import parser
# 具体算子 cpuCompute 路径,只支持单文件检查,不能文件跳转
count_file_path = '/home/cambricon/vscode/TestCode/code1.c++'
with open(count_file_path, 'r', encoding='utf-8') as file:
all_lines = file.readlines()
# 扫描 [// count_ops_begin, // count_ops_end] 之间的文件
# 可以注释掉不想统计计算量的部分
codeLines = []
flag = 0
for line in all_lines:
if "// count_ops_begin" in line:
flag = 1
elif "// count_ops_end" in line:
flag = 0
codeLines.append("\n")
if flag == 1:
codeLines.append(line)
# 合并行
code = ""
for item in codeLines:
code += item
# 语法解析,解析完后会把结果存储到 theoryOps_store 中
parser.parse(code, lexer)
# 显示状态,0 为不显示,1 为以注释显示,2 为以代码显示
# stmt_show 对应单语句的计算量
# block_show 为语句作用域的计算量,但作用域发生变化时就统计累计计算量
# control_show 是除顺序语句之外语句的计算量(if, for, while, switch 等)
def show_theoryOps(stmt_show = 0, block_show = 2, control_show = 0):
from parser_rules import stmt_theoryOps_store, \
block_theoryOps_store, \
control_theoryOps_store
# 逐语句和逐块不能同时以代码显示
assert(not(stmt_show == 2 and block_show == 2))
block_show += 3
control_show += 6
show_ops_count = []
if block_show % 3 > 0:
for key in block_theoryOps_store:
show_ops_count.append([key, block_theoryOps_store[key], block_show])
if control_show % 3 > 0:
for key in control_theoryOps_store:
show_ops_count.append([key, control_theoryOps_store[key], control_show])
if stmt_show > 0:
for key in stmt_theoryOps_store:
show_ops_count.append([key, stmt_theoryOps_store[key], stmt_show])
show_ops_count = sorted(show_ops_count, key=lambda x: x[0], reverse=True)
return show_ops_count
for item in show_theoryOps():
if item[1] == 0:
continue
if item[2] == 1:
insert_str = "// last stmt: theory_ops_ += " + str(item[1]) + ";\n"
codeLines.insert(item[0], insert_str)
elif item[2] == 2:
insert_str = "theory_ops_ += " + str(item[1]) + "; // last stmt\n"
codeLines.insert(item[0], insert_str)
elif item[2] == 4:
insert_str = "// cur block: theory_ops_ += " + str(item[1]) + ";\n"
codeLines.insert(item[0], insert_str)
elif item[2] == 5:
insert_str = "theory_ops_ += " + str(item[1]) + "; // cur block\n"
codeLines.insert(item[0], insert_str)
elif item[2] == 7:
insert_str = "// cur control: theory_ops_ += " + str(item[1]) + ";\n"
codeLines.insert(item[0], insert_str)
elif item[2] == 8:
insert_str = "theory_ops_ += " + str(item[1]) + "; // cur control\n"
codeLines.insert(item[0], insert_str)
# 按当前时间输出文件
from datetime import datetime
current_time = datetime.now()
output_name = current_time.strftime("%Y-%m-%d-%H:%M") + ".cpp"
with open(output_name, 'w', encoding='utf-8') as file:
file.writelines(codeLines)
# 调用 clang-format 刷新对齐格式
import subprocess
clang_format_command = [
'clang-format',
'-style={BasedOnStyle: google, PointerAlignment: Right, \
SortIncludes: false, ColumnLimit: 80}',
'-i',
output_name
]
subprocess.run(clang_format_command, check=True)