Skip to content

Commit 3922cdd

Browse files
authored
Merge pull request #32 from dannystaple/patch-1
Add helpful wrappers for errors
2 parents 5bfd524 + df06622 commit 3922cdd

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ bundles
1616
dist
1717
**/*.egg-info
1818
.vscode
19+
.venv

adafruit_pioasm.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ def assemble(text_program):
103103
raise SyntaxError(f"Invalid jmp target {repr(target)}")
104104

105105
if len(instruction) > 2:
106-
assembled[-1] |= CONDITIONS.index(instruction[1]) << 5
106+
try:
107+
assembled[-1] |= CONDITIONS.index(instruction[1]) << 5
108+
except ValueError as exc:
109+
raise ValueError(
110+
f"Invalid jmp condition '{instruction[1]}'"
111+
) from exc
107112

108113
elif instruction[0] == "wait":
109114
# instr delay p sr index
@@ -151,7 +156,10 @@ def assemble(text_program):
151156
source = instruction[-1]
152157
source_split = mov_splitter(source)
153158
if len(source_split) == 1:
154-
assembled[-1] |= MOV_SOURCES.index(source)
159+
try:
160+
assembled[-1] |= MOV_SOURCES.index(source)
161+
except ValueError as exc:
162+
raise ValueError(f"Invalid mov source '{source}'") from exc
155163
else:
156164
assembled[-1] |= MOV_SOURCES.index(source_split[1])
157165
if source[:1] == "!":
@@ -183,7 +191,10 @@ def assemble(text_program):
183191
elif instruction[0] == "set":
184192
# instr delay dst data
185193
assembled.append(0b111_00000_000_00000)
186-
assembled[-1] |= SET_DESTINATIONS.index(instruction[1]) << 5
194+
try:
195+
assembled[-1] |= SET_DESTINATIONS.index(instruction[1]) << 5
196+
except ValueError as exc:
197+
raise ValueError(f"Invalid set destination '{instruction[1]}'") from exc
187198
value = int(instruction[-1])
188199
if not 0 <= value <= 31:
189200
raise RuntimeError("Set value out of range")

tests/testpioasm.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ def assertAssemblesTo(self, source, expected):
2929
f"Assembling {source!r}: Expected {expected_bin}, got {actual_bin}",
3030
)
3131

32-
def assertAssemblyFails(self, source):
33-
self.assertRaises(RuntimeError, adafruit_pioasm.assemble, source)
32+
def assertAssemblyFails(self, source, match=None, errtype=RuntimeError):
33+
if match:
34+
self.assertRaisesRegex(errtype, match, adafruit_pioasm.assemble, source)
35+
else:
36+
self.assertRaises(errtype, adafruit_pioasm.assemble, source)
3437

3538
def testNonsense(self):
3639
self.assertAssemblyFails("nope")
@@ -52,6 +55,18 @@ def testSidesetOpt(self):
5255
)
5356
self.assertAssemblesTo(".side_set 1 opt\nnop [1]", [0b101_00001_010_00_010])
5457

58+
def testMov(self):
59+
# non happy path
60+
self.assertAssemblyFails(
61+
"mov x, blah", match="Invalid mov source 'blah'", errtype=ValueError
62+
)
63+
64+
def testSet(self):
65+
# non happy path
66+
self.assertAssemblyFails(
67+
"set isr, 1", match="Invalid set destination 'isr'", errtype=ValueError
68+
)
69+
5570
def testJmp(self):
5671
self.assertAssemblesTo("l:\njmp l", [0b000_00000_000_00000])
5772
self.assertAssemblesTo("l:\njmp 7", [0b000_00000_000_00111])
@@ -63,6 +78,10 @@ def testJmp(self):
6378
self.assertAssemblesTo("jmp x!=y, l\nl:", [0b000_00000_101_00001])
6479
self.assertAssemblesTo("jmp pin, l\nl:", [0b000_00000_110_00001])
6580
self.assertAssemblesTo("jmp !osre, l\nl:", [0b000_00000_111_00001])
81+
# non happy path
82+
self.assertAssemblyFails(
83+
"jmp x--., l\nl:", match="Invalid jmp condition 'x--.'", errtype=ValueError
84+
)
6685

6786
def testWait(self):
6887
self.assertAssemblesTo("wait 0 gpio 0", [0b001_00000_0_00_00000])

0 commit comments

Comments
 (0)