Skip to content

Commit 4fbc955

Browse files
committed
同步一下
1 parent 3e88ac4 commit 4fbc955

File tree

3 files changed

+119
-52
lines changed

3 files changed

+119
-52
lines changed

main.py

Lines changed: 88 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,100 @@ def getParams() -> list:
3535
if param_verbose == "verbose":
3636
verbose_enabled = True
3737

38-
filein = "bkscr/多行.bkscr"
39-
fileout = "compile_out/out.txt" # 备份 fileout = filein.replace(".bkscr",".txt")
38+
# filein = "bkscr/多行.bkscr"
39+
# filein = os.path.split(param_input_dir)[1] # 多好.bkscr
40+
# fileout = "",filein.replace(".bkscr",".txt")
41+
# fileout = "compile_out/out.txt" # 备份 fileout = filein.replace(".bkscr",".txt")
4042

4143
ColorPrint.print_verbose_once()
4244
ColorPrint.print_compile_once()
4345
ColorPrint.print_verbose_hint("输入文件夹 -> " + param_input_dir)
44-
ColorPrint.print_verbose_hint("输出文件 -> " + fileout)
46+
# ColorPrint.print_verbose_hint("输出文件 -> " + fileout)
47+
48+
def getAllBkscr(p_dir) -> list:
49+
L = []
50+
for root, dirs, files in os.walk(p_dir):
51+
#获得所有bkscr文件
52+
for file in files:
53+
if os.path.splitext(file)[1] == ".bkscr":
54+
if file != "macro.bkscr":
55+
L.append(os.path.join(root, file))
56+
return L
57+
58+
# 先预处理一变所有文件的所有行的开头和结尾的空格 因为这个空格是非必要的,而且三种格式可能都存在首尾空格,所以去除空格并不会影响游戏代码
59+
def preCompile(eachFile: str) -> list:
60+
result = [] # string []
61+
# 针对每个文件,读取每个文件的所有行
62+
each_lines:str = eachFile.readlines()
63+
for lines in each_lines:
64+
result.append(预处理.处理("单行宏",lines,verbose_enabled))
65+
ColorPrint.print_compile("预处理结果",str(result))
66+
67+
return result
68+
# 过程 getAllBkscr -> preCompile -> compile
69+
def compileProject(p_dir,p_verbose):
70+
all_FilePaths:list = getAllBkscr(p_dir)
71+
72+
for each_FilePath in all_FilePaths:
73+
with open(each_FilePath,"r",encoding="utf-8") as eachFile:
74+
# filein = os.path.split(param_input_dir)[1] # 多好.bkscr
75+
# fileout filein.replace(".bkscr",".txt")
76+
p = os.path.split(each_FilePath)
77+
output_name = "compile_out/" + p[1].replace(".bkscr",".txt")
78+
FileOut = open(output_name,"w",encoding="utf-8")
79+
# 这里的预处理结果是当前的一行
80+
每一行的预处理结果:list = preCompile(eachFile)
81+
# ColorPrint.print_compile("预处理结果",str(预处理结果))
82+
for index,line in enumerate(每一行的预处理结果):
83+
currentScanLine = index + 1
84+
if line[0] == "@":
85+
lineToken:list = sp.parse(line,currentScanLine,verbose_enabled)
86+
#print("缓冲区",buffer_line)
87+
for element in lineToken:
88+
# print(currentScanLine, buffer_array)
89+
90+
FileOut.write(element + "\n")
91+
# result.append(buffer_line)
92+
if line[0] == "[":
93+
lineToken:list = mp.parse(line,currentScanLine,verbose_enabled)
94+
for element in lineToken:
95+
# #print(currentScanLine, buffer_array)
96+
FileOut.write(element + "\n")
97+
# #result.append(buffer_array)
4598

4699
if param_input_dir != None:
47100
if Encoder.encodeProject(param_input_dir,verbose_enabled):
48-
srcfile = open(filein,"r",encoding="utf-8")
49-
lines = srcfile.readlines()
50-
srcfile.close()
51-
# print("所有行")
52-
# #print(lines)
101+
compileProject(param_input_dir,verbose_enabled)
102+
# srcfile = open(filein,"r",encoding="utf-8")
103+
# lines = srcfile.readlines()
104+
# srcfile.close()
105+
# # print("所有行")
106+
# # #print(lines)
107+
108+
# out = open(fileout,"w",encoding="utf-8")
53109

54-
out = open(fileout,"w",encoding="utf-8")
55-
# result = [] 以后可能要用到的变量 先暂时备份
56-
# 先预处理一变所有文件的所有行的开头和结尾的空格 因为这个空格是非必要的,而且三种格式可能都存在首尾空格,所以去除空格并不会影响游戏代码
57-
预处理结果 = [] # string []
58-
for line in lines:
59-
预处理结果.append(预处理.处理("单行宏",line,verbose_enabled))
60-
ColorPrint.print_compile("预处理结果",str(预处理结果))
110+
# 预处理结果 = [] # string []
111+
# for line in lines:
112+
# 预处理结果.append(预处理.处理("单行宏",line,verbose_enabled))
113+
# ColorPrint.print_compile("预处理结果",str(预处理结果))
61114

