Skip to content

Commit 0c65e75

Browse files
committed
refact: use Python's Enum for classes
1 parent d347c8f commit 0c65e75

3 files changed

Lines changed: 11 additions & 21 deletions

File tree

src/api/constants.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
os.path.abspath(os.path.dirname(os.path.abspath(__file__))), os.path.pardir)
2727
)
2828

29+
2930
# ----------------------------------------------------------------------
3031
# Class enums
3132
# ----------------------------------------------------------------------
3233

33-
34-
class CLASS:
34+
@enum.unique
35+
class CLASS(str, enum.Enum):
3536
""" Enums class constants
3637
"""
3738
unknown = 'unknown' # 0
@@ -41,35 +42,24 @@ class CLASS:
4142
label = 'label' # 4 Labels
4243
const = 'const' # 5 # constant literal value e.g. 5 or "AB"
4344
sub = 'sub' # 6 # subroutine
44-
type_ = 'type' # 7 # type
45-
46-
_CLASS_NAMES = {
47-
unknown: '(unknown)',
48-
var: 'var',
49-
array: 'array',
50-
function: 'function',
51-
label: 'label',
52-
const: 'const',
53-
sub: 'sub',
54-
type_: 'type'
55-
}
45+
type = 'type' # 7 # type
5646

5747
@classproperty
5848
def classes(cls):
5949
return (cls.unknown, cls.var, cls.array, cls.function, cls.sub,
6050
cls.const, cls.label)
6151

6252
@classmethod
63-
def is_valid(cls, class_):
53+
def is_valid(cls, class_: Union[str, 'CLASS']):
6454
""" Whether the given class is
6555
valid or not.
6656
"""
67-
return class_ in cls.classes
57+
return class_ in set(CLASS)
6858

6959
@classmethod
70-
def to_string(cls, class_):
60+
def to_string(cls, class_: 'CLASS'):
7161
assert cls.is_valid(class_)
72-
return cls._CLASS_NAMES[class_]
62+
return class_.value
7363

7464

7565
class ARRAY:

src/api/symboltable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def entry_size(var_entry: Union[SymbolVAR, SymbolVARARRAY]):
321321
offset = 0
322322

323323
for entry in entries: # Symbols of the current level
324-
if entry.class_ in (CLASS.function, CLASS.label, CLASS.type_):
324+
if entry.class_ in (CLASS.function, CLASS.label, CLASS.type):
325325
continue
326326

327327
# Local variables offset
@@ -832,7 +832,7 @@ def check_labels(self):
832832
""" Checks if all the labels has been declared
833833
"""
834834
for entry in self.labels:
835-
self.check_is_declared(entry.name, entry.lineno, CLASS.label)
835+
self.check_is_declared(entry.name, entry.lineno, CLASS.label.value)
836836

837837
def check_classes(self, scope=-1):
838838
""" Check if pending identifiers are defined or not. If not,

src/symbols/type_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, name: str, lineno: int, *children):
2929
self.lineno = lineno # The line the type was defined. Line 0 = basic type
3030
self.final = self # self.final always return the original aliased type (if this type is an alias)
3131
self.caseins = OPTIONS.case_insensitive # Whether this ID is case insensitive or not
32-
self.class_ = CLASS.type_
32+
self.class_ = CLASS.type
3333
self.accessed = False # Whether this type has been used or not
3434

3535
def __repr__(self):

0 commit comments

Comments
 (0)