Skip to content

Commit 3ff4442

Browse files
committed
refact: Memory.declare_label() returns None
Improve typing. Also do some code clean up in asmlex.py
1 parent 30b3eb2 commit 3ff4442

3 files changed

Lines changed: 30 additions & 28 deletions

File tree

src/zxbasm/asmlex.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
# ----------------------------------------------------------------------
1313

1414
import sys
15+
from typing import Tuple
1516

1617
from src.ply import lex
1718
from src.api.config import OPTIONS
1819
from src.api.errmsg import error
1920

20-
_tokens = ('STRING', 'NEWLINE', 'CO',
21-
'ID', 'COMMA', 'PLUS', 'MINUS', 'LB', 'RB', 'LP', 'RP', 'LPP', 'RPP', 'MUL', 'DIV', 'POW', 'MOD',
22-
'UMINUS', 'APO', 'INTEGER', 'ADDR',
23-
'LSHIFT', 'RSHIFT', 'BAND', 'BOR', 'BXOR'
24-
)
21+
22+
_tokens: Tuple[str, ...] = (
23+
'STRING', 'NEWLINE', 'CO',
24+
'ID', 'COMMA', 'PLUS', 'MINUS', 'LB', 'RB',
25+
'LP', 'RP', 'LPP', 'RPP', 'MUL', 'DIV', 'POW',
26+
'MOD', 'UMINUS', 'APO', 'INTEGER', 'ADDR',
27+
'LSHIFT', 'RSHIFT', 'BAND', 'BOR', 'BXOR'
28+
)
2529

2630
reserved_instructions = {
2731
'adc': 'ADC',
@@ -173,7 +177,7 @@
173177
}
174178

175179
# List of token names.
176-
_tokens = sorted(
180+
_tokens = tuple(sorted(
177181
_tokens +
178182
tuple(reserved_instructions.values()) +
179183
tuple(pseudo.values()) +
@@ -182,7 +186,7 @@
182186
tuple(flags.values()) +
183187
tuple(zx_next_mnemonics.values()) +
184188
tuple(preprocessor.values())
185-
)
189+
))
186190

187191
keywords = set(
188192
flags.keys()).union(
@@ -218,20 +222,17 @@ class Lexer(object):
218222

219223
# -------------- TOKEN ACTIONS --------------
220224

221-
def __set_lineno(self, value):
222-
""" Setter for lexer.lineno
223-
"""
224-
self.lex.lineno = value
225-
226-
def __get_lineno(self):
225+
@property
226+
def lineno(self) -> int:
227227
""" Getter for lexer.lineno
228228
"""
229-
if self.lex is None:
230-
return 0
231-
232-
return self.lex.lineno
229+
return 0 if self.lex is None else self.lex.lineno
233230

234-
lineno = property(__get_lineno, __set_lineno)
231+
@lineno.setter
232+
def lineno(self, value: int):
233+
""" Setter for lexer.lineno
234+
"""
235+
self.lex.lineno = value
235236

236237
def t_INITIAL_preproc_skip(self, t):
237238
r'[ \t]+'
@@ -442,10 +443,10 @@ def __init__(self):
442443
self.tokens = tokens
443444
self.next_token = None # if set to something, this will be returned once
444445

445-
def input(self, str):
446+
def input(self, s: str):
446447
""" Defines input string, removing current lexer.
447448
"""
448-
self.input_data = str
449+
self.input_data = s
449450
self.lex = lex.lex(object=self)
450451
self.lex.input(self.input_data)
451452

@@ -454,17 +455,15 @@ def token(self):
454455

455456
def find_column(self, token):
456457
""" Compute column:
457-
- token is a token instance
458+
:param token: token instance
458459
"""
459460
i = token.lexpos
460461
while i > 0:
461462
if self.input_data[i - 1] == '\n':
462463
break
463464
i -= 1
464465

465-
column = token.lexpos - i + 1
466-
467-
return column
466+
return token.lexpos - i + 1
468467

469468

470469
# --------------------- PREPROCESSOR FUNCTIONS -------------------

src/zxbasm/memory.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import defaultdict
12
from typing import Dict, List, Optional, Tuple
23

34
from src.api import global_ as gl
@@ -23,6 +24,8 @@ def __init__(self, org: int = 0):
2324
self.memory_bytes: Dict[int, int] = {} # An array (associative) containing memory bytes
2425
self.local_labels: List[Dict[str, Label]] = [{}] # Local labels in the current memory scope
2526
self.global_labels = self.local_labels[0] # Global memory labels
27+
self.tmp_labels: Dict[int, Dict[str, Label]] = defaultdict(dict)
28+
self.tmp_labels_lines: List[int] = []
2629
self.ORG = org # last ORG value set
2730
self.scopes: List[int] = []
2831

@@ -181,7 +184,7 @@ def declare_label(
181184
value: int = None,
182185
local: bool = False,
183186
namespace: Optional[str] = None
184-
):
187+
) -> None:
185188
""" Sets a label with the given value or with the current address (org)
186189
if no value is passed.
187190
@@ -206,8 +209,6 @@ def declare_label(
206209

207210
self.set_memory_slot()
208211

209-
return self.local_labels[-1][ex_label]
210-
211212
def get_label(self, label: str, lineno: int) -> Label:
212213
""" Returns a label in the current context or in the global one.
213214
If the label does not exists, creates a new one and returns it.

src/zxbasm/zxbasm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from src.zxbpp import zxbpp
1919

2020
import src.api.config
21+
22+
from src.api import errmsg
2123
from src.api.config import OPTIONS
2224
from src.api import global_
2325

@@ -120,7 +122,7 @@ def main(args=None):
120122
return 1
121123

122124
if not asmparse.MEMORY.memory_bytes: # empty seq.
123-
asmparse.warning(0, "Nothing to assemble. Exiting...")
125+
errmsg.warning(0, "Nothing to assemble. Exiting...")
124126
return 0
125127

126128
current_org = max(asmparse.MEMORY.memory_bytes.keys() or [0]) + 1

0 commit comments

Comments
 (0)