|
17 | 17 | import re |
18 | 18 |
|
19 | 19 | from dataclasses import dataclass |
| 20 | +from enum import Enum, unique |
20 | 21 |
|
21 | 22 | from typing import Any |
22 | 23 | from typing import Dict |
|
45 | 46 | from src import arch |
46 | 47 |
|
47 | 48 |
|
| 49 | +@unique |
| 50 | +class PreprocMode(str, Enum): |
| 51 | + BASIC = "BASIC" |
| 52 | + ASM = "ASM" |
| 53 | + |
| 54 | + |
48 | 55 | # Generated output |
49 | 56 | OUTPUT = "" |
50 | 57 |
|
@@ -143,17 +150,19 @@ def set_include_path(): |
143 | 150 | INCLUDEPATH = [os.path.join(pwd, "library"), os.path.join(pwd, "library-asm")] |
144 | 151 |
|
145 | 152 |
|
146 | | -def setMode(mode: str) -> None: |
| 153 | +def setMode(mode: PreprocMode) -> None: |
147 | 154 | global LEXER |
148 | 155 |
|
149 | 156 | mode = mode.upper() |
150 | | - if mode not in ("ASM", "BASIC"): |
| 157 | + if mode not in list(PreprocMode): |
151 | 158 | raise PreprocError('Invalid mode "%s"' % mode, lineno=LEXER.lineno) |
152 | 159 |
|
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)] |
157 | 166 |
|
158 | 167 |
|
159 | 168 | def search_filename(fname: str, lineno: int, local_first: bool) -> str: |
@@ -862,7 +871,7 @@ def entry_point(args=None): |
862 | 871 |
|
863 | 872 | config.init() |
864 | 873 | init() |
865 | | - setMode("BASIC") |
| 874 | + setMode(PreprocMode.BASIC) |
866 | 875 |
|
867 | 876 | parser = argparse.ArgumentParser() |
868 | 877 | parser.add_argument( |
@@ -924,6 +933,10 @@ def entry_point(args=None): |
924 | 933 | config.OPTIONS.stderr_filename = options.stderr |
925 | 934 | config.OPTIONS.stderr = utils.open_file(config.OPTIONS.stderr_filename, "wt", "utf-8") |
926 | 935 |
|
| 936 | + _, ext = os.path.splitext(options.input_file) |
| 937 | + if ext.lower() == "asm": |
| 938 | + setMode(PreprocMode.ASM) |
| 939 | + |
927 | 940 | result = main([options.input_file] if options.input_file else []) |
928 | 941 | if not global_.has_errors: # ok? |
929 | 942 | if options.output_file: |
|
0 commit comments