Skip to content

Commit 7679dff

Browse files
authored
Merge pull request #548 from boriel/refact/code_refact
Refact/code refact
2 parents 351b76a + 0c65e75 commit 7679dff

5 files changed

Lines changed: 46 additions & 80 deletions

File tree

src/api/constants.py

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import enum
1313
import os
1414

15-
from typing import Optional
15+
from typing import Optional, Union
1616

1717
from .decorator import classproperty
1818

@@ -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:
@@ -179,83 +169,59 @@ def to_type(typename: str) -> Optional['TYPE']:
179169
return None
180170

181171

182-
class SCOPE:
172+
@enum.unique
173+
class SCOPE(str, enum.Enum):
183174
""" Enum scopes
184175
"""
185-
unknown = None
186176
global_ = 'global'
187177
local = 'local'
188178
parameter = 'parameter'
189179

190-
_names = {
191-
unknown: 'unknown',
192-
global_: 'global',
193-
local: 'local',
194-
parameter: 'parameter'
195-
}
196-
197-
@classmethod
198-
def is_valid(cls, scope):
199-
return cls._names.get(scope, None) is not None
180+
@staticmethod
181+
def is_valid(scope: Union[str, 'SCOPE']):
182+
return scope in set(SCOPE)
200183

201-
@classmethod
202-
def to_string(cls, scope):
203-
assert cls.is_valid(scope)
204-
return cls._names[scope]
184+
@staticmethod
185+
def to_string(scope: 'SCOPE'):
186+
assert SCOPE.is_valid(scope)
187+
return scope.value
205188

206189

207-
class KIND:
190+
@enum.unique
191+
class KIND(str, enum.Enum):
208192
""" Enum kind
209193
"""
210-
unknown = None
194+
unknown = 'unknown'
211195
var = 'var'
212196
function = 'function'
213197
sub = 'sub'
214-
type_ = 'type'
215-
216-
_NAMES = {
217-
unknown: '(unknown)',
218-
var: 'variable',
219-
function: 'function',
220-
sub: 'subroutine',
221-
type_: 'type'
222-
}
198+
type = 'type'
223199

224-
@classmethod
225-
def is_valid(cls, kind):
226-
return cls._NAMES.get(kind, None) is not None
200+
@staticmethod
201+
def is_valid(kind: Union[str, 'KIND']):
202+
return kind in set(KIND)
227203

228-
@classmethod
229-
def to_string(cls, kind):
230-
assert cls.is_valid(kind)
231-
return cls._NAMES.get(kind)
204+
@staticmethod
205+
def to_string(kind: 'KIND'):
206+
assert KIND.is_valid(kind)
207+
return kind.value
232208

233209

234-
class CONVENTION:
235-
unknown = None
210+
@enum.unique
211+
class CONVENTION(str, enum.Enum):
212+
unknown = 'unknown'
236213
fastcall = '__fastcall__'
237214
stdcall = '__stdcall__'
238215

239-
_NAMES = {
240-
unknown: '(unknown)',
241-
fastcall: '__fastcall__',
242-
stdcall: '__stdcall__'
243-
}
244-
245-
@classmethod
246-
def is_valid(cls, convention):
247-
return cls._NAMES.get(convention, None) is not None
248-
249-
@classmethod
250-
def to_string(cls, convention):
251-
assert cls.is_valid(convention)
252-
return cls._NAMES[convention]
216+
@staticmethod
217+
def is_valid(convention: Union[str, 'CONVENTION']):
218+
return convention in set(CONVENTION)
253219

220+
@staticmethod
221+
def to_string(convention: 'CONVENTION'):
222+
assert CONVENTION.is_valid(convention)
223+
return convention.value
254224

255-
# ----------------------------------------------------------------------
256-
# Identifier Class (variable, function, label, array)
257-
# ----------------------------------------------------------------------
258-
ID_CLASSES = CLASS.classes
259225

260226
# ----------------------------------------------------------------------
261227
# Deprecated suffixes for variable names, such as "a$"

src/api/errmsg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def syntax_error_cant_convert_to_type(lineno: int, expr_str: str, type_: str):
270270
# Syntax error: is a SUB not a FUNCTION
271271
# ----------------------------------------
272272
def syntax_error_is_a_sub_not_a_func(lineno: int, name: str):
273-
error(lineno, "'%s' is SUBROUTINE not a FUNCTION" % name)
273+
error(lineno, "'%s' is a SUB not a FUNCTION" % name)
274274

275275

276276
# ----------------------------------------

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):

tests/functional/test_errmsg.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ doloop2.bas:4: warning: Variable 'a' is never used
1515
dowhile1.bas:1: warning: Condition is always True
1616
dowhile1.bas:1: warning: Empty loop
1717
>>> process_file('subcall1.bas')
18-
subcall1.bas:6: error: 'test' is SUBROUTINE not a FUNCTION
18+
subcall1.bas:6: error: 'test' is a SUB not a FUNCTION
1919
>>> process_file('subcall2.bas')
20-
subcall2.bas:6: error: 'test' is a SUBROUTINE, not a FUNCTION
20+
subcall2.bas:6: error: 'test' is a SUB, not a FUNCTION
2121
>>> process_file('prepro05.bi')
2222
prepro05.bi:2: warning: [W510] "test" redefined (previous definition at prepro05.bi:2)
2323
>>> process_file('prepro07.bi')
@@ -86,7 +86,7 @@ strict.bas:4: error: strict mode: missing type declaration for 'a'
8686
>>> process_file('errletfunc.bas')
8787
errletfunc.bas:5: error: Cannot assign a value to 'x'. It's not a variable
8888
>>> process_file('read0.bas')
89-
read0.bas:12: error: 'x' is SUBROUTINE not a FUNCTION
89+
read0.bas:12: error: 'x' is a SUB not a FUNCTION
9090
>>> process_file('read1.bas')
9191
read1.bas:11: error: Cannot read 'x'. It's an array
9292
>>> process_file('read3.bas')

0 commit comments

Comments
 (0)