Skip to content

Commit e2539c2

Browse files
committed
feat: implement temporary labels
Also allows numbers with _ in the ASM context!
1 parent 8f839ab commit e2539c2

15 files changed

Lines changed: 61 additions & 9 deletions

src/zxbasm/asmlex.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def t_CHAR(self, t):
246246
return t
247247

248248
def t_HEXA(self, t):
249-
r'([0-9][0-9a-fA-F]*[hH])|(\$[0-9a-fA-F]+)|(0x[0-9a-fA-F]+)'
249+
r'([0-9](_?[0-9a-fA-F])*[hH])|(\$[0-9a-fA-F](_?[0-9a-fA-F])*)|(0x[0-9a-fA-F](_?[0-9a-dA-F])*)'
250250

251251
if t.value[:2] == '0x':
252252
t.value = t.value[2:] # Remove initial 0x
@@ -255,22 +255,22 @@ def t_HEXA(self, t):
255255
else:
256256
t.value = t.value[:-1] # Remove last 'h'
257257

258-
t.value = int(t.value, 16) # Convert to decimal
258+
t.value = int(t.value.replace('_', ''), 16) # Convert to decimal
259259
t.type = 'INTEGER'
260260
return t
261261

262262
def t_BIN(self, t):
263-
r'(%[01]+)|([01]+[bB])' # A Binary integer
263+
r'(%[01](_?[01])*)|(0[bB](_?[01])+)' # A Binary integer
264264
# Note 00B is a 0 binary, but
265265
# 00Bh is a 12 in hex. So this pattern must come
266266
# after HEXA
267267

268268
if t.value[0] == '%':
269269
t.value = t.value[1:] # Remove initial %
270270
else:
271-
t.value = t.value[:-1] # Remove last 'b'
271+
t.value = t.value[2:] # Remove last 'b'
272272

273-
t.value = int(t.value, 2) # Convert to decimal
273+
t.value = int(t.value.replace('_', ''), 2) # Convert to decimal
274274
t.type = 'INTEGER'
275275
return t
276276

@@ -281,8 +281,8 @@ def t_INITIAL_TMPLABEL(self, t):
281281
return t
282282

283283
def t_INITIAL_preproc_INTEGER(self, t):
284-
r'[0-9]+' # an integer decimal number
285-
t.value = int(t.value)
284+
r'[0-9](_?\d)*' # an integer decimal number
285+
t.value = int(t.value.replace('_', ''))
286286
return t
287287

288288
def t_INITIAL_ID(self, t):

src/zxbasm/memory.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def enter_proc(self, lineno: int):
4040
"""
4141
self.local_labels.append({}) # Add a new context
4242
self.scopes.append(lineno)
43-
self.clear_temporary_labels()
4443
__DEBUG__('Entering scope level %i at line %i' % (len(self.scopes), lineno))
4544

4645
def set_org(self, value: int, lineno: int):
@@ -118,7 +117,6 @@ def exit_proc(self, lineno: int):
118117

119118
self.local_labels.pop() # Removes current context
120119
self.scopes.pop()
121-
self.clear_temporary_labels()
122120

123121
def set_memory_slot(self):
124122
if self.org not in self.orgs:

tests/functional/hex_num.asm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
ld a, $1f
4+
ld a, $1e
5+

tests/functional/hex_num.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
>>

tests/functional/tmp_label0.asm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1:
2+
ld hl, 1b
3+
ld hl, (1b)
4+
ld hl, 1f
5+
ld hl, (1f)
6+
1:
7+

tests/functional/tmp_label0.bin

12 Bytes
Binary file not shown.

tests/functional/tmp_label1.asm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1:
2+
nop
3+
ld hl, 1f
4+

tests/functional/tmp_label2.asm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
ld hl, 1b
3+
1:
4+

tests/functional/tmp_label3.asm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
;; Cannot access labels in different namespaces
2+
push namespace test
3+
1:
4+
pop namespace
5+
6+
ld hl, 1b
7+

tests/functional/tmp_label4.asm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
1:
3+
push namespace test
4+
nop
5+
1:
6+
ld hl, 1b
7+
pop namespace
8+
ld hl, 1b
9+

0 commit comments

Comments
 (0)