diff --git a/.flake8 b/.flake8 deleted file mode 100644 index b8aba99..0000000 --- a/.flake8 +++ /dev/null @@ -1,24 +0,0 @@ -[flake8] -# Specify the maximum allowed line length -max-line-length = 88 - -# Ignore specific rules -# For example, E501: Line too long, W503: Line break before binary operator -ignore = E501, W503, W504, E226, E265 - -# Exclude specific files or directories -exclude = - .git, - __pycache__, - build, - dist, - .conda, - .venv - -# Enable specific plugins or options -# Example: Enabling flake8-docstrings -select = C,E,F,W,D - -# Specify custom error codes to ignore or enable -per-file-ignores = - tests/*: D, E712 \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index c992751..d9b30fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,7 @@ copapy = ["obj/*.o", "py.typed"] [project.optional-dependencies] dev = [ - "flake8", + "ruff", "mypy", "pytest" ] @@ -50,4 +50,23 @@ show_error_codes = true minversion = "6.0" addopts = "-ra -q" testpaths = ["tests"] -pythonpath = ["src"] \ No newline at end of file +pythonpath = ["src"] + +[tool.ruff] +lint.ignore = ["E501", "E226", "E265"] + +# Equivalent to Flake8's "exclude" +exclude = [ + ".git", + "__pycache__", + "build", + "dist", + ".conda", + ".venv", +] + +# "D" for dockstrings +lint.select = ["C", "E", "F", "W"] + +[tool.ruff.lint.per-file-ignores] +"tests/*" = ["D", "E712"] diff --git a/src/copapy/_basic_types.py b/src/copapy/_basic_types.py index b1e2dad..c9132cc 100644 --- a/src/copapy/_basic_types.py +++ b/src/copapy/_basic_types.py @@ -161,11 +161,11 @@ class variable(Generic[TNum], Net): def __lt__(self, other: NumLike) -> 'variable[bool]': ret = add_op('gt', [other, self]) return variable(ret.source, dtype='bool') - + def __ge__(self, other: NumLike) -> 'variable[bool]': ret = add_op('ge', [self, other]) return variable(ret.source, dtype='bool') - + def __le__(self, other: NumLike) -> 'variable[bool]': ret = add_op('ge', [other, self]) return variable(ret.source, dtype='bool') diff --git a/src/copapy/_binwrite.py b/src/copapy/_binwrite.py index 627b3ae..294d0d3 100644 --- a/src/copapy/_binwrite.py +++ b/src/copapy/_binwrite.py @@ -14,7 +14,7 @@ COMMAND_SIZE = 4 class data_writer(): def __init__(self, byteorder: ByteOrder): - self._data: list[tuple[str, bytes, int]] = list() + self._data: list[tuple[str, bytes, int]] = [] self.byteorder: ByteOrder = byteorder def write_int(self, value: int, num_bytes: int = 4, signed: bool = False) -> None: diff --git a/src/copapy/_math.py b/src/copapy/_math.py index 2fe33d8..1adec2a 100644 --- a/src/copapy/_math.py +++ b/src/copapy/_math.py @@ -37,4 +37,3 @@ def abs(x: T) -> T: ret = (x < 0) * -x + (x >= 0) * x return ret # pyright: ignore[reportReturnType] - diff --git a/src/copapy/_target.py b/src/copapy/_target.py index 3b9c63d..2668641 100644 --- a/src/copapy/_target.py +++ b/src/copapy/_target.py @@ -4,7 +4,7 @@ from coparun_module import coparun, read_data_mem import struct from ._basic_types import stencil_db_from_package from ._basic_types import variable, Net, Node, Write, NumLike -from ._compiler import compile_to_instruction_list +from ._compiler import compile_to_dag def add_read_command(dw: binw.data_writer, variables: dict[Net, tuple[int, int, str]], net: Net) -> None: @@ -18,7 +18,7 @@ def add_read_command(dw: binw.data_writer, variables: dict[Net, tuple[int, int, class Target(): def __init__(self, arch: str = 'native', optimization: str = 'O3') -> None: self.sdb = stencil_db_from_package(arch, optimization) - self._variables: dict[Net, tuple[int, int, str]] = dict() + self._variables: dict[Net, tuple[int, int, str]] = {} def compile(self, *variables: int | float | variable[int] | variable[float] | variable[bool] | Iterable[int | float | variable[int] | variable[float] | variable[bool]]) -> None: nodes: list[Node] = [] @@ -30,7 +30,7 @@ class Target(): else: nodes.append(Write(s)) - dw, self._variables = compile_to_instruction_list(nodes, self.sdb) + dw, self._variables = compile_to_dag(nodes, self.sdb) dw.write_com(binw.Command.END_COM) assert coparun(dw.get_data()) > 0 diff --git a/src/copapy/backend.py b/src/copapy/backend.py index 6d4d9e9..6b02056 100644 --- a/src/copapy/backend.py +++ b/src/copapy/backend.py @@ -1,6 +1,6 @@ from ._target import add_read_command from ._basic_types import Net, Op, Node, CPConstant, Write -from ._compiler import compile_to_instruction_list, \ +from ._compiler import compile_to_dag, \ stable_toposort, get_const_nets, get_all_dag_edges, add_read_ops, \ add_write_ops @@ -11,7 +11,7 @@ __all__ = [ "Node", "CPConstant", "Write", - "compile_to_instruction_list", + "compile_to_dag", "stable_toposort", "get_const_nets", "get_all_dag_edges", diff --git a/tests/test_compile.py b/tests/test_compile.py index 8e3a018..8adeb84 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -1,5 +1,5 @@ from copapy import variable, NumLike -from copapy.backend import Write, compile_to_instruction_list, add_read_command +from copapy.backend import Write, compile_to_dag, add_read_command import copapy import subprocess import struct @@ -49,7 +49,7 @@ def test_compile(): out = [Write(r) for r in ret] - il, variables = compile_to_instruction_list(out, copapy.generic_sdb) + il, variables = compile_to_dag(out, copapy.generic_sdb) # run program command il.write_com(_binwrite.Command.RUN_PROG) diff --git a/tests/test_compile_div.py b/tests/test_compile_div.py index 06eaa5f..e199917 100644 --- a/tests/test_compile_div.py +++ b/tests/test_compile_div.py @@ -1,5 +1,5 @@ from copapy import variable, NumLike -from copapy.backend import Write, compile_to_instruction_list +from copapy.backend import Write, compile_to_dag import copapy import subprocess from copapy import _binwrite @@ -26,7 +26,7 @@ def test_compile(): out = [Write(r) for r in ret] - il, _ = compile_to_instruction_list(out, copapy.generic_sdb) + il, _ = compile_to_dag(out, copapy.generic_sdb) # run program command il.write_com(_binwrite.Command.RUN_PROG) diff --git a/tests/test_coparun_module2.py b/tests/test_coparun_module2.py index 77594ad..aa4ef3a 100644 --- a/tests/test_coparun_module2.py +++ b/tests/test_coparun_module2.py @@ -1,6 +1,6 @@ from coparun_module import coparun from copapy import variable -from copapy.backend import Write, compile_to_instruction_list, add_read_command +from copapy.backend import Write, compile_to_dag, add_read_command import copapy from copapy import _binwrite @@ -15,7 +15,7 @@ def test_compile(): r2 = i1 + 9 out = [Write(r1), Write(r2), Write(c2)] - il, variables = compile_to_instruction_list(out, copapy.generic_sdb) + il, variables = compile_to_dag(out, copapy.generic_sdb) # run program command il.write_com(_binwrite.Command.RUN_PROG) diff --git a/tests/test_crash_win.py b/tests/test_crash_win.py index 144549c..aab0a68 100644 --- a/tests/test_crash_win.py +++ b/tests/test_crash_win.py @@ -1,5 +1,5 @@ from copapy import NumLike, variable -from copapy.backend import Write, Net, compile_to_instruction_list, add_read_command +from copapy.backend import Write, Net, compile_to_dag, add_read_command import copapy import subprocess from copapy import _binwrite @@ -29,7 +29,7 @@ def test_compile(): ret = function(c1, c2) - dw, variable_list = compile_to_instruction_list([Write(net) for net in ret], copapy.generic_sdb) + dw, variable_list = compile_to_dag([Write(net) for net in ret], copapy.generic_sdb) # run program command dw.write_com(_binwrite.Command.RUN_PROG) diff --git a/tools/make_example.py b/tools/make_example.py index 37f3b05..6b47378 100644 --- a/tools/make_example.py +++ b/tools/make_example.py @@ -1,33 +1,43 @@ -from copapy import _binwrite, variable -from copapy.backend import Write, compile_to_instruction_list +from copapy import variable +from copapy.backend import Write, compile_to_dag import copapy as cp +from copapy._binwrite import Command def test_compile() -> None: - + """Test compilation of a simple program.""" c1 = variable(9.0) #ret = [c1 / 4, c1 / -4, c1 // 4, c1 // -4, (c1 * -1) // 4] - #ret = [c1 // 3.3 + 5] - ret = [cp.sqrt(c1)] + ret = [c1 // 3.3 + 5] + #ret = [cp.sqrt(c1)] + #c2 = cp._math.get_42() + #ret = [c2] out = [Write(r) for r in ret] - il, _ = compile_to_instruction_list(out, cp.generic_sdb) + dw, vars = compile_to_dag(out, cp.generic_sdb) # run program command - il.write_com(_binwrite.Command.RUN_PROG) + dw.write_com(Command.RUN_PROG) - il.write_com(_binwrite.Command.READ_DATA) - il.write_int(0) - il.write_int(36) + # read first 32 byte + dw.write_com(Command.READ_DATA) + dw.write_int(0) + dw.write_int(32) - il.write_com(_binwrite.Command.END_COM) + # read variables + for addr, lengths, _ in vars.values(): + dw.write_com(Command.READ_DATA) + dw.write_int(addr) + dw.write_int(lengths) + + dw.write_com(Command.END_COM) print('* Data to runner:') - il.print() + dw.print() - il.to_file('bin/test.copapy') + dw.to_file('bin/test.copapy') if __name__ == "__main__":