Skip to content

Commit 25fc931

Browse files
committed
Refact internal error
Moved to the right place. Also add some more typing.
1 parent ed62ed2 commit 25fc931

3 files changed

Lines changed: 24 additions & 19 deletions

File tree

src/api/errors.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
'InvalidLoopError',
1919
'InvalidCONSTexpr',
2020
'InvalidBuiltinFunctionError',
21-
'InternalError'
21+
'InternalError',
22+
'TempAlreadyFreedError'
2223
]
2324

2425

@@ -52,6 +53,14 @@ def __init__(self, fname):
5253
self.msg = "Invalid BUILTIN function '%s'" % fname
5354

5455

55-
class InternalError:
56+
class InternalError(Error):
5657
def __init__(self, msg):
5758
self.msg = msg
59+
60+
61+
class TempAlreadyFreedError(InternalError):
62+
""" Raised when a TEMP label has been already freed.
63+
"""
64+
def __init__(self, label):
65+
super().__init__(f"Label '{label}' already freed")
66+
self.label = label

src/arch/zx48k/backend/__common.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
# vim ts=4:et:sw=4:ai
44

55
import math
6-
from .errors import TempAlreadyFreedError
6+
7+
from typing import List
8+
from typing import Set
9+
10+
import src.api.errors
11+
712

813
MEMORY = [] # Must be initialized by with init()
914

@@ -12,19 +17,19 @@
1217

1318
# Counter for generated tmp labels (__TMP0, __TMP1, __TMPN)
1419
TMP_COUNTER = 0
15-
TMP_STORAGES = []
20+
TMP_STORAGES: List[str] = []
1621

1722
# Set containing REQUIRED libraries
18-
REQUIRES = set() # Set of required libraries (included once)
23+
REQUIRES: Set[str] = set() # Set of required libraries (included once)
1924

2025
# Set containing automatic on start called routines
21-
INITS = set() # Set of INIT routines
26+
INITS: Set[str] = set() # Set of INIT routines
2227

2328
# CONSTANT LN(2)
2429
__LN2 = math.log(2)
2530

2631
# GENERATED labels __LABELXX
27-
TMP_LABELS = set()
32+
TMP_LABELS: Set[str] = set()
2833

2934

3035
def init():
@@ -83,9 +88,9 @@ def tmp_temp() -> str:
8388
return result
8489

8590

86-
def tmp_remove(label):
91+
def tmp_remove(label: str):
8792
if label not in TMP_STORAGES:
88-
raise TempAlreadyFreedError(label)
93+
raise src.api.errors.TempAlreadyFreedError(label)
8994

9095
TMP_STORAGES.pop(TMP_STORAGES.index(label))
9196

src/arch/zx48k/backend/errors.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
__all__ = ['GenericError',
88
'InvalidICError',
99
'NoMoreRegistersError',
10-
'UnsupportedError',
11-
'TempAlreadyFreedError']
10+
'UnsupportedError']
1211

1312

1413
class GenericError(Error):
@@ -52,14 +51,6 @@ def __init__(self, feat):
5251
self.feature = feat
5352

5453

55-
class TempAlreadyFreedError(GenericError):
56-
""" Raised when a TEMP label has been already freed.
57-
"""
58-
def __init__(self, label):
59-
GenericError.__init__(self, "Label '%s' already freed" % label)
60-
self.label = label
61-
62-
6354
# -----------------------------------------------------------------------------
6455
# Functions for throwing errors
6556
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)