Skip to content

Commit

Permalink
Merge pull request #4 from Deric-W/hatch
Browse files Browse the repository at this point in the history
switch build system to Hatch
  • Loading branch information
Deric-W authored Oct 22, 2023
2 parents f46e418 + 7a10dae commit 4aae4b8
Show file tree
Hide file tree
Showing 15 changed files with 341 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 100
extend-ignore = E221,E501
39 changes: 19 additions & 20 deletions .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,42 @@ on: [push, workflow_dispatch]

jobs:
Test:
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
os: [ubuntu-latest]

runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install test dependencies
run: python -m pip install mypy coverage build

- name: Build wheel
run: python -m build
python-version: |
3.10
3.11
3.12
- name: Install wheel
run: python -m pip install --no-cache-dir dist/AMN-*.whl
- name: Install Hatch
run: python -m pip install hatch

- name: Perform release check
run: hatch run lint:release

- name: Run MyPy
run: python -m mypy -p AMN
- name: Run tests
run: hatch run test:cov --verbose

- name: Run tests and generate report
run: coverage run -m unittest discover --verbose
- name: Generate report
run: hatch env run -e test.py3.12 coverage xml

- name: Upload coverage
uses: codecov/codecov-action@v3
with:
flags: ${{ runner.os }}
verbose: true

- name: Build wheel
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
run: hatch build

- name: Publish package
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ celerybeat.pid
# Environments
.env
.venv
.direnv
env/
venv/
ENV/
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# AMN

