-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmacro_preprocessor.py
More file actions
417 lines (339 loc) · 12.8 KB
/
Copy pathmacro_preprocessor.py
File metadata and controls
417 lines (339 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import doctest
import logging
import re
from pathlib import Path
from pprint import pprint
C64LIST_DIRECTIVES = [
'{include:',
'{uses:',
'{addsearchpath:',
'{buildrev:',
'{def:',
'{undef:',
'{ifdef:',
'{ifndef:',
'{else}',
'{endif}',
'{usedef:',
'{asm}',
'{endasm}',
'{$:',
'{%:',
'{info:',
'{warning:',
'{error:',
'{fatal:',
]
C64_CONSTANTS = {
# VIC-II
'BORDER': '$d020',
'BACKGROUND': '$d021',
'VIC_CTRL1': '$d011',
'VIC_CTRL2': '$d016',
'VIC_MEMCTRL': '$d018',
'RASTER': '$d012',
'SPRITE_EN': '$d015',
# SID
'SID_VOL': '$d418',
'SID_FREQ1_LO': '$d400',
'SID_FREQ1_HI': '$d401',
# CIA1
'CIA1_PRA': '$dc00',
'CIA1_PRB': '$dc01',
'CIA1_TOD_HR': '$dc08',
'CIA1_TOD_MIN': '$dc09',
'CIA1_TOD_SEC': '$dc0a',
'CIA1_TOD_10': '$dc0b',
# CIA2
'CIA2_PRA': '$dd00',
# Screen / color RAM
'SCREEN_RAM': '$0400',
'COLOR_RAM': '$d800',
# KERNAL
'CHROUT': '$ffd2',
'GETIN': '$ffe4',
}
def process_constants(code: list,
existing: dict = None) -> tuple[dict, list]:
"""
Processes constant definitions and returns them as a dictionary,
along with the remaining non-constant code.
>>> code = ['{const: BORDER $d020}',
... '{const: BACKGROUND $d021}',
... 'lda #5',
... 'sta BORDER']
>>> process_constants(code)
({'BORDER': '$d020', 'BACKGROUND': '$d021'}, ['lda #5', 'sta BORDER'])
>>> process_constants(['{const: BORDER $d020}', '{const: BORDER $d021}'])
Traceback (most recent call last):
...
SyntaxError: Line 2: Duplicate constant 'BORDER'
>>> process_constants(['{const: SCREEN_RAM $9000}'], existing=C64_CONSTANTS)
Traceback (most recent call last):
...
SyntaxError: Line 1: Cannot redefine built-in constant 'SCREEN_RAM'
"""
if existing is None:
existing = {}
constants = {}
non_const_code = []
for line_num, line in enumerate(code, start=1):
if line.startswith('{const: '):
inner = strip_surrounding(line, '{const: ', '}')
parts = inner.split()
if len(parts) != 2:
raise SyntaxError(f"Line {line_num}: "
f"Expected '{{const: NAME value}}', "
f"got '{line}'")
name, value = parts
if name in existing:
raise SyntaxError(f"Line {line_num}: "
f"Cannot redefine built-in constant '{name}'")
if name in constants:
raise SyntaxError(f"Line {line_num}: "
f"Duplicate constant '{name}'")
constants[name] = value
logging.info(f"Defined constant: {name} = {value}")
else:
non_const_code.append(line)
return constants, non_const_code
def expand_constants(code: list, constants: dict) -> list:
"""
Replaces constant names in code lines with their values.
>>> constants = {'BORDER': '$d020', 'BACKGROUND': '$d021'}
>>> code = ['lda #5', 'sta BORDER', 'sta BACKGROUND']
>>> expand_constants(code, constants)
['lda #5', 'sta $d020', 'sta $d021']
"""
result = []
for line in code:
expanded = line
for name, value in constants.items():
expanded = re.sub(rf'\b{name}\b', value, expanded)
result.append(expanded)
return result
def is_c64list_directive(line: str) -> bool:
"""
Returns True if the line is a C64List native directive
that should be passed through to C64List unchanged.
>>> is_c64list_directive('{include:somefile.asm}')
True
>>> is_c64list_directive('{def:macro border @1}')
False
>>> is_c64list_directive('{ifdef:debug}')
True
>>> is_c64list_directive('lda #$d020')
False
>>> is_c64list_directive('{const: BORDER $d020}')
False
"""
stripped = line.strip()
if stripped.startswith('{def:macro '):
return False
for directive in C64LIST_DIRECTIVES:
if stripped.startswith(directive):
return True
return False
def process_macros(code: list):
"""
Processes macro definitions and stores them in a dictionary.
:param code: A list of lines containing code and macro definitions.
:returns: A dictionary of macros, keyed by name and parameters.
:returns: A list of non-macro code lines.
>>> code = ['{def:macro border @1}',
... 'lda @1',
... 'sta $d020',
... '{endmacro}',
... '{macro: border #5}',
... 'sta $d021']
>>> process_macros(code=code)
({'border @1': ['lda @1', 'sta $d020']}, ['{macro: border #5}', 'sta $d021'])
"""
macros = {}
non_macro_code = []
current_macro = None
for line_num, line in enumerate(code, start=1):
logging.info(f"{line_num:3} {line}")
if line.startswith('{def:macro '):
macro_body = []
logging.info(f'found macro definition: {line=}')
macro_definition = strip_surrounding(line, left="{def:macro ", right="}")
current_macro = macro_definition
logging.info(f'after: {macro_definition=}')
params = [param for param in macro_definition.split()[1:]
if param.startswith("@") and param[1].isdigit()]
logging.info(f"{params=}. {len(params)=}")
for next_line in code[line_num:]:
if next_line.strip() != '{endmacro}':
macro_body.append(next_line)
logging.info(f'{macro_body=}')
else:
logging.info(f"Found 'endmacro'")
macros[macro_definition] = macro_body
current_macro = None
break
if current_macro:
raise SyntaxError(f"Line {line_num}: Unterminated macro definition '{current_macro}'")
elif line == '{endmacro}':
pass # already consumed by inner loop, skip it
elif any(line == body_line
for macro_body in macros.values()
for body_line in macro_body):
pass # skip macro body lines
else:
# regular code line or C64List directive - pass through
if is_c64list_directive(line):
logging.info(f"Passing C64List directive through: {line}")
non_macro_code.append(line)
return macros, non_macro_code
def strip_surrounding(line: str, left: str, right: str) -> str:
"""
Strips exact substrings from left and right of a string.
>>> strip_surrounding("{def:macro blah @1}", "{def:macro ", "}")
'blah @1'
>>> strip_surrounding("{const: BORDER $d020}", "{const: ", "}")
'BORDER $d020'
"""
if line.startswith(left):
line = line[len(left):]
if line.endswith(right):
line = line[:-len(right)]
return line
def parse_macro_call(line: str) -> tuple[str, list[str]]:
"""
Parses a macro call line into a name and argument list.
>>> parse_macro_call("{macro: border #1}")
('border', ['#1'])
>>> parse_macro_call("{macro: nested_loop #$de #$ad}")
('nested_loop', ['#$de', '#$ad'])
"""
inner = strip_surrounding(line, "{macro: ", "}")
parts = inner.split()
name = parts[0]
args = parts[1:]
return name, args
def substitute_params(macro_body: list[str], args: list[str]) -> list[str]:
"""
Replaces @1, @2 etc. in macro body lines with actual arguments.
>>> substitute_params(['lda @1', 'sta $d020'], ['#5'])
['lda #5', 'sta $d020']
>>> substitute_params(['ldx @2', 'ldy @1', 'dey', 'bne *-2'], ['#$de', '#$ad'])
['ldx #$ad', 'ldy #$de', 'dey', 'bne *-2']
"""
result = []
for line in macro_body:
expanded = line
for i, arg in enumerate(args, start=1):
expanded = expanded.replace(f"@{i}", arg)
result.append(expanded)
return result
def expand_macros(non_macro_code: list, macros: dict) -> list:
"""
Replaces macro calls in code with their expanded definitions.
:param non_macro_code: list of lines of code
:param macros: A dictionary containing defined macros.
:return: A new list with macros replaced.
>>> macros = {'nested_loop @1 @2': ['ldx @2', 'ldy @1', 'dey', 'bne *-2']}
>>> expand_macros(['{macro: nested_loop #$de}'], macros)
Traceback (most recent call last):
...
SyntaxError: Line 1: Macro 'nested_loop' expects 2 argument(s), but got 1
>>> expand_macros(['{macro: unknown #1}'], {})
Traceback (most recent call last):
...
SyntaxError: Line 1: Unknown macro 'unknown'
"""
new_lines = []
for line_num, code_line in enumerate(non_macro_code, start=1):
if code_line.startswith("{macro: "):
name, args = parse_macro_call(code_line)
matching_keys = [key for key in macros if key.split()[0] == name]
if not matching_keys:
raise SyntaxError(f"Line {line_num}: Unknown macro '{name}'")
key = name + "".join(f" @{i + 1}" for i in range(len(args)))
if key not in macros:
expected_key = matching_keys[0]
expected_count = len(expected_key.split()) - 1
raise SyntaxError(f"Line {line_num}: Macro '{name}' expects "
f"{expected_count} argument(s), "
f"but got {len(args)}")
expanded = substitute_params(macros[key], args)
new_lines.extend(expanded)
else:
new_lines.append(code_line)
logging.debug("Code after macro expansion:")
for line_num, line in enumerate(new_lines, start=1):
logging.debug(f"{line_num:3}: {line}")
return new_lines
def preprocess_file(input_path: str) -> str:
"""
Reads an .asm file, runs it through the full preprocessing pipeline,
and returns the output file path.
:param input_path: Path to the input .asm file
:return: Path to the preprocessed output file
"""
input_file = Path(input_path)
output_file = input_file.with_stem(input_file.stem + '_pp')
logging.info(f"Reading: {input_file}")
code = input_file.read_text().splitlines()
logging.info(f"Read {len(code)} lines")
# Step 1: process and expand constants
constants, code_after_consts = process_constants(code,
existing=C64_CONSTANTS)
all_constants = {**C64_CONSTANTS, **constants}
logging.debug(f"Constants defined: {len(all_constants)}")
# Step 2: expand constants throughout remaining code
code_with_consts = expand_constants(code_after_consts, all_constants)
# Step 3: process and expand macros
macros, non_macro_code = process_macros(code_with_consts)
logging.debug(f"Macros defined: {len(macros)}")
# Step 4: expand macro calls
result = expand_macros(non_macro_code, macros)
logging.info(f"Writing: {output_file}")
output_file.write_text('\n'.join(result) + '\n')
logging.info(f"Wrote {len(result)} lines")
return str(output_file)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description='Preprocessor for C64List assembly files. '
'Handles {const:} and {def:macro} directives '
'before passing to C64List.'
)
parser.add_argument('input',
nargs='?',
help='Input .asm file to preprocess')
parser.add_argument('--verbose', '-v',
action='store_true',
help='Enable verbose logging')
parser.add_argument('--debug', '-d',
action='store_true',
help='Enable debug logging')
parser.add_argument('--test', '-t',
action='store_true',
help='Run doctests instead of processing a file')
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
elif args.verbose:
logging.basicConfig(level=logging.INFO)
else:
logging.basicConfig(level=logging.WARNING)
if args.test:
results = doctest.testmod(verbose=True)
raise SystemExit(0 if results.failed == 0 else 1)
if not args.input:
parser.print_help()
raise SystemExit(1)
input_file = Path(args.input)
if not input_file.exists():
print(f"Error: File not found: {input_file}")
raise SystemExit(1)
if input_file.suffix != '.asm':
print(f"Warning: Input file does not have .asm extension: {input_file}")
try:
output_path = preprocess_file(args.input)
print(f"Preprocessed: {input_file} -> {output_path}")
except SyntaxError as e:
print(f"Error: {e}")
raise SystemExit(1)