62-
# 逐行扫描
63-
for index,line in enumerate(lines):
64-
currentScanLine = index + 1
65-
# # 每一行的结果 如果遇到注释行,则会丢弃那一行
66-
# buffer_array = []
67-
if line[0] == "@":
68-
lineToken:list = sp.parse(line,currentScanLine,verbose_enabled)
69-
#print("缓冲区",buffer_line)
70-
for element in lineToken:
71-
# print(currentScanLine, buffer_array)
72-
73-
out.write(element + "\n")
74-
# result.append(buffer_line)
75-
if line[0] == "[":
76-
lineToken:list = mp.parse(code=line, scanLine=currentScanLine, verbose=verbose_enabled)
77-
for element in lineToken:
78-
# #print(currentScanLine, buffer_array)
79-
out.write(element + "\n")
80-
# #result.append(buffer_array)
81-
out.close()
115+
# # 逐行扫描
116+
# for index,line in enumerate(lines):
117+
# currentScanLine = index + 1
118+
# # # 每一行的结果 如果遇到注释行,则会丢弃那一行
119+
# # buffer_array = []
120+
# if line[0] == "@":
121+
# lineToken:list = sp.parse(line,currentScanLine,verbose_enabled)
122+
# #print("缓冲区",buffer_line)
123+
# for element in lineToken:
124+
# # print(currentScanLine, buffer_array)
125+
126+
# out.write(element + "\n")
127+
# # result.append(buffer_line)
128+
# if line[0] == "[":
129+
# lineToken:list = mp.parse(code=line, scanLine=currentScanLine, verbose=verbose_enabled)
130+
# for element in lineToken:
131+
# # #print(currentScanLine, buffer_array)
132+
# out.write(element + "\n")
133+
# # #result.append(buffer_array)
134+
# out.close()

test.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 预处理
2+
import os
23
# 测试单行宏预处理
34
def test_single():
45
filein = "bkscr/单行.bkscr"
@@ -9,7 +10,16 @@ def test_single():
910
for line in lines:
1011
预处理结果.append(预处理.处理("单行宏",line,True))
1112
return 预处理结果
13+
# a = test_single()
14+
# print("结果")
15+
# print(a)
1216

13-
a = test_single()
14-
print("结果")
15-
print(a)
17+
def test():
18+
L = []
19+
for root, dirs, files in os.walk("bkscr"):
20+
#获得所有bkscr文件
21+
for file in files:
22+
if os.path.splitext(file)[1] == ".bkscr":
23+
L.append(os.path.join(root, file))
24+
print(L)
25+
test()

预处理.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,24 @@ def 检查到_当前代码行是空的(code) -> bool:
8787
else:
8888
return False
8989

90+
def 清理数组(code_line: str,fianl_result: list) -> list:
91+
for element in code_line:
92+
if element == None or element == "\n":
93+
pass
94+
else:
95+
fianl_result.append(element)
96+
return fianl_result
97+
9098
# 返回当前代码行
99+
# todo 移除数组中的None和\n
91100
def 处理(code_type: str, 当前代码行: str, 详细输出: bool) -> str:
92101
删除注释后的代码行 = ""
93102
删除首尾空格后的代码行 = ""
94103
删除数组空格后的代码行 = ""
104+
最终结果 = []
95105
# if code_type == "多行宏":
96106
# pass
107+
# todo 移除这个参数?
97108
if code_type == "单行宏":
98109

99110
if 检查到_当前代码行是空的(当前代码行):
@@ -121,24 +132,17 @@ def 处理(code_type: str, 当前代码行: str, 详细输出: bool) -> str:
121132
ColorPrint.print_verbose("删除数组之间空格后的字符串",删除数组空格后的代码行)
122133
if 详细输出:
123134
ColorPrint.print_compile("预处理完毕的字符串",删除数组空格后的代码行)
124-
return 删除数组空格后的代码行
125-
return 删除数组空格后的代码行
135+
最终结果 = 清理数组(删除数组空格后的代码行)
136+
return 最终结果
126137

127138
else:
128139
ColorPrint.print_verbose_hint("没有检测到数组之间的空格")
129140
ColorPrint.print_verbose("预处理完毕的结果",删除首尾空格后的代码行)
130141
if 详细输出:
131142
ColorPrint.print_compile("预处理完毕的字符串",删除首尾空格后的代码行)
132-
return 删除首尾空格后的代码行
133-
return 删除首尾空格后的代码行
134-
143+
最终结果 = 清理数组(删除数组空格后的代码行)
144+
return 最终结果
145+
# return 删除首尾空格后的代码行
146+
135147
# elif code_type == "Parser脚本":
136-
# pass
137-
138-
139-
140-
# def 删除_首尾空格(当前代码行: str, 详细输出: bool):
141-
# if 检查到_有空格(当前代码行, 详细输出):
142-
# 处理结果:str = 删除_首尾的空格(当前代码行)
143-
# ColorPrint.print_verbose("删除头尾空格后的字符串",当前代码行)
144-
# return 处理结果
148+
# pass

0 commit comments

Comments
 (0)