[![Hatch project](https://img.shields.io/badge/%F0%9F%A5%9A-Hatch-4051b5.svg)](https://github.com/pypa/hatch)
![Tests](https://github.com/Deric-W/AMN/actions/workflows/Tests.yaml/badge.svg)
[![codecov](https://codecov.io/gh/Deric-W/AMN/branch/main/graph/badge.svg?token=SU3982mC17)](https://codecov.io/gh/Deric-W/AMN)

Expand Down
58 changes: 52 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "AMN"
version = "0.6.0"
version = "0.6.1"
description = "Virtual machines for the AMN instructions sets"
requires-python = ">=3.10"
keywords = [
Expand Down Expand Up @@ -35,11 +35,49 @@ Bugtracker = "https://github.com/Deric-W/AMN/issues"
amn = "AMN.main:main"

[build-system]
requires = ["setuptools >= 61.0.0"]
build-backend = "setuptools.build_meta"
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.setuptools.package-data]
AMN = ["py.typed"]
[tool.hatch.envs.test]
dependencies = [
"coverage[toml] == 7.*"
]

[tool.hatch.envs.test.scripts]
test = "python -m unittest discover {args}"
cov-run = "coverage run -m unittest discover {args}"
cov-report = [
"- coverage combine",
"coverage report"
]
cov = [
"cov-run",
"cov-report"
]

[[tool.hatch.envs.test.matrix]]
python = ["3.10", "3.11", "3.12"]

[tool.hatch.envs.lint]
dependencies = [
"mypy >= 1.0.0",
"pylint >= 2.12.2",
"flake8 >= 5.0.0",
"isort >= 5.10.1"
]

[tool.hatch.envs.lint.scripts]
lint = [
"- flake8 src/AMN",
"- pylint src/AMN"
]
typecheck = "mypy -p AMN"
release = [
"typecheck"
]

[tool.hatch.build.targets.sdist]
exclude = ["/.github"]

[tool.mypy]
disallow_any_unimported = true
Expand All @@ -50,4 +88,12 @@ strict_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_return_any = true
warn_unreachable = false # problems with match statement
warn_unreachable = false # problems with match statement

[tool.pylint]
max-line-length = 100

[tool.coverage.run]
source_pkgs = ["AMN"]
branch = true
parallel = true
2 changes: 1 addition & 1 deletion AMN/__init__.py → src/AMN/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections.abc import Iterator, Iterable, Sequence, Mapping
from typing import Type, TypeVar, Generic

__version__ = "0.6.0"
__version__ = "0.6.1"
__author__ = "Eric Niklas Wolf"
__email__ = "eric_niklas.wolf@mailbox.tu-dresden.de"
__all__ = (
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions AMN/am0.py → src/AMN/am0.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,22 @@ def execute_instruction(self, instruction: tuple[Instruction, int]) -> int | Non
self.stack[-2] = self.stack[-2] % self.stack[-1]
self.stack.pop()
case (Instruction.EQ, _):
self.stack[-2] = self.stack[-2] == self.stack[-1]
self.stack[-2] = int(self.stack[-2] == self.stack[-1])
self.stack.pop()
case (Instruction.NE, _):
self.stack[-2] = self.stack[-2] != self.stack[-1]
self.stack[-2] = int(self.stack[-2] != self.stack[-1])
self.stack.pop()
case (Instruction.LT, _):
self.stack[-2] = self.stack[-2] < self.stack[-1]
self.stack[-2] = int(self.stack[-2] < self.stack[-1])
self.stack.pop()
case (Instruction.GT, _):
self.stack[-2] = self.stack[-2] > self.stack[-1]
self.stack[-2] = int(self.stack[-2] > self.stack[-1])
self.stack.pop()
case (Instruction.LE, _):
self.stack[-2] = self.stack[-2] <= self.stack[-1]
self.stack[-2] = int(self.stack[-2] <= self.stack[-1])
self.stack.pop()
case (Instruction.GE, _):
self.stack[-2] = self.stack[-2] >= self.stack[-1]
self.stack[-2] = int(self.stack[-2] >= self.stack[-1])
self.stack.pop()
case (Instruction.LOAD, address):
self.stack.append(self.memory[address])
Expand Down
12 changes: 6 additions & 6 deletions AMN/am1.py → src/AMN/am1.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,22 @@ def execute_instruction(self, instruction: tuple[Instruction, MemoryContext, int
self.stack[-2] = self.stack[-2] % self.stack[-1]
self.stack.pop()
case (Instruction.EQ, _, _):
self.stack[-2] = self.stack[-2] == self.stack[-1]
self.stack[-2] = int(self.stack[-2] == self.stack[-1])
self.stack.pop()
case (Instruction.NE, _, _):
self.stack[-2] = self.stack[-2] != self.stack[-1]
self.stack[-2] = int(self.stack[-2] != self.stack[-1])
self.stack.pop()
case (Instruction.LT, _, _):
self.stack[-2] = self.stack[-2] < self.stack[-1]
self.stack[-2] = int(self.stack[-2] < self.stack[-1])
self.stack.pop()
case (Instruction.GT, _, _):
self.stack[-2] = self.stack[-2] > self.stack[-1]
self.stack[-2] = int(self.stack[-2] > self.stack[-1])
self.stack.pop()
case (Instruction.LE, _, _):
self.stack[-2] = self.stack[-2] <= self.stack[-1]
self.stack[-2] = int(self.stack[-2] <= self.stack[-1])
self.stack.pop()
case (Instruction.GE, _, _):
self.stack[-2] = self.stack[-2] >= self.stack[-1]
self.stack[-2] = int(self.stack[-2] >= self.stack[-1])
self.stack.pop()
case (Instruction.LOAD, context, address):
index = context.resolve_address(address, self.reference_pointer) - 1
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
61 changes: 55 additions & 6 deletions tests/test_am0.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


# do not remove trailing whitespace!
EXAMPLE_PROGRAM = \
EXAMPLE_PROGRAM1 = \
"""
READ 2;
LIT 1;
Expand All @@ -34,6 +34,50 @@
WRITE 3;
"""

EXAMPLE_PROGRAM2 = \
"""
READ 1;
LOAD 1;
LIT 7;
LIT 1;
SUB;
DIV;
LIT 2;
MOD;
STORE 1;
LOAD 1;
LIT 0;
EQ;
LOAD 1;
LIT 0;
NE;
LOAD 1;
LIT 0;
LT;
LOAD 1;
LIT 0;
GT;
LOAD 1;
LIT 1;
LE;
LOAD 1;
LIT 1;
GE;
WRITE 1;
STORE 2;
WRITE 2;
STORE 2;
WRITE 2;
STORE 2;
WRITE 2;
STORE 2;
WRITE 2;
STORE 2;
WRITE 2;
STORE 2;
WRITE 2;
"""


class InstructionTest(TestCase):
"""AM0 instruction tests"""
Expand All @@ -56,7 +100,7 @@ def test_parse(self) -> None:
def test_parse_program(self) -> None:
"""test program parsing"""
self.assertEqual(
list(Instruction.parse_program(EXAMPLE_PROGRAM)),
list(Instruction.parse_program(EXAMPLE_PROGRAM1)),
[
(Instruction.READ, 2),
(Instruction.LIT, 1),
Expand Down Expand Up @@ -133,7 +177,7 @@ def test_default(self) -> None:

def test_reset(self) -> None:
"""test reset state"""
iterator = iter([])
iterator = iter(range(0))
machine = Machine(42, [1, 2], {3: 4, 5: 6}, iterator)
machine.reset()
self.assertEqual(machine.counter, 1)
Expand Down Expand Up @@ -166,10 +210,15 @@ def test_state(self) -> None:

def test_execute(self) -> None:
"""test program execution"""
program = tuple(Instruction.parse_program(EXAMPLE_PROGRAM))
program1 = tuple(Instruction.parse_program(EXAMPLE_PROGRAM1))
program2 = tuple(Instruction.parse_program(EXAMPLE_PROGRAM2))
machine = Machine.default(iter([2, 42]))
self.assertEqual(
list(machine.execute_program(program)),
list(machine.execute_program(program1)),
[None] * 39 + [5]
)
self.assertEqual(next(machine.input), 42)
self.assertEqual(
list(filter(lambda value: value is not None, machine.execute_program(program2))),
[1, 1, 1, 1, 0, 1, 0]
)
self.assertIsNone(next(machine.input, None))
Loading

0 comments on commit 4aae4b8

Please sign in to comment.