-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformat_source.py
executable file
·116 lines (92 loc) · 2.97 KB
/
format_source.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
#!/usr/bin/python3
from lib2to3.pgen2.token import LESSEQUAL
import sys
import re
import math
import argparse
import tempfile
import shutil
import os
from pathlib import Path
# parser = argparse.ArgumentParser(description='Beautify m68k assembly')
# parser.add_argument('--inplace')
# help='an integer for the accumulator')
# parser.add_argument('--sum', dest='accumulate', action='store_const',
# const=sum, default=max,
# help='sum the integers (default: find the max)')
# args = parser.parse_args()
tabsize : int = 4
directiveColumn : int = 16
operandColumn : int = 24
commentColumn : int = 48
def fillTabs(line: str, column : int):
expanded = line.expandtabs(tabsize)
length = len(expanded)
if length >= column:
line = line + ' '
else:
numTabs : int = int(math.ceil(float(column - length) / float(tabsize)))
line = line + ('\t' * numTabs)
return line
if __name__ == "__main__":
# print(f"Arguments count: {len(sys.argv)}")
if len(sys.argv) < 2 or not sys.argv[1]:
# FIXME: print to std::err instead
print("need filename")
sys.exit(-1)
# for i, arg in enumerate(sys.argv):
# print(f"Argument {i:>6}: {arg}")
# Using readlines()
inputFileName = sys.argv[1]
if not os.path.isfile(inputFileName):
print(inputFileName + "is not a file", file = sys.stderr)
exit(-1)
inputFile = open(sys.argv[1], 'r')
Lines = inputFile.readlines()
temp = tempfile.NamedTemporaryFile(mode='w+', buffering=- 1, encoding=None, newline=None, suffix=None, prefix=None, dir=Path(inputFileName).parent, delete=False)
count = 0
asmline = re.compile(
r"""(?P<empty>\s*?$)|(?P<startcomment>\s*[;*]+.*?)$|((?P<label>\S+?:?)?(\s+(?P<directive>\S+))?(\s+(?P<operands>(((".*?")|('.*?')|(\S+?)),?)*))?(\s+(?P<endcomment>.*?))?$)""")
for line in Lines:
match = asmline.match(line)
if not match:
print("NO MATCH in line {}: '{}'".format(count, line))
sys.exit(-1)
count += 1
empty = match.group('empty')
startcomment = match.group('startcomment')
label = match.group('label')
directive = match.group('directive')
operands = match.group('operands')
endcomment = match.group('endcomment')
out = ""
if match:
pass
# print("{}: startcomment '{}' label '{}' directive '{}' operands '{}' endcomment '{}'".format(
# count, startcomment, label, directive, operands, endcomment))
else:
print("No match line Line{}:".format(line))
assert False
if empty:
pass
elif startcomment:
assert not label and not directive and not operands and not endcomment
out = startcomment.rstrip()
else:
column = 0
if label:
out = out + label
if directive:
out = fillTabs(out, directiveColumn)
out = out + directive
if operands:
out = fillTabs(out, operandColumn)
out = out + operands
if endcomment:
out = fillTabs(out, commentColumn)
out = out + endcomment.strip()
print(out, file = temp)
temp.flush()
os.rename(temp.name, inputFileName)
temp.close()
sys.exit(0)