Skip to content

Commit

Permalink
Merge pull request #65 from adafruit/better-error-sideset-delay
Browse files Browse the repository at this point in the history
Better diagnose the incorrect lines like "side 1" or "[5]"
  • Loading branch information
jepler authored Jul 11, 2024
2 parents 73170eb + be66c02 commit 357f933
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
6 changes: 3 additions & 3 deletions adafruit_pioasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ def __init__(self, text_program: str, *, build_debuginfo: bool = False) -> None:
for line in instructions:
instruction = splitter(line.strip())
delay = 0
if instruction[-1].endswith("]"): # Delay
if len(instruction) > 1 and instruction[-1].endswith("]"): # Delay
delay = int(instruction[-1].strip("[]"), 0)
if delay < 0:
raise RuntimeError("Delay negative:", delay)
if delay > max_delay:
raise RuntimeError("Delay too long:", delay)
instruction.pop()
if len(instruction) > 1 and instruction[-2] == "side":
if len(instruction) > 2 and instruction[-2] == "side":
if sideset_count == 0:
raise RuntimeError("No side_set count set")
sideset_value = int(instruction[-1], 0)
Expand Down Expand Up @@ -236,7 +236,7 @@ def __init__(self, text_program: str, *, build_debuginfo: bool = False) -> None:
raise RuntimeError("Set value out of range")
assembled[-1] |= value
else:
raise RuntimeError("Unknown instruction:" + instruction[0])
raise RuntimeError(f"Unknown instruction: {instruction[0]}")
assembled[-1] |= delay << 8
# print(bin(assembled[-1]))

Expand Down
39 changes: 39 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# SPDX-FileCopyrightText: KB Sriram
#
# SPDX-License-Identifier: MIT

"""
Tests out
"""

from pytest_helpers import assert_assembly_fails


def test_invalid_sideset() -> None:
source = [
".side_set 2",
"side 2 [5]",
]
assert_assembly_fails(
"\n".join(source), match="Unknown instruction: side", errtype=RuntimeError
)

source = [
".side_set 2",
"side 2",
]
assert_assembly_fails(
"\n".join(source), match="Unknown instruction: side", errtype=RuntimeError
)


def test_invalid_delay() -> None:
assert_assembly_fails(
"[5]", match=r"Unknown instruction: \[5\]", errtype=RuntimeError
)


def test_invalid_instruction() -> None:
assert_assembly_fails(
"bad", match=r"Unknown instruction: bad", errtype=RuntimeError
)

0 comments on commit 357f933

Please sign in to comment.