Skip to content

Commit

Permalink
同步一下
Browse files Browse the repository at this point in the history
  • Loading branch information
2439905184 committed Oct 4, 2022
1 parent 3e88ac4 commit 4fbc955
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 52 deletions.
123 changes: 88 additions & 35 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,100 @@ def getParams() -> list:
if param_verbose == "verbose":
verbose_enabled = True

filein = "bkscr/多行.bkscr"
fileout = "compile_out/out.txt" # 备份 fileout = filein.replace(".bkscr",".txt")
# filein = "bkscr/多行.bkscr"
# filein = os.path.split(param_input_dir)[1] # 多好.bkscr
# fileout = "",filein.replace(".bkscr",".txt")
# fileout = "compile_out/out.txt" # 备份 fileout = filein.replace(".bkscr",".txt")

ColorPrint.print_verbose_once()
ColorPrint.print_compile_once()
ColorPrint.print_verbose_hint("输入文件夹 -> " + param_input_dir)
ColorPrint.print_verbose_hint("输出文件 -> " + fileout)
# ColorPrint.print_verbose_hint("输出文件 -> " + fileout)

def getAllBkscr(p_dir) -> list:
L = []
for root, dirs, files in os.walk(p_dir):
#获得所有bkscr文件
for file in files:
if os.path.splitext(file)[1] == ".bkscr":
if file != "macro.bkscr":
L.append(os.path.join(root, file))
return L

# 先预处理一变所有文件的所有行的开头和结尾的空格 因为这个空格是非必要的,而且三种格式可能都存在首尾空格,所以去除空格并不会影响游戏代码
def preCompile(eachFile: str) -> list:
result = [] # string []
# 针对每个文件,读取每个文件的所有行
each_lines:str = eachFile.readlines()
for lines in each_lines:
result.append(预处理.处理("单行宏",lines,verbose_enabled))
ColorPrint.print_compile("预处理结果",str(result))

return result
# 过程 getAllBkscr -> preCompile -> compile
def compileProject(p_dir,p_verbose):
all_FilePaths:list = getAllBkscr(p_dir)

for each_FilePath in all_FilePaths:
with open(each_FilePath,"r",encoding="utf-8") as eachFile:
# filein = os.path.split(param_input_dir)[1] # 多好.bkscr
# fileout filein.replace(".bkscr",".txt")
p = os.path.split(each_FilePath)
output_name = "compile_out/" + p[1].replace(".bkscr",".txt")
FileOut = open(output_name,"w",encoding="utf-8")
# 这里的预处理结果是当前的一行
每一行的预处理结果:list = preCompile(eachFile)
# ColorPrint.print_compile("预处理结果",str(预处理结果))
for index,line in enumerate(每一行的预处理结果):
currentScanLine = index + 1
if line[0] == "@":
lineToken:list = sp.parse(line,currentScanLine,verbose_enabled)
#print("缓冲区",buffer_line)
for element in lineToken:
# print(currentScanLine, buffer_array)

FileOut.write(element + "\n")
# result.append(buffer_line)
if line[0] == "[":
lineToken:list = mp.parse(line,currentScanLine,verbose_enabled)
for element in lineToken:
# #print(currentScanLine, buffer_array)
FileOut.write(element + "\n")
# #result.append(buffer_array)

if param_input_dir != None:
if Encoder.encodeProject(param_input_dir,verbose_enabled):
srcfile = open(filein,"r",encoding="utf-8")
lines = srcfile.readlines()
srcfile.close()
# print("所有行")
# #print(lines)
compileProject(param_input_dir,verbose_enabled)
# srcfile = open(filein,"r",encoding="utf-8")
# lines = srcfile.readlines()
# srcfile.close()
# # print("所有行")
# # #print(lines)

# out = open(fileout,"w",encoding="utf-8")

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

# 逐行扫描
for index,line in enumerate(lines):
currentScanLine = index + 1
# # 每一行的结果 如果遇到注释行,则会丢弃那一行
# buffer_array = []
if line[0] == "@":
lineToken:list = sp.parse(line,currentScanLine,verbose_enabled)
#print("缓冲区",buffer_line)
for element in lineToken:
# print(currentScanLine, buffer_array)
out.write(element + "\n")
# result.append(buffer_line)
if line[0] == "[":
lineToken:list = mp.parse(code=line, scanLine=currentScanLine, verbose=verbose_enabled)
for element in lineToken:
# #print(currentScanLine, buffer_array)
out.write(element + "\n")
# #result.append(buffer_array)
out.close()
# # 逐行扫描
# for index,line in enumerate(lines):
# currentScanLine = index + 1
# # # 每一行的结果 如果遇到注释行,则会丢弃那一行
# # buffer_array = []
# if line[0] == "@":
# lineToken:list = sp.parse(line,currentScanLine,verbose_enabled)
# #print("缓冲区",buffer_line)
# for element in lineToken:
# # print(currentScanLine, buffer_array)

# out.write(element + "\n")
# # result.append(buffer_line)
# if line[0] == "[":
# lineToken:list = mp.parse(code=line, scanLine=currentScanLine, verbose=verbose_enabled)
# for element in lineToken:
# # #print(currentScanLine, buffer_array)
# out.write(element + "\n")
# # #result.append(buffer_array)
# out.close()
16 changes: 13 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 预处理
import os
# 测试单行宏预处理
def test_single():
filein = "bkscr/单行.bkscr"
Expand All @@ -9,7 +10,16 @@ def test_single():
for line in lines:
预处理结果.append(预处理.处理("单行宏",line,True))
return 预处理结果
# a = test_single()
# print("结果")
# print(a)

a = test_single()
print("结果")
print(a)
def test():
L = []
for root, dirs, files in os.walk("bkscr"):
#获得所有bkscr文件
for file in files:
if os.path.splitext(file)[1] == ".bkscr":
L.append(os.path.join(root, file))
print(L)
test()
32 changes: 18 additions & 14 deletions 预处理.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,24 @@ def 检查到_当前代码行是空的(code) -> bool:
else:
return False

def 清理数组(code_line: str,fianl_result: list) -> list:
for element in code_line:
if element == None or element == "\n":
pass
else:
fianl_result.append(element)
return fianl_result

# 返回当前代码行
# todo 移除数组中的None和\n
def 处理(code_type: str, 当前代码行: str, 详细输出: bool) -> str:
删除注释后的代码行 = ""
删除首尾空格后的代码行 = ""
删除数组空格后的代码行 = ""
最终结果 = []
# if code_type == "多行宏":
# pass
# todo 移除这个参数?
if code_type == "单行宏":

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

else:
ColorPrint.print_verbose_hint("没有检测到数组之间的空格")
ColorPrint.print_verbose("预处理完毕的结果",删除首尾空格后的代码行)
if 详细输出:
ColorPrint.print_compile("预处理完毕的字符串",删除首尾空格后的代码行)
return 删除首尾空格后的代码行
return 删除首尾空格后的代码行

最终结果 = 清理数组(删除数组空格后的代码行)
return 最终结果
# return 删除首尾空格后的代码行

# elif code_type == "Parser脚本":
# pass



# def 删除_首尾空格(当前代码行: str, 详细输出: bool):
# if 检查到_有空格(当前代码行, 详细输出):
# 处理结果:str = 删除_首尾的空格(当前代码行)
# ColorPrint.print_verbose("删除头尾空格后的字符串",当前代码行)
# return 处理结果
# pass

0 comments on commit 4fbc955

Please sign in to comment.