Skip to content

Commit

Permalink
Merge pull request #267 from tomhea/feature/black-formatting
Browse files Browse the repository at this point in the history
Feature/black formatting
  • Loading branch information
tomhea authored Dec 12, 2023
2 parents 6519d80 + 760febd commit 9480b0b
Show file tree
Hide file tree
Showing 37 changed files with 1,053 additions and 608 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![Tests](https://github.com/tomhea/flip-jump/actions/workflows/tests.yml/badge.svg)](https://github.com/tomhea/flip-jump/actions/workflows/tests.yml)
[![PyPI - Version](https://img.shields.io/pypi/v/flipjump)](https://pypi.org/project/flipjump/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
[![Website](https://img.shields.io/website?down_color=red&down_message=down&up_message=up&url=https%3A%2F%2Fesolangs.org%2Fwiki%2FFlipJump)](https://esolangs.org/wiki/FlipJump)
[![GitHub Discussions](https://img.shields.io/github/discussions/tomhea/flip-jump)](https://github.com/tomhea/flip-jump/discussions)
[![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/tomhea/flip-jump)](https://mango-dune-07a8b7110.1.azurestaticapps.net/?repo=Tomhea%2Fflip-jump)
Expand Down Expand Up @@ -208,10 +210,11 @@ _You can also use the `flipjump.assemble_run_according_to_cmd_line_args(cmd_line
- inout/ - Contains the .in and .out files for each test.
- conftest.py - The pytest configuration file. The tests are being generated here.
- test_fj.py - The base test functions for compilation and running ([how to run](tests/README.md#run-the-tests)).
- test_compile_*.csv - Arguments for the compile tests ([compile test arguments format](tests/README.md#compile-csvs-format)).
- test_run_*.csv - Arguments for the run tests ([run test arguments format](tests/README.md#run-csvs-format)).
- conf.json - The tests groups+order lists.
- xfail_*.csv - [xfail](https://docs.pytest.org/en/7.1.x/how-to/skipping.html#xfail-mark-test-functions-as-expected-to-fail) these tests.
- [tests_tables/](tests/tests_tables)
- test_compile_*.csv - Arguments for the compile tests ([compile test arguments format](tests/README.md#compile-csvs-format)).
- test_run_*.csv - Arguments for the run tests ([run test arguments format](tests/README.md#run-csvs-format)).
- xfail_*.csv - [xfail](https://docs.pytest.org/en/7.1.x/how-to/skipping.html#xfail-mark-test-functions-as-expected-to-fail) these tests.


# Read More - Extra Documentation
Expand Down
11 changes: 9 additions & 2 deletions flipjump/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from flipjump.flipjump_cli import assemble_run_according_to_cmd_line_args
from flipjump.flipjump_quickstart import (assemble, run, debug, run_test_output,
assemble_and_run, assemble_and_debug, assemble_and_run_test_output)
from flipjump.flipjump_quickstart import (
assemble,
run,
debug,
run_test_output,
assemble_and_run,
assemble_and_debug,
assemble_and_run_test_output,
)
from flipjump.fjm.fjm_consts import FJMVersion
from flipjump.interpretter.fjm_run import TerminationStatistics
from flipjump.utils.classes import TerminationCause
Expand Down
72 changes: 41 additions & 31 deletions flipjump/assembler/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ def assert_address_in_memory(memory_width: int, address: int) -> None:

def validate_addresses(memory_width: int, first_address: int, last_address: int) -> None:
if first_address % memory_width != 0 or last_address % memory_width != 0:
raise FlipJumpAssemblerException(f'segment boundaries are unaligned: '
f'[{hex(first_address)}, {hex(last_address - 1)}].')
raise FlipJumpAssemblerException(
f'segment boundaries are unaligned: ' f'[{hex(first_address)}, {hex(last_address - 1)}].'
)

assert_address_in_memory(memory_width, first_address)
assert_address_in_memory(memory_width, last_address - 1)


def add_segment_to_fjm(memory_width: int,
fjm_writer: Writer,
first_address: int, last_address: int,
fj_words: List[int], wflip_words: List[int]) -> None:
def add_segment_to_fjm(
memory_width: int,
fjm_writer: Writer,
first_address: int,
last_address: int,
fj_words: List[int],
wflip_words: List[int],
) -> None:
"""
The new segment will be placed in [first_address, last_address),
And will include the next data: fj_words + wflip_words.
Expand All @@ -48,8 +53,10 @@ def add_segment_to_fjm(memory_width: int,
try:
fjm_writer.add_segment(segment_start_address, segment_length, data_start, len(data_words))
except FlipJumpWriteFjmException as e:
exception_message = (f"failed to add the segment: "
f"{fjm_writer.get_segment_addresses_repr(segment_start_address, segment_length)}.")
exception_message = (
f"failed to add the segment: "
f"{fjm_writer.get_segment_addresses_repr(segment_start_address, segment_length)}."
)
raise FlipJumpAssemblerException(exception_message) from e

fj_words.clear()
Expand Down Expand Up @@ -78,7 +85,7 @@ def __init__(self, memory_width: int, first_segment: NewSegment, labels: Dict[st
self.fj_words: List[int] = []
self.wflip_words: List[int] = []

self.padding_ops_indices: List[int] = [] # indices in self.fj_words
self.padding_ops_indices: List[int] = [] # indices in self.fj_words

# return_address -> { (f3, f2, f1, f0) -> start_flip_address }
self.wflips_dict: Dict[int, Dict[Tuple[int, ...], int]] = defaultdict(lambda: {})
Expand All @@ -97,17 +104,17 @@ def close_and_add_segment(self, fjm_writer: Writer) -> None:
if self.next_wflip_address == self.first_address:
return

add_segment_to_fjm(self.memory_width, fjm_writer,
self.first_address, self.next_wflip_address,
self.fj_words, self.wflip_words)
add_segment_to_fjm(
self.memory_width, fjm_writer, self.first_address, self.next_wflip_address, self.fj_words, self.wflip_words
)

def _insert_wflip_label(self, address: int) -> None:
self.labels[f'{WFLIP_LABEL_PREFIX}{self.wflips_so_far}'] = address
self.wflips_so_far += 1

def insert_fj_op(self, flip: int, jump: int) -> None:
self.fj_words += (flip, jump)
self.current_address += 2*self.memory_width
self.current_address += 2 * self.memory_width

def insert_wflip_ops(self, word_address: int, flip_value: int, return_address: int) -> None:
if 0 == flip_value:
Expand Down Expand Up @@ -171,8 +178,7 @@ def insert_reserve_bits(self, fjm_writer: Writer, new_first_address: int) -> Non
self.padding_ops_indices.clear()


def labels_resolve(ops: Deque[LastPhaseOp], labels: Dict[str, int],
memory_width: int, fjm_writer: Writer) -> None:
def labels_resolve(ops: Deque[LastPhaseOp], labels: Dict[str, int], memory_width: int, fjm_writer: Writer) -> None:
"""
resolve the labels and expressions to get the list of fj ops, and add all the data and segments into the fjm_writer.
@param ops:[in]: the list ops returned from the preprocessor stage
Expand All @@ -195,8 +201,9 @@ def labels_resolve(ops: Deque[LastPhaseOp], labels: Dict[str, int],

elif isinstance(op, WordFlip):
try:
binary_data.insert_wflip_ops(op.get_word_address(labels), op.get_flip_value(labels),
op.get_return_address(labels))
binary_data.insert_wflip_ops(
op.get_word_address(labels), op.get_flip_value(labels), op.get_return_address(labels)
)
except FlipJumpException as e:
raise FlipJumpAssemblerException(f"{e} in op {op}.")

Expand All @@ -215,16 +222,17 @@ def labels_resolve(ops: Deque[LastPhaseOp], labels: Dict[str, int],
binary_data.close_and_add_segment(fjm_writer)


def assemble(input_files: List[Tuple[str, Path]],
memory_width: int,
fjm_writer: Writer,
*,
warning_as_errors: bool = True,
debugging_file_path: Optional[Path] = None,
show_statistics: bool = False,
print_time: bool = True,
max_recursion_depth: int = DEFAULT_MAX_MACRO_RECURSION_DEPTH,
) -> None:
def assemble(
input_files: List[Tuple[str, Path]],
memory_width: int,
fjm_writer: Writer,
*,
warning_as_errors: bool = True,
debugging_file_path: Optional[Path] = None,
show_statistics: bool = False,
print_time: bool = True,
max_recursion_depth: int = DEFAULT_MAX_MACRO_RECURSION_DEPTH,
) -> None:
"""
runs the assembly pipeline. assembles the input files to a .fjm.
:param input_files:[in]: a list of (short_file_name, fj_file_path). The files will to be parsed in that given order.
Expand All @@ -242,8 +250,9 @@ def assemble(input_files: List[Tuple[str, Path]],
macros = parse_macro_tree(input_files, memory_width, warning_as_errors)

with PrintTimer(' macro resolve: ', print_time=print_time):
ops, labels = resolve_macros(memory_width, macros,
show_statistics=show_statistics, max_recursion_depth=max_recursion_depth)
ops, labels = resolve_macros(
memory_width, macros, show_statistics=show_statistics, max_recursion_depth=max_recursion_depth
)

with PrintTimer(' labels resolve: ', print_time=print_time):
labels_resolve(ops, labels, memory_width, fjm_writer)
Expand All @@ -255,5 +264,6 @@ def assemble(input_files: List[Tuple[str, Path]],
except FlipJumpException as fj_exception:
raise fj_exception
except Exception as unknown_exception:
raise FlipJumpAssemblerException("Unknown exception during assembling the .fj files, please report this bug") \
from unknown_exception
raise FlipJumpAssemblerException(
"Unknown exception during assembling the .fj files, please report this bug"
) from unknown_exception
Loading

0 comments on commit 9480b0b

Please sign in to comment.