Skip to content

Add helpful wrappers for errors #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ bundles
dist
**/*.egg-info
.vscode
.venv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always push back gently about additions like this to gitignore files.

If you commonly create ".venv" in projects where you work, it's better if you use your own global gitignore file, because it'll allow git to ignore ".venv" in any project on your system, without modifying the project gitignore file.

Here's a quick read that shows how to set it up: https://sebastiandedeyne.com/setting-up-a-global-gitignore-file/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh - didn't know about that. Thanks.

17 changes: 14 additions & 3 deletions adafruit_pioasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ def assemble(text_program):
raise SyntaxError(f"Invalid jmp target {repr(target)}")

if len(instruction) > 2:
assembled[-1] |= CONDITIONS.index(instruction[1]) << 5
try:
assembled[-1] |= CONDITIONS.index(instruction[1]) << 5
except ValueError as exc:
raise ValueError(
f"Invalid jmp condition '{instruction[1]}'"
) from exc

elif instruction[0] == "wait":
# instr delay p sr index
Expand Down Expand Up @@ -151,7 +156,10 @@ def assemble(text_program):
source = instruction[-1]
source_split = mov_splitter(source)
if len(source_split) == 1:
assembled[-1] |= MOV_SOURCES.index(source)
try:
assembled[-1] |= MOV_SOURCES.index(source)
except ValueError as exc:
raise ValueError(f"Invalid mov source '{source}'") from exc
else:
assembled[-1] |= MOV_SOURCES.index(source_split[1])
if source[:1] == "!":
Expand Down Expand Up @@ -183,7 +191,10 @@ def assemble(text_program):
elif instruction[0] == "set":
# instr delay dst data
assembled.append(0b111_00000_000_00000)
assembled[-1] |= SET_DESTINATIONS.index(instruction[1]) << 5
try:
assembled[-1] |= SET_DESTINATIONS.index(instruction[1]) << 5
except ValueError as exc:
raise ValueError(f"Invalid set destination '{instruction[1]}'") from exc
value = int(instruction[-1])
if not 0 <= value <= 31:
raise RuntimeError("Set value out of range")
Expand Down
23 changes: 21 additions & 2 deletions tests/testpioasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ def assertAssemblesTo(self, source, expected):
f"Assembling {source!r}: Expected {expected_bin}, got {actual_bin}",
)

def assertAssemblyFails(self, source):
self.assertRaises(RuntimeError, adafruit_pioasm.assemble, source)
def assertAssemblyFails(self, source, match=None, errtype=RuntimeError):
if match:
self.assertRaisesRegex(errtype, match, adafruit_pioasm.assemble, source)
else:
self.assertRaises(errtype, adafruit_pioasm.assemble, source)

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

def testMov(self):
# non happy path
self.assertAssemblyFails(
"mov x, blah", match="Invalid mov source 'blah'", errtype=ValueError
)

def testSet(self):
# non happy path
self.assertAssemblyFails(
"set isr, 1", match="Invalid set destination 'isr'", errtype=ValueError
)

def testJmp(self):
self.assertAssemblesTo("l:\njmp l", [0b000_00000_000_00000])
self.assertAssemblesTo("l:\njmp 7", [0b000_00000_000_00111])
Expand All @@ -63,6 +78,10 @@ def testJmp(self):
self.assertAssemblesTo("jmp x!=y, l\nl:", [0b000_00000_101_00001])
self.assertAssemblesTo("jmp pin, l\nl:", [0b000_00000_110_00001])
self.assertAssemblesTo("jmp !osre, l\nl:", [0b000_00000_111_00001])
# non happy path
self.assertAssemblyFails(
"jmp x--., l\nl:", match="Invalid jmp condition 'x--.'", errtype=ValueError
)

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