Skip to content

Commit

Permalink
Address some suggestions from Ruff and Pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
neuroevolutus committed Oct 22, 2024
1 parent ae81f6e commit 14d26ed
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 47 deletions.
6 changes: 4 additions & 2 deletions gen_from_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion generate_expected_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 2 additions & 8 deletions test_framework/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions test_framework/parser/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
14 changes: 6 additions & 8 deletions test_framework/regalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
14 changes: 14 additions & 0 deletions test_framework/tacky/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
14 changes: 1 addition & 13 deletions test_framework/tacky/const_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
13 changes: 1 addition & 12 deletions test_framework/tacky/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down

0 comments on commit 14d26ed

Please sign in to comment.