diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2024-04-29 22:38:21 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2024-04-29 22:38:21 +0300 |
| commit | ac0efecc5558687b1215264896b436b33353c7ec (patch) | |
| tree | 92462e6e15a8c53858f240b02cf93de511ed555e | |
| parent | 49c6b2739f166c62764574335650535c84bb07a1 (diff) | |
Make @include idempotent
| -rw-r--r-- | atk16_asm/asm_pass0.py | 37 | ||||
| -rw-r--r-- | atk16_ast_walking_compiler/sample_py/sample_py_src.py (renamed from atk16_ast_walking_compiler/sample_py/test_py_src.py) | 0 | ||||
| -rw-r--r-- | test/e2e/include_idempotency/p1.atk16 | 4 | ||||
| -rw-r--r-- | test/e2e/include_idempotency/p2.atk16 | 6 | ||||
| -rw-r--r-- | test/e2e/include_idempotency/test_include_idempotent.py | 14 |
5 files changed, 51 insertions, 10 deletions
diff --git a/atk16_asm/asm_pass0.py b/atk16_asm/asm_pass0.py index bda16a4..35bfff2 100644 --- a/atk16_asm/asm_pass0.py +++ b/atk16_asm/asm_pass0.py @@ -16,10 +16,15 @@ class Result0: lines: list[Result0Line] InsertAtDataSegment = Callable[[str, list[str], str, int], None] +IncludeModule = Callable[[str], None] + +def pass_0(lines: list[str], file_name: str, + insert_at_data_segment: InsertAtDataSegment | None = None, + include_module: IncludeModule | None = None) -> Result0: -def pass_0(lines: list[str], file_name: str, insert_at_data_segment: InsertAtDataSegment | None = None) -> Result0: file_name = file_name if file_name.endswith(".atk16") else file_name + ".atk16" result_lines: list[Result0Line] = [] + included_module_abs_paths: list[str] = [] if not insert_at_data_segment: def _insert_at_data_segment(label: str, values: list[str], context_file_name: str, context_line_num: int): @@ -71,6 +76,26 @@ def pass_0(lines: list[str], file_name: str, insert_at_data_segment: InsertAtDat insert_at_data_segment = _insert_at_data_segment + if not include_module: + def _include_module(module_abs_path: str): + if module_abs_path in included_module_abs_paths: + return + + with open(module_abs_path, "r") as f: + incl_lines = f.readlines() + + incl_result0 = pass_0(incl_lines, asm_file_name, insert_at_data_segment) + for incl_line in incl_result0.lines: + result_lines.append(Result0Line( + src_file=incl_line.src_file, + line_num=incl_line.line_num, + line=incl_line.line + )) + + included_module_abs_paths.append(module_abs_path) + + include_module = _include_module + for (line_num, line) in enumerate(lines): line_num += 1 # line numbers are 1-based @@ -106,16 +131,8 @@ def pass_0(lines: list[str], file_name: str, insert_at_data_segment: InsertAtDat path = asm_file_name else: path = os.path.join(os.path.dirname(file_name), asm_file_name) - with open(path, "r") as f: - incl_lines = f.readlines() - incl_result0 = pass_0(incl_lines, asm_file_name, insert_at_data_segment) - for incl_line in incl_result0.lines: - result_lines.append(Result0Line( - src_file=incl_line.src_file, - line_num=incl_line.line_num, - line=incl_line.line - )) + include_module(path) case _: result_lines.append(Result0Line( diff --git a/atk16_ast_walking_compiler/sample_py/test_py_src.py b/atk16_ast_walking_compiler/sample_py/sample_py_src.py index 619d22e..619d22e 100644 --- a/atk16_ast_walking_compiler/sample_py/test_py_src.py +++ b/atk16_ast_walking_compiler/sample_py/sample_py_src.py diff --git a/test/e2e/include_idempotency/p1.atk16 b/test/e2e/include_idempotency/p1.atk16 new file mode 100644 index 0000000..f9f6363 --- /dev/null +++ b/test/e2e/include_idempotency/p1.atk16 @@ -0,0 +1,4 @@ +@include %bootstrap + +@label main + hlt diff --git a/test/e2e/include_idempotency/p2.atk16 b/test/e2e/include_idempotency/p2.atk16 new file mode 100644 index 0000000..c6d46b9 --- /dev/null +++ b/test/e2e/include_idempotency/p2.atk16 @@ -0,0 +1,6 @@ +@include %bootstrap +@include %bootstrap +@include %bootstrap + +@label main + hlt diff --git a/test/e2e/include_idempotency/test_include_idempotent.py b/test/e2e/include_idempotency/test_include_idempotent.py new file mode 100644 index 0000000..e7b4fc8 --- /dev/null +++ b/test/e2e/include_idempotency/test_include_idempotent.py @@ -0,0 +1,14 @@ +import os.path +from test.utils import assemble_and_run_until_halted + +def test_include_idempotent(): + machine1 = assemble_and_run_until_halted( + os.path.join(os.path.dirname(__file__), "p1.atk16") + ) + + machine2 = assemble_and_run_until_halted( + os.path.join(os.path.dirname(__file__), "p2.atk16") + ) + + for i in range(len(machine1.rom.memory)): + assert machine1.rom.memory[i] == machine2.rom.memory[i] |
