Skip to content

Commit 39e7c0d

Browse files
committed
refact: use Enums for lexer modes in zxbpp
1 parent 9560f4a commit 39e7c0d

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

src/zxbpp/zxbpp.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import re
1818

1919
from dataclasses import dataclass
20+
from enum import Enum, unique
2021

2122
from typing import Any
2223
from typing import Dict
@@ -45,6 +46,12 @@
4546
from src import arch
4647

4748

49+
@unique
50+
class PreprocMode(str, Enum):
51+
BASIC = "BASIC"
52+
ASM = "ASM"
53+
54+
4855
# Generated output
4956
OUTPUT = ""
5057

@@ -143,17 +150,19 @@ def set_include_path():
143150
INCLUDEPATH = [os.path.join(pwd, "library"), os.path.join(pwd, "library-asm")]
144151

145152

146-
def setMode(mode: str) -> None:
153+
def setMode(mode: PreprocMode) -> None:
147154
global LEXER
148155

149156
mode = mode.upper()
150-
if mode not in ("ASM", "BASIC"):
157+
if mode not in list(PreprocMode):
151158
raise PreprocError('Invalid mode "%s"' % mode, lineno=LEXER.lineno)
152159

153-
if mode == "ASM":
154-
LEXER = zxbasmpplex.Lexer(defines_table=ID_TABLE)
155-
else:
156-
LEXER = zxbpplex.Lexer(defines_table=ID_TABLE)
160+
lexers = {
161+
PreprocMode.ASM: zxbasmpplex.Lexer(defines_table=ID_TABLE),
162+
PreprocMode.BASIC: zxbpplex.Lexer(defines_table=ID_TABLE),
163+
}
164+
165+
LEXER = lexers[PreprocMode(mode)]
157166

158167

159168
def search_filename(fname: str, lineno: int, local_first: bool) -> str:
@@ -862,7 +871,7 @@ def entry_point(args=None):
862871

863872
config.init()
864873
init()
865-
setMode("BASIC")
874+
setMode(PreprocMode.BASIC)
866875

867876
parser = argparse.ArgumentParser()
868877
parser.add_argument(
@@ -924,6 +933,10 @@ def entry_point(args=None):
924933
config.OPTIONS.stderr_filename = options.stderr
925934
config.OPTIONS.stderr = utils.open_file(config.OPTIONS.stderr_filename, "wt", "utf-8")
926935

936+
_, ext = os.path.splitext(options.input_file)
937+
if ext.lower() == "asm":
938+
setMode(PreprocMode.ASM)
939+
927940
result = main([options.input_file] if options.input_file else [])
928941
if not global_.has_errors: # ok?
929942
if options.output_file:

0 commit comments

Comments
 (0)