Skip to content

Commit dde2f8f

Browse files
authored
Merge pull request #1048 from boriel-basic/feat/add_output_format_param_to_zxbasm
feat: make zxbasm use new format file
2 parents c9a2162 + 81e1e33 commit dde2f8f

2 files changed

Lines changed: 48 additions & 13 deletions

File tree

src/zxbasm/zxbasm.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@
1010
import argparse
1111
import os
1212
import sys
13+
from enum import StrEnum
1314

1415
import src.api.config
1516
from src.api import errmsg, global_
1617
from src.api.config import OPTIONS
18+
from src.api.errmsg import warning_command_line_flag_deprecation
1719
from src.zxbasm import asmparse, expr
1820
from src.zxbasm.version import VERSION
1921
from src.zxbpp import zxbpp
22+
from src.zxbpp.zxbpp import PreprocMode
23+
24+
25+
class FileType(StrEnum):
26+
BIN = "bin"
27+
SNA = "sna"
28+
TAP = "tap"
29+
TZX = "tzx"
30+
Z80 = "z80"
2031

2132

2233
def main(args=None):
@@ -50,7 +61,8 @@ def main(args=None):
5061
default=None,
5162
)
5263

53-
o_parser.add_argument(
64+
output_file_type_group = o_parser.add_mutually_exclusive_group()
65+
output_file_type_group.add_argument(
5466
"-T",
5567
"--tzx",
5668
action="store_true",
@@ -59,7 +71,7 @@ def main(args=None):
5971
help="Sets output format to tzx (default is .bin)",
6072
)
6173

62-
o_parser.add_argument(
74+
output_file_type_group.add_argument(
6375
"-t",
6476
"--tap",
6577
action="store_true",
@@ -68,6 +80,16 @@ def main(args=None):
6880
help="Sets output format to tzx (default is .bin)",
6981
)
7082

83+
output_file_type_group.add_argument(
84+
"-f",
85+
"--output-format",
86+
type=str,
87+
choices=[str(x) for x in FileType],
88+
required=False,
89+
help="Output format",
90+
default=OPTIONS.output_file_type,
91+
)
92+
7193
o_parser.add_argument(
7294
"-B",
7395
"--BASIC",
@@ -114,7 +136,6 @@ def main(args=None):
114136

115137
if not os.path.exists(options.PROGRAM):
116138
o_parser.error("No such file or directory: '%s'" % options.PROGRAM)
117-
sys.exit(2)
118139

119140
OPTIONS.debug_level = int(options.debug)
120141
OPTIONS.input_filename = options.PROGRAM
@@ -127,10 +148,18 @@ def main(args=None):
127148
OPTIONS.force_asm_brackets = options.bracket
128149
OPTIONS.zxnext = options.zxnext
129150

130-
if options.tzx:
131-
OPTIONS.output_file_type = "tzx"
151+
if options.output_format:
152+
OPTIONS.output_file_type = options.output_format
153+
elif options.tzx:
154+
OPTIONS.output_file_type = FileType.TZX
155+
warning_command_line_flag_deprecation(
156+
f"--tzx (use -f {FileType.TZX} or --output-format={FileType.TZX} instead)"
157+
)
132158
elif options.tap:
133-
OPTIONS.output_file_type = "tap"
159+
OPTIONS.output_file_type = FileType.TAP
160+
warning_command_line_flag_deprecation(
161+
f"--tap (use -f {FileType.TAP} or --output-format={FileType.TAP} instead)"
162+
)
134163

135164
if not OPTIONS.output_filename:
136165
OPTIONS.output_filename = (
@@ -149,7 +178,7 @@ def main(args=None):
149178
return 4
150179

151180
# Configure the preprocessor to use the asm-preprocessor-lexer
152-
zxbpp.setMode("asm")
181+
zxbpp.setMode(PreprocMode.ASM)
153182

154183
# Now filter them against the preprocessor
155184
zxbpp.main([OPTIONS.input_filename])

src/zxbpp/zxbpp.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
from enum import Enum, unique
1616
from typing import Any, Final, NamedTuple
1717

18-
from src import arch
1918
from src.api import config, global_, utils
20-
from src.arch import AVAILABLE_ARCHITECTURES
2119
from src.ply import yacc
2220
from src.zxbpp import zxbasmpplex, zxbpplex
2321
from src.zxbpp.base_pplex import STDIN
@@ -35,6 +33,9 @@ class PreprocMode(str, Enum):
3533
ASM = "ASM"
3634

3735

36+
# List of available architectures
37+
AVAILABLE_ARCHITECTURES: Final[list[str]] = []
38+
3839
# Generated output
3940
OUTPUT = ""
4041

@@ -124,6 +125,11 @@ def init():
124125
global ENABLED
125126
global IFDEFS
126127

128+
from src import arch
129+
130+
AVAILABLE_ARCHITECTURES.clear()
131+
AVAILABLE_ARCHITECTURES.extend(arch.AVAILABLE_ARCHITECTURES)
132+
127133
config.OPTIONS(config.Action.ADD_IF_NOT_DEFINED, name="debug_zxbpp", type=bool, default=False)
128134
global_.FILENAME = STDIN
129135
OUTPUT = ""
@@ -1014,9 +1020,9 @@ def entry_point(args=None):
10141020
parser.add_argument(
10151021
"--arch",
10161022
type=str,
1017-
default=arch.AVAILABLE_ARCHITECTURES[0],
1018-
help=f"Target architecture (defaults is'{arch.AVAILABLE_ARCHITECTURES[0]}'). "
1019-
f"Available architectures: {','.join(arch.AVAILABLE_ARCHITECTURES)}",
1023+
default=AVAILABLE_ARCHITECTURES[0],
1024+
help=f"Target architecture (defaults is'{AVAILABLE_ARCHITECTURES[0]}'). "
1025+
f"Available architectures: {','.join(AVAILABLE_ARCHITECTURES)}",
10201026
)
10211027
parser.add_argument(
10221028
"--expect-warnings",
@@ -1030,7 +1036,7 @@ def entry_point(args=None):
10301036
config.OPTIONS.debug_zxbpp = config.OPTIONS.debug_level > 0
10311037
config.OPTIONS.expected_warnings = options.expect_warnings
10321038

1033-
if options.arch not in arch.AVAILABLE_ARCHITECTURES:
1039+
if options.arch not in AVAILABLE_ARCHITECTURES:
10341040
parser.error(f"Invalid architecture '{options.arch}'") # Exits with error
10351041

10361042
config.OPTIONS.architecture = options.arch

0 commit comments

Comments
 (0)