From be66c02eb3199e5540888b3375fcf3374206b0c0 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 10 Jul 2024 08:09:46 -0500 Subject: [PATCH] Better diagnose the incorrect lines like "side 1" or "[5]" Before this, the exception would be 'IndexError: list index out of range'. Now it is 'Unknown instruction: side' or similar. (These need an instruction, even if it's a `nop`: `nop side 1 [5]`) --- adafruit_pioasm.py | 6 +++--- tests/test_misc.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tests/test_misc.py diff --git a/adafruit_pioasm.py b/adafruit_pioasm.py index 514d1ba..fa5a529 100644 --- a/adafruit_pioasm.py +++ b/adafruit_pioasm.py @@ -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) @@ -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])) diff --git a/tests/test_misc.py b/tests/test_misc.py new file mode 100644 index 0000000..ea07e51 --- /dev/null +++ b/tests/test_misc.py @@ -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 + )