From 14d26edc7403892d4f18636c1a65a245cf15e579 Mon Sep 17 00:00:00 2001 From: neuroevolutus <19356702+neuroevolutus@users.noreply.github.com> Date: Sun, 20 Oct 2024 22:31:08 -0500 Subject: [PATCH] Address some suggestions from Ruff and Pylint --- gen_from_templates.py | 6 ++++-- generate_expected_results.py | 1 - test_framework/basic.py | 10 ++-------- test_framework/parser/parse.py | 5 ++--- test_framework/regalloc.py | 14 ++++++-------- test_framework/tacky/common.py | 14 ++++++++++++++ test_framework/tacky/const_fold.py | 14 +------------- test_framework/tacky/pipeline.py | 13 +------------ 8 files changed, 30 insertions(+), 47 deletions(-) diff --git a/gen_from_templates.py b/gen_from_templates.py index bd9a4883..704aff6e 100755 --- a/gen_from_templates.py +++ b/gen_from_templates.py @@ -7,6 +7,8 @@ from string import ascii_lowercase from typing import Any, Iterable +import sys + from jinja2 import Environment, FileSystemLoader, pass_environment from jinja2.filters import do_wordwrap @@ -124,7 +126,7 @@ def format_string(text: str, fmt: str) -> str: def gen_assembly(template_file: Path, output_dir: Path) -> None: if not template_file.name.endswith(".s.jinja"): - exit(f"Expected assembly template, found {template_file}") + sys.exit(f"Expected assembly template, found {template_file}") templ = env.get_template(str(template_file)) basename = template_file.name.removesuffix(".s.jinja") for platform in ["linux", "osx"]: @@ -297,7 +299,7 @@ def gen_assembly(template_file: Path, output_dir: Path) -> None: template_files = Path("templates/chapter_20_templates").iterdir() for t in template_files: if t.suffix != ".jinja": - exit(f"Found non-template {f} in templates directory") + sys.exit(f"Found non-template {f} in templates directory") relative_path = t.relative_to("templates") templ = env.get_template(str(relative_path)) if t.name in configurable_templates: diff --git a/generate_expected_results.py b/generate_expected_results.py index e476ca8a..553f7797 100755 --- a/generate_expected_results.py +++ b/generate_expected_results.py @@ -34,7 +34,6 @@ def cleanup_keys() -> None: full_path = TEST_DIR / k if not full_path.exists(): del results[k] - return def main() -> None: diff --git a/test_framework/basic.py b/test_framework/basic.py index 8f00a5a8..e41c3a7f 100644 --- a/test_framework/basic.py +++ b/test_framework/basic.py @@ -50,17 +50,11 @@ def get_platform() -> str: - if IS_OSX: - return "os_x" - else: - return "linux" + return "os_x" if IS_OSX else "linux" def get_platform_suffix() -> str: - if IS_OSX: - return MAC_SUFFIX - else: - return LINUX_SUFFIX + return MAC_SUFFIX if IS_OSX else LINUX_SUFFIX def get_props_key(source_file: Path) -> str: diff --git a/test_framework/parser/parse.py b/test_framework/parser/parse.py index 17238425..0647a40d 100644 --- a/test_framework/parser/parse.py +++ b/test_framework/parser/parse.py @@ -252,7 +252,7 @@ def parse_immediate(toks: list[Token]) -> Immediate: if tok_type == TokType.INT: val = int(next_tok.tok_str, base=0) return Immediate(val) - elif tok_type in [TokType.PLUS_SIGN, TokType.MINUS_SIGN]: + if tok_type in [TokType.PLUS_SIGN, TokType.MINUS_SIGN]: # next tok should val num_tok = toks.pop(0) if num_tok.tok_type != TokType.INT: @@ -261,8 +261,7 @@ def parse_immediate(toks: list[Token]) -> Immediate: if tok_type == TokType.MINUS_SIGN: return Immediate(-val) return Immediate(val) - else: - raise ParseError(f"Bad immediate value: ${next_tok}") + raise ParseError(f"Bad immediate value: ${next_tok}") def parse_expr(toks: list[Token]) -> Expr: diff --git a/test_framework/regalloc.py b/test_framework/regalloc.py index f6b6f99e..3cf38340 100644 --- a/test_framework/regalloc.py +++ b/test_framework/regalloc.py @@ -41,14 +41,12 @@ def is_stack(operand: asm.Operand) -> bool: def get_spilled_operand_count(spill_instructions: List[asm.AsmItem]) -> int: """Count number of distinct stack operands in spill instructions""" - spilled_operands = set( - [ - str(op) # convert to string b/c Operands themselves are not hashable - for i in spill_instructions - for op in i.operands # type: ignore - if isinstance(op, asm.Memory) and op.base == Register.BP - ] - ) + spilled_operands = { + str(op) # convert to string b/c Operands themselves are not hashable + for i in spill_instructions + for op in i.operands # type: ignore + if isinstance(op, asm.Memory) and op.base == Register.BP + } return len(spilled_operands) diff --git a/test_framework/tacky/common.py b/test_framework/tacky/common.py index bdb15b02..c13dba4b 100644 --- a/test_framework/tacky/common.py +++ b/test_framework/tacky/common.py @@ -220,3 +220,17 @@ def is_zero_instr(i: asm.AsmItem) -> bool: and i.opcode == Opcode.XOR and i.operands[0] == i.operands[1] ) + +def check_constant_folds(parsed_asm, program_source_file): + for fn_name, fn_body in parsed_asm.items(): + if fn_name.startswith("target"): + bad_instructions = [i for i in fn_body.instructions if not ok(i)] + self.assertFalse( + bad_instructions, + msg=common.build_msg( + "Found instructions that should have been constant folded", + bad_instructions=bad_instructions, + full_prog=fn_body, + program_path=program_source_file, + ), + ) diff --git a/test_framework/tacky/const_fold.py b/test_framework/tacky/const_fold.py index 351ba8d8..da73f13d 100644 --- a/test_framework/tacky/const_fold.py +++ b/test_framework/tacky/const_fold.py @@ -32,19 +32,7 @@ def ok(i: AsmItem) -> bool: or common.is_zero_instr(i) ) - for fn_name, fn_body in parsed_asm.items(): - if fn_name.startswith("target"): - bad_instructions = [i for i in fn_body.instructions if not ok(i)] - self.assertFalse( - bad_instructions, - msg=common.build_msg( - "Found instructions that should have been constant folded", - bad_instructions=bad_instructions, - full_prog=fn_body, - program_path=program, - ), - ) - + common.check_constant_folds(parsed_asm, program) def make_constant_fold_test(program: Path) -> Callable[[TestConstantFolding], None]: """Generate test method for a single test program.""" diff --git a/test_framework/tacky/pipeline.py b/test_framework/tacky/pipeline.py index 3f7fcd26..034e9de9 100644 --- a/test_framework/tacky/pipeline.py +++ b/test_framework/tacky/pipeline.py @@ -60,18 +60,7 @@ def ok(i: AsmItem) -> bool: # source isn't immediate or RIP-relative return False - for fn_name, fn_body in parsed_asm.items(): - if fn_name.startswith("target"): - bad_instructions = [i for i in fn_body.instructions if not ok(i)] - self.assertFalse( - bad_instructions, - msg=common.build_msg( - "Found instructions that should have been constant folded", - bad_instructions=bad_instructions, - full_prog=fn_body, - program_path=source_file, - ), - ) + common.check_constant_folds(parsed_asm, source_file) def global_var_unused_test(self, *, source_file: Path, unused_var: str) -> None: """