-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmac-gen.py
452 lines (340 loc) · 11.7 KB
/
mac-gen.py
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import sublime
import sublime_plugin
import os
import re
import sys
import time
def each_line(file):
f = open(file, "r", encoding="ascii")
try:
while True:
x = f.readline()
if not x: break
x = x.rstrip("\r\n")
yield x
except Exception as e:
raise e
finally:
f.close()
def macro_files(dirs, filter):
rv = []
for root in dirs:
try:
tmp = [x for x in os.listdir(root) if filter(x)]
tmp.sort(key=str.upper)
tmp = [os.path.join(root, x) for x in tmp]
tmp = [x for x in tmp if os.path.isfile(x)]
rv += tmp
except Exception as e:
pass
return rv
re_pmc = re.compile('^[^.,/\\- ]*')
re_dot_s = re.compile('\\.S$', re.IGNORECASE)
re_ws = re.compile('[\t ]+')
re_comment = re.compile('^[\t ]*[;*]')
class CommonMacGen(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
missing = set()
known = set()
self.build_missing(known, missing)
if len(missing) == 0: return
buffer = ""
files = self.build_file_list()
for f in files:
buffer += self.one_macro_file(f, known, missing)
if len(missing) == 0: break;
buffer = self.front_matter() + self.missing_matter(missing) + buffer
self.create_macro_file(buffer)
def front_matter(self):
buffer = ""
name = self.view.file_name()
if name:
buffer += "* Generated from " + os.path.basename(name) + " on " + time.asctime() + "\n"
else:
buffer += "* Generated on " + time.asctime() + "\n"
buffer += "\n"
return buffer
def missing_matter(self, missing):
buffer = ""
if len(missing) == 0:
return ""
buffer = "* Missing Macros\n"
missing = list(missing)
missing.sort
for macro in missing:
buffer += "* " + macro + "\n"
buffer += "\n"
return buffer
def create_macro_file(self, buffer):
view = self.view
mv = view.window().new_file()
mv.settings().set("auto_indent", False)
mv.run_command('insert', {'characters': buffer})
# override these:
def build_missing(self, known, missing):
pass
def build_file_list(self):
return []
def one_macro_file(self, file, known, missing):
return ""
class MerlinMacGen(CommonMacGen):
opcodes = {
'BRL', 'COP', 'JML', 'JMPL', 'JSL', 'MVN', 'MVP', 'MX', 'PEA',
'PEI', 'PER', 'REP', 'SEP', 'WDM', 'XCE', 'ADR', 'ADRL', 'ASC',
'AST', 'BRK', 'CHK', 'CYC', 'DA', 'DAT', 'DB', 'DCI', 'DDB', 'DEND',
'DFB', 'DL', 'DS', 'DSK', 'DUM', 'DW', 'END', 'ENT', 'EQU', 'ERR', 'EXP',
'EXT', 'FLS', 'HEX', 'INV', 'KBD', 'LST', 'LSTL', 'LUP', 'OBJ',
'ORG', 'PAG', 'PAU', 'PMC', 'PUT', 'REL', 'REV', 'SAV', 'SKP',
'STR', 'TR', 'TTL', 'TYP', 'USE', 'USR', 'VAR', 'XC', 'XREF','=',
'>>>', 'DO', 'ELS', 'EOM', 'FIN', 'IF', 'MAC', '--^', '<<<',
'CLC', 'CLD', 'CLI', 'CLV', 'DEX', 'DEY', 'INX', 'INY', 'NOP',
'PHA', 'PHB', 'PHD', 'PHK', 'PHX', 'PHY', 'PLA', 'PLB', 'PLD',
'PLP', 'PLX', 'PLY', 'RTI', 'RTL', 'RTS', 'SEC', 'SED', 'SEI',
'STP', 'SWA', 'TAD', 'TAS', 'TAX', 'TAY', 'TCD', 'TCS', 'TDA',
'TDC', 'TSA', 'TSC', 'TSX', 'TXA', 'TXS', 'TXY', 'TYA', 'TYX',
'WAI', 'XBA', 'BCC', 'BCS', 'BEQ', 'BGE', 'BLT', 'BMI', 'BNE',
'BPL', 'BRA', 'BVC', 'BVS', 'ADCL', 'ANDL', 'CMPL', 'EORL',
'LDAL', 'ORAL', 'SBCL', 'STAL', 'ADC', 'AND', 'ASL', 'BIT',
'CMP', 'CPX', 'CPY', 'DEC', 'EOR', 'INC', 'JMP', 'JSR', 'LDA',
'LDX', 'LDY', 'LSR', 'ORA', 'ROL', 'ROR', 'SBC', 'STA', 'STX',
'STY', 'STZ', 'TRB', 'TSB', 'PHP', 'STRL', 'FLO', 'EXD', 'CAS',
'ELSE', # processed as ELS
''
}
def build_file_list(self):
dirlist = self.view.settings().get('merlin_macro_directories', [])
files = macro_files(dirlist, lambda x: x[-2:].upper() == ".S")
return files
def build_missing(self, known, missing):
view = self.view
all = sublime.Region(0, view.size())
for r in view.lines(all):
text = view.substr(r).rstrip()
if text == "" or re_comment.match(text):
continue
tokens = re.split(re_ws, text, maxsplit=2)
if len(tokens) < 2:
continue
# _macro args
# PMC _macro,args << . / , - ( Space are separators.
# >>> _macro
label = tokens[0].upper()
opcode = tokens[1].upper()
if opcode == "" or opcode[0] == ";":
continue
if opcode == 'MAC':
# label MAC
if label != "":
known.add(label)
continue
if opcode in {'PMC', '>>>'} and len(tokens) > 2:
tmp = tokens[2].upper()
opcode = re_pmc.match(tmp).group(0)
if opcode == "":
continue
if opcode in self.opcodes:
continue
if opcode in known:
continue
missing.add(opcode)
def one_macro_file(self, file, known, missing):
inmac = False
buffer = ""
for text in each_line(file):
text = text.rstrip()
if inmac:
buffer += text + "\n"
if text == "": continue
if re_comment.match(text): continue
tokens = re.split(re_ws, text, maxsplit=2)
if len(tokens) < 2: continue
label = tokens[0].upper()
opcode = tokens[1].upper()
if opcode == "" or opcode[0] == ";": continue
if opcode == "MAC" and label != "":
# print(label,file=sys.stderr)
# print(label in missing, file=sys.stderr)
if inmac:
known.add(label)
missing.discard(label)
elif label in missing:
inmac = True
known.add(label)
missing.discard(label)
buffer += text + "\n"
continue
if opcode in { '<<<', 'EOM' }:
inmac = False
continue
if not inmac: continue
if opcode in {'PMC', '>>>'} and len(tokens) > 2:
tmp = tokens[2].upper()
opcode = re_pmc.match(tmp).group(0)
if opcode == "":
continue
if opcode in self.opcodes: continue
if opcode in known: continue
missing.add(opcode)
return buffer
class OrcaMacGen(CommonMacGen):
opcodes = {
'ADC', 'AND', 'CMP', 'EOR', 'LDA', 'ORA', 'SBC', 'ASL', 'LSR',
'ROR', 'ROL', 'BIT', 'CPX', 'CPY', 'DEC', 'INC', 'LDX', 'LDY',
'STA', 'STX', 'STY', 'JMP', 'JSR', 'STZ', 'TRB', 'TSB', 'JSL',
'JML', 'COP', 'MVN', 'MVP', 'PEA', 'PEI', 'REP', 'SEP', 'PER',
'BRL', 'BRA', 'BEQ', 'BMI', 'BNE', 'BPL', 'BVC', 'BVS', 'BCC',
'BCS', 'CLI', 'CLV', 'DEX', 'DEY', 'INX', 'INY', 'NOP', 'PHA',
'PLA', 'PHP', 'PLP', 'RTI', 'RTS', 'SEC', 'SED', 'SEI', 'TAX',
'TAY', 'TSX', 'TXA', 'TXS', 'TYA', 'BRK', 'CLC', 'CLD', 'PHX',
'PHY', 'PLX', 'PLY', 'DEA', 'INA', 'PHB', 'PHD', 'PHK', 'PLB',
'PLD', 'WDM', 'RTL', 'STP', 'TCD', 'TCS', 'TDC', 'TSC', 'TXY',
'TYX', 'WAI', 'XBA', 'XCE', 'CPA', 'BLT', 'BGE', 'GBLA', 'GBLB',
'GBLC', 'LCLA', 'LCLB', 'LCLC', 'SETA', 'SETB', 'SETC', 'AMID',
'ASEARCH', 'AINPUT', 'AIF', 'AGO', 'ACTR', 'MNOTE', 'ANOP', 'DS',
'ORG', 'OBJ', 'EQU', 'GEQU', 'MERR', 'DIRECT', 'KIND', 'SETCOM',
'EJECT', 'ERR', 'GEN', 'MSB', 'LIST', 'SYMBOL', 'PRINTER',
'65C02', '65816', 'LONGA', 'LONGI', 'DATACHK', 'CODECHK',
'DYNCHK', 'IEEE', 'NUMSEX', 'CASE', 'OBJCASE', 'ABSADDR',
'INSTIME', 'TRACE', 'EXPAND', 'DC', 'USING', 'ENTRY', 'OBJEND',
'DATA', 'PRIVDATA', 'END', 'ALIGN', 'START', 'PRIVATE', 'MEM',
'TITLE', 'RENAME', 'KEEP', 'COPY', 'APPEND', 'MCOPY', 'MDROP',
'MLOAD', 'MACRO', 'MEXIT', 'MEND'
}
def build_file_list(self):
dirlist = self.view.settings().get('orca_macro_directories', [])
files = macro_files(dirlist, lambda x: x[:4].upper() == "M16.")
return files
def build_missing(self, known, missing):
view = self.view
all = sublime.Region(0, view.size())
for r in view.lines(all):
text = view.substr(r).rstrip()
if text == "": continue
if text[0] in "!*": continue
if re_comment.match(text): continue
tokens = re.split(re_ws, text, maxsplit=2)
if len(tokens) < 2: continue
label = tokens[0].upper()
opcode = tokens[1].upper()
if opcode in self.opcodes: continue
missing.add(opcode)
def one_macro_file(self, file, known, missing):
buffer = ""
state = 0
for text in each_line(file):
if state == 2:
buffer += text + "\n"
if text == "": continue
if re_comment.match(text): continue
tokens = re.split(re_ws, text, maxsplit=2)
if len(tokens) < 2: continue
label = tokens[0].upper()
opcode = tokens[1].upper()
if state == 0:
if opcode == 'MACRO': state = 1
elif state == 1:
if opcode in missing:
known.add(opcode)
missing.discard(opcode)
buffer += "\tMACRO\n"
buffer += text + "\n"
state = 2
else:
state = 0
elif state == 2:
if opcode == 'MEND': state = 0
if opcode not in self.opcodes and opcode not in known:
missing.add(opcode)
return buffer
class MpwMacGen(CommonMacGen):
opcodes = {
'ACTR', 'ADC', 'AERROR', 'ALIGN', 'AND', 'ANOP', 'ASL',
'BBC0', 'BBC1', 'BBC2', 'BBC3', 'BBC4', 'BBC5', 'BBC6', 'BBC7',
'BBR0', 'BBR1', 'BBR2', 'BBR3', 'BBR4', 'BBR5', 'BBR6', 'BBR7',
'BBS0', 'BBS1', 'BBS2', 'BBS3', 'BBS4', 'BBS5', 'BBS6', 'BBS7',
'BCC', 'BCS', 'BEQ', 'BGE', 'BIT', 'BLANKS', 'BLT', 'BMI', 'BNE',
'BPL', 'BRA', 'BRK', 'BRL', 'BVC', 'BVS', 'CASE',
'CLB0', 'CLB1', 'CLB2', 'CLB3', 'CLB4', 'CLB5', 'CLB6', 'CLB7',
'CLC', 'CLD', 'CLI', 'CLT', 'CLV', 'CMP', 'CODECHK', 'COM', 'COP', 'CPA',
'CPX', 'CPY', 'CYCLE', 'DATACHK', 'DC', 'DCB', 'DS', 'DEA', 'DEC', 'DEX', 'DEY',
'DIRECT', 'DUMP', 'EJECT', 'ELSE', 'ELSEIF', 'END', 'ENDF', 'ENDFUNC',
'ENDI', 'ENDIF', 'ENDINIT', 'ENDM', 'ENDMACRO', 'ENDP', 'ENDPROC',
'ENDR', 'ENDS', 'ENDSTACK', 'ENDW', 'ENDWHILE', 'ENDWITH', 'ENTRY',
'EOR', 'EQU', 'EXITM', 'EXPORT', 'FUNC', 'GBLA', 'GBLC', 'GOTO',
'IF', 'IMPORT', 'INA', 'INC', 'INCLUDE', 'INIT', 'INX', 'INY', 'JML', 'JMP',
'JSL', 'JSR', 'LCLA', 'LCLC', 'LDA', 'LDM', 'LDX', 'LDY', 'LEAVE',
'LOAD', 'LONGA', 'LONGI', 'LSR', 'MACHINE', 'MACRO', 'MEND', 'MEXIT',
'MSB', 'MVN', 'MVP', 'NOP', 'ORA', 'ORG', 'PAGESIZE', 'PEA', 'PEI',
'PER', 'PHA', 'PHB', 'PHD', 'PHK', 'PHP', 'PHX', 'PHY', 'PLA', 'PLB',
'PLD', 'PLP', 'PLX', 'PLY', 'PRINT', 'PROC', 'RECORD', 'REP', 'RMB0',
'RMB1', 'RMB2', 'RMB3', 'RMB4', 'RMB5', 'RMB6', 'RMB7', 'ROL', 'ROR',
'RRF', 'RTI', 'RTL', 'RTS', 'SBC', 'SEB0', 'SEB1', 'SEB2', 'SEB3',
'SEB4', 'SEB5', 'SEB6', 'SEB7', 'SEC', 'SED', 'SEG', 'SEGATTR', 'SEI',
'SEP', 'SET', 'SETA', 'SETC', 'SETT', 'SMB0', 'SMB1', 'SMB2', 'SMB3',
'SMB4', 'SMB5', 'SMB6', 'SMB7', 'SPACE', 'STA', 'STACKDP', 'STP',
'STRING', 'STX', 'STY', 'STZ', 'SWA', 'TAD', 'TAS', 'TAX', 'TAY', 'TCD',
'TCS', 'TDA', 'TDC', 'TITLE', 'TRB', 'TSA', 'TSB', 'TSC', 'TST', 'TSX',
'TXA', 'TXS', 'TXY', 'TYA', 'TYX', 'WAI', 'WHILE', 'WITH', 'WRITE',
'WRITELN', 'XBA', 'XCE'
}
def build_file_list(self):
dirlist = self.view.settings().get('mpw_macro_directories', [])
files = macro_files(dirlist, lambda x: x[:4].upper() == "M16.")
return files
def build_missing(self, known, missing):
state = 0
view = self.view
all = sublime.Region(0, view.size())
for r in view.lines(all):
text = view.substr(r).rstrip()
if text == "": continue
if re_comment.match(text): continue
tokens = re.split(re_ws, text, maxsplit=2)
if len(tokens) < 2: continue
label = tokens[0].upper()
opcode = tokens[1].upper()
ix = opcode.find('.')
if ix >= 0: opcode = opcode[:ix]
if opcode == "MACRO":
state = 1
continue
if opcode == "MEND":
state = 0
continue
if state == 1:
known.add(opcode)
missing.discard(opcode)
state = 0
continue
if opcode in self.opcodes: continue
if opcode in known: continue
missing.add(opcode)
def one_macro_file(self, file, known, missing):
buffer = ""
state = 0
for text in each_line(file):
if state == 2:
buffer += text + "\n"
if text == "": continue
if re_comment.match(text): continue
tokens = re.split(re_ws, text, maxsplit=2)
if len(tokens) < 2: continue
label = tokens[0].upper()
opcode = tokens[1].upper()
if state == 0:
if opcode == 'MACRO': state = 1
elif state == 1:
if opcode in missing:
known.add(opcode)
missing.discard(opcode)
buffer += "\tMACRO\n"
buffer += text + "\n"
state = 2
else:
state = 0
elif state == 2:
if opcode == 'MEND': state = 0
if opcode not in self.opcodes and opcode not in known:
missing.add(opcode)
return buffer