From 4cda2a22701659c5a03f74873ae1ad9db7aa5362 Mon Sep 17 00:00:00 2001 From: Lorenzo Venerandi Date: Sun, 25 Aug 2024 18:34:45 +0200 Subject: [PATCH] project refactor --- task-splitter/.gitignore => .gitignore | 0 main.py | 12 +++ task-splitter/readme.md => readme.md | 0 {task-splitter/src => source-code}/tasks.c | 0 src/__init__.py | 0 src/__pycache__/__init__.cpython-310.pyc | Bin 0 -> 156 bytes src/task_splitter/__init__.py | 0 .../__pycache__/__init__.cpython-310.pyc | Bin 0 -> 170 bytes .../code_processor.cpython-310.pyc | Bin 0 -> 2419 bytes .../task_splitter/code_processor.py | 72 ++++++++++-------- .../wasm_runtime_builder}/Dockerfile | 0 .../wasm_runtime_builder}/readme.md | 0 wasm-container-runtime/src/main.c | 10 --- 13 files changed, 51 insertions(+), 43 deletions(-) rename task-splitter/.gitignore => .gitignore (100%) create mode 100644 main.py rename task-splitter/readme.md => readme.md (100%) rename {task-splitter/src => source-code}/tasks.c (100%) create mode 100644 src/__init__.py create mode 100644 src/__pycache__/__init__.cpython-310.pyc create mode 100644 src/task_splitter/__init__.py create mode 100644 src/task_splitter/__pycache__/__init__.cpython-310.pyc create mode 100644 src/task_splitter/__pycache__/code_processor.cpython-310.pyc rename task-splitter/main.py => src/task_splitter/code_processor.py (52%) rename {wasm-container-runtime => src/wasm_runtime_builder}/Dockerfile (100%) rename {wasm-container-runtime => src/wasm_runtime_builder}/readme.md (100%) delete mode 100644 wasm-container-runtime/src/main.c diff --git a/task-splitter/.gitignore b/.gitignore similarity index 100% rename from task-splitter/.gitignore rename to .gitignore diff --git a/main.py b/main.py new file mode 100644 index 0000000..c01e4d1 --- /dev/null +++ b/main.py @@ -0,0 +1,12 @@ +import src.task_splitter.code_processor as splitter + +input_file = "source-code/tasks.c" # Replace this with your actual file name +output_dir = "build/" + +functions = splitter.extract_functions_from_file(input_file) +print(f"Found {len(functions)} functions in the file.") + +splitter.create_independent_files(functions, output_dir) +print("Functions have been split into separate files with their own main functions.") + +splitter.save_functions_to_yaml(functions, output_dir + "functions.yaml") \ No newline at end of file diff --git a/task-splitter/readme.md b/readme.md similarity index 100% rename from task-splitter/readme.md rename to readme.md diff --git a/task-splitter/src/tasks.c b/source-code/tasks.c similarity index 100% rename from task-splitter/src/tasks.c rename to source-code/tasks.c diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/__pycache__/__init__.cpython-310.pyc b/src/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e942f6a806152837252dcbf3e5544a389ee3a997 GIT binary patch literal 156 zcmd1j<>g`kf}2sN(?IlN5P=LBfgA@QE@lA|DGb33nv8xc8Hzx{2;!H0vQg`kf~!%d(?IlN5P=LBfgA@QE@lA|DGb33nv8xc8Hzx{2;!G-vQvfJJclNRC zBjjRBNa3}r`dpAMkNp~b?Kf~A`;^x{20=QzxeFI)*V^5g@y_hd?EGeBgij~KB7pH2X_SHn$MJbVT_$%dFLrHVc z^@J=cYtK?6(fcGZ`r0n6MM|O#wDMF<%{Z6nJBX4E!oEs$s!b}X3A%+O54vBZ#iS_o z5_3ai(SVkgVEt{Rgr9;;r(_rPja?-zNiGK`Dko*CbP;PL<$j*()Y!!^=cx(jE++*z zPYG5OUSe^gCs*Ng@2reTPFSf(syMJdF1gZxn@DnCrIL&#<4GZzpyijEv~@PgK~%@; zy(_hc&gQDK>2POy?Y!mi^EGEv%+pS-w))Uoo3lboduni=Lgn_thf$#+<|%f z)p^7=X5(>3aXa6Mc=OOzO{yGI7#blR7CdER5FJ|ud$taP3sF5JAhL%WyCbo* zC*KL$7~V*cq_7zwCeQ+~)z~PAh7)B4;u3yKM+McWyM}@47!ZD*SxNh(amiw`L^rUOxcE^EafKqLwv*GX}LDVkJ%Q>dU+NfDp`;ULQar3>nn4s~Ss-*|;ZElE&9 zPAN3MgMYb{YEUmwq7Ett0t5qMY2iqR&|)9c5-q<_4z2f9I<|`zl^wK;V5iC}EzL_W=46Ocg-yc%7BOlYLZWC778Ar^HMTTUAy8u7_v* z3~gq}?0si8!?@b^nqkeVZkttTmo8NoIPbQbVWS#9@?sc*>hN-^k33!tqv~M}RiApT zZXmUK_(Jtq85Us{KwgH<#jXtq?iztoto(|fU^pns(5y@?KtG#87b`UFe;&uF69WPCa|o zOeHVe70n_UiNI2(Z8E448F?HvfsuJ@>1gA%hwN#YEIA zv~HI~P>+)Oea}w+p9_n^&yQg+@H8&t3NirzSk0;A&6H}qDe6=I;OFEuy9i5;#aq-% z1G-G*c_1Ja;pu}kET-`6^$|QV_=5yGIhsH*B>N5$Pb1t8u4jl@D*FsfWds6FC43-M zkTN0=!r6?7k)4J$GD`2edFBIfyX!1!OWjpa;-Z!p0m4%U27sM?4(u_2iVQmP>KZTt zVA88`c+k;foCx3m1b+rdjCv=a005aB^-scs)Y&<(^R0#KJQShW-3Ye$k)K;$yOj~z zZFfK186@xf_!0M>21ggo-8gcE6(5%J91KM~fj&84Ujk|A8ICv`7gnx_ rK@=o;mC|#$X2iTGyG#PXm}^J0+Y08z6Uqg=4aGq4VdER)RMGesF7;(T literal 0 HcmV?d00001 diff --git a/task-splitter/main.py b/src/task_splitter/code_processor.py similarity index 52% rename from task-splitter/main.py rename to src/task_splitter/code_processor.py index 5812f19..6975b9f 100644 --- a/task-splitter/main.py +++ b/src/task_splitter/code_processor.py @@ -2,15 +2,19 @@ import os import re import yaml -def extract_functions(file_content): +def __extract_functions(file_content): + # Regular expression to match C function definitions - function_pattern = re.compile(r"(^\w[\w\s\*]+)\s+(\w+)\s*\(([^)]*)\)\s*(\{[^{}]*\})", re.MULTILINE) + REGULAR_EXPRESSION_C = r"(^\w[\w\s\*]+)\s+(\w+)\s*\(([^)]*)\)\s*(\{[^{}]*\})" + REGULAR_EXPRESSION_ANNOTATION = r"(^\@[A-Z]\w+){1}(\(.+\))*" + + + function_pattern = re.compile(REGULAR_EXPRESSION_C, re.MULTILINE) function_matches = function_pattern.finditer(file_content) - annotation_pattern = re.compile(r"(\@[A-Z]\w+){1}(\(.+\))*") + annotation_pattern = re.compile(REGULAR_EXPRESSION_ANNOTATION, re.MULTILINE) functions = [] - function_contents = [] positions = [] for match in function_matches: @@ -42,55 +46,57 @@ def extract_functions(file_content): } ) - + func_return_type = match.group(1).strip() func_name = match.group(2) func_args = match.group(3).strip() + func_content = match.group(4).strip() - function_contents.append( - f"{func_return_type} {func_name}({func_args}) {match.group(4)}" - ) + # function_contents.append( + # f"{func_return_type} {func_name}({func_args}) {match.group(4)}" + # ) functions.append({ "name": func_name, "return_type": func_return_type, "args": func_args, - "annotations": annotations + "annotations": annotations, + "content": func_content }) + return functions - return functions, function_contents - -def create_independent_files(functions, function_contents, output_dir): - for i, function in enumerate(functions): - filename = f"{function['name']}.c" +def create_independent_files(functions, output_dir): + + for fun in functions: + + function_content = f"{fun['return_type']} {fun['name']}({fun['args']}) {fun['content']}" + + filename = f"{fun['name']}.c" with open(output_dir + filename, "w") as f: + # Write the original function - f.write(function_contents[i]) + f.write(function_content) f.write("\n") # Add a main function to call the function main_function = f""" - int main() {{ - // Assuming that the function has no return value, or you can modify it to handle return values. - {function['name']}({', '.join(['0' for _ in function['args'].split(',')])}); - return 0; - }} +int main() {{ + // Assuming that the function has no return value + {fun['name']}({', '.join(['0' for _ in fun['args'].split(',')])}); + return 0; +}} """ f.write(main_function) - -def split_functions_into_files(input_file, output_dir): + +def extract_functions_from_file(input_file): + with open(input_file, "r") as file: file_content = file.read() + + functions = __extract_functions(file_content) + return functions - functions, function_contents = extract_functions(file_content) +def save_functions_to_yaml(functions, output_file): - with open(output_dir + "config.yaml","w") as outfile: - yaml.safe_dump(functions, outfile, default_style=None, default_flow_style=False, sort_keys=False) - - create_independent_files(functions, function_contents, output_dir) - -if __name__ == "__main__": - input_file = "src/tasks.c" # Replace this with your actual file name - output_dir = "build/" - split_functions_into_files(input_file, output_dir) - print("Functions have been split into separate files with their own main functions.") + with open(output_file, "w") as file: + yaml.dump(functions, file, sort_keys=False) diff --git a/wasm-container-runtime/Dockerfile b/src/wasm_runtime_builder/Dockerfile similarity index 100% rename from wasm-container-runtime/Dockerfile rename to src/wasm_runtime_builder/Dockerfile diff --git a/wasm-container-runtime/readme.md b/src/wasm_runtime_builder/readme.md similarity index 100% rename from wasm-container-runtime/readme.md rename to src/wasm_runtime_builder/readme.md diff --git a/wasm-container-runtime/src/main.c b/wasm-container-runtime/src/main.c deleted file mode 100644 index 936b6f1..0000000 --- a/wasm-container-runtime/src/main.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -int main() { - while (1) - { - printf("hello, world!\n"); - } - - return 0; -} \ No newline at end of file