mirror of
https://github.com/mnaberez/py65.git
synced 2025-04-06 20:37:18 +00:00
Catch bad label syntax and raise KeyError
This commit is contained in:
parent
62cdc8363a
commit
0f60f9b3f6
@ -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,41 +37,46 @@ class AddressParser(object):
|
||||
def number(self, num):
|
||||
"""Parse a string containing a label or number into an address.
|
||||
"""
|
||||
if num.startswith('$'):
|
||||
return self._constrain(int(num[1:], 16))
|
||||
try:
|
||||
if num.startswith('$'):
|
||||
# hexadecimal
|
||||
return self._constrain(int(num[1:], 16))
|
||||
|
||||
elif num.startswith('+'):
|
||||
return self._constrain(int(num[1:], 10))
|
||||
elif num.startswith('+'):
|
||||
# decimal
|
||||
return self._constrain(int(num[1:], 10))
|
||||
|
||||
elif num.startswith('%'):
|
||||
return self._constrain(int(num[1:], 2))
|
||||
elif num.startswith('%'):
|
||||
# binary
|
||||
return self._constrain(int(num[1:], 2))
|
||||
|
||||
elif num in self.labels:
|
||||
return self.labels[num]
|
||||
|
||||
else:
|
||||
matches = re.match('^([^\s+-]+)\s*([+\-])\s*([$+%]?\d+)$', num)
|
||||
if matches:
|
||||
label, sign, offset = matches.groups()
|
||||
|
||||
if label not in self.labels:
|
||||
raise KeyError("Label not found: %s" % label)
|
||||
|
||||
base = self.labels[label]
|
||||
offset = self.number(offset)
|
||||
|
||||
if sign == '+':
|
||||
address = base + offset
|
||||
else:
|
||||
address = base - offset
|
||||
|
||||
return self._constrain(address)
|
||||
elif num in self.labels:
|
||||
# label name
|
||||
return self.labels[num]
|
||||
|
||||
else:
|
||||
try:
|
||||
matches = re.match('^([^\s+-]+)\s*([+\-])\s*([$+%]?\d+)$', num)
|
||||
if matches:
|
||||
label, sign, offset = matches.groups()
|
||||
|
||||
if label not in self.labels:
|
||||
raise KeyError("Label not found: %s" % label)
|
||||
|
||||
base = self.labels[label]
|
||||
offset = self.number(offset)
|
||||
|
||||
if sign == '+':
|
||||
address = base + offset
|
||||
else:
|
||||
address = base - offset
|
||||
|
||||
return self._constrain(address)
|
||||
|
||||
else:
|
||||
return self._constrain(int(num, self.radix))
|
||||
except ValueError:
|
||||
raise KeyError("Label not found: %s" % num)
|
||||
|
||||
except ValueError:
|
||||
raise KeyError("Label not found: %s" % num)
|
||||
|
||||
def range(self, addresses):
|
||||
"""Parse a string containing an address or a range of addresses
|
||||
|
Loading…
x
Reference in New Issue
Block a user