From 4fbc955266cf0d3d997f201dee458dcd81d0099d Mon Sep 17 00:00:00 2001 From: 2439905184 Date: Tue, 4 Oct 2022 19:23:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=B8=80=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 123 ++++++++++++++++------ test.py | 16 ++- "\351\242\204\345\244\204\347\220\206.py" | 32 +++--- 3 files changed, 119 insertions(+), 52 deletions(-) diff --git a/main.py b/main.py index 0479755..ea79a2a 100644 --- a/main.py +++ b/main.py @@ -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() \ No newline at end of file + # # 逐行扫描 + # 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() \ No newline at end of file diff --git a/test.py b/test.py index 7d0b029..c59dc78 100644 --- a/test.py +++ b/test.py @@ -1,4 +1,5 @@ import 预处理 +import os # 测试单行宏预处理 def test_single(): filein = "bkscr/单行.bkscr" @@ -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) \ No newline at end of file +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() \ No newline at end of file diff --git "a/\351\242\204\345\244\204\347\220\206.py" "b/\351\242\204\345\244\204\347\220\206.py" index 24efd8e..fcd8001 100644 --- "a/\351\242\204\345\244\204\347\220\206.py" +++ "b/\351\242\204\345\244\204\347\220\206.py" @@ -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 检查到_当前代码行是空的(当前代码行): @@ -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 \ No newline at end of file