Skip to content

Commit 357f933

Browse files
authored
Merge pull request #65 from adafruit/better-error-sideset-delay
Better diagnose the incorrect lines like "side 1" or "[5]"
2 parents 73170eb + be66c02 commit 357f933

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

Diff for: adafruit_pioasm.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ def __init__(self, text_program: str, *, build_debuginfo: bool = False) -> None:
9494
for line in instructions:
9595
instruction = splitter(line.strip())
9696
delay = 0
97-
if instruction[-1].endswith("]"): # Delay
97+
if len(instruction) > 1 and instruction[-1].endswith("]"): # Delay
9898
delay = int(instruction[-1].strip("[]"), 0)
9999
if delay < 0:
100100
raise RuntimeError("Delay negative:", delay)
101101
if delay > max_delay:
102102
raise RuntimeError("Delay too long:", delay)
103103
instruction.pop()
104-
if len(instruction) > 1 and instruction[-2] == "side":
104+
if len(instruction) > 2 and instruction[-2] == "side":
105105
if sideset_count == 0:
106106
raise RuntimeError("No side_set count set")
107107
sideset_value = int(instruction[-1], 0)
@@ -236,7 +236,7 @@ def __init__(self, text_program: str, *, build_debuginfo: bool = False) -> None:
236236
raise RuntimeError("Set value out of range")
237237
assembled[-1] |= value
238238
else:
239-
raise RuntimeError("Unknown instruction:" + instruction[0])
239+
raise RuntimeError(f"Unknown instruction: {instruction[0]}")
240240
assembled[-1] |= delay << 8
241241
# print(bin(assembled[-1]))
242242

Diff for: tests/test_misc.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-FileCopyrightText: KB Sriram
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Tests out
7+
"""
8+
9+
from pytest_helpers import assert_assembly_fails
10+
11+
12+
def test_invalid_sideset() -> None:
13+
source = [
14+
".side_set 2",
15+
"side 2 [5]",
16+
]
17+
assert_assembly_fails(
18+
"\n".join(source), match="Unknown instruction: side", errtype=RuntimeError
19+
)
20+
21+
source = [
22+
".side_set 2",
23+
"side 2",
24+
]
25+
assert_assembly_fails(
26+
"\n".join(source), match="Unknown instruction: side", errtype=RuntimeError
27+
)
28+
29+
30+
def test_invalid_delay() -> None:
31+
assert_assembly_fails(
32+
"[5]", match=r"Unknown instruction: \[5\]", errtype=RuntimeError
33+
)
34+
35+
36+
def test_invalid_instruction() -> None:
37+
assert_assembly_fails(
38+
"bad", match=r"Unknown instruction: bad", errtype=RuntimeError
39+
)

0 commit comments

Comments
 (0)