mirror of
https://github.com/mnaberez/py65.git
synced 2025-08-07 07:29:02 +00:00
Catch bad label syntax and raise KeyError
This commit is contained in:
@@ -97,6 +97,15 @@ class AddressParserTests(unittest.TestCase):
|
||||
except KeyError, why:
|
||||
self.assertEqual('Label not found: bad_label', why[0])
|
||||
|
||||
def test_number_bad_label_syntax(self):
|
||||
parser = AddressParser()
|
||||
parser.labels = {'foo': 0xFFFF}
|
||||
try:
|
||||
parser.number('#$foo')
|
||||
self.fail()
|
||||
except KeyError, why:
|
||||
self.assertEqual('Label not found: #$foo', why[0])
|
||||
|
||||
def test_number_constrains_address_at_zero_or_above(self):
|
||||
parser = AddressParser()
|
||||
self.assertEqual(0x0000, parser.number('-1'))
|
||||
|
@@ -37,16 +37,21 @@ class AddressParser(object):
|
||||
def number(self, num):
|
||||
"""Parse a string containing a label or number into an address.
|
||||
"""
|
||||
try:
|
||||
if num.startswith('$'):
|
||||
# hexadecimal
|
||||
return self._constrain(int(num[1:], 16))
|
||||
|
||||
elif num.startswith('+'):
|
||||
# decimal
|
||||
return self._constrain(int(num[1:], 10))
|
||||
|
||||
elif num.startswith('%'):
|
||||
# binary
|
||||
return self._constrain(int(num[1:], 2))
|
||||
|
||||
elif num in self.labels:
|
||||
# label name
|
||||
return self.labels[num]
|
||||
|
||||
else:
|
||||
@@ -68,8 +73,8 @@ class AddressParser(object):
|
||||
return self._constrain(address)
|
||||
|
||||
else:
|
||||
try:
|
||||
return self._constrain(int(num, self.radix))
|
||||
|
||||
except ValueError:
|
||||
raise KeyError("Label not found: %s" % num)
|
||||
|
||||
|
Reference in New Issue
Block a user