Skip to content

Commit fcd0478

Browse files
committed
A: add main.py to init
1 parent 1662ba6 commit fcd0478

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

main.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from factory import Instr_Factory
2+
from utils import separate, trans
3+
import argparse
4+
5+
'''
6+
input: file_path
7+
mode: STD | FILE
8+
'''
9+
def main(mode: str, infile_path: str, outfile_path=None):
10+
assert not (mode == "FILE" and outfile_path is None)
11+
12+
fin = open(infile_path)
13+
lines = fin.readlines()
14+
lines = [int(line.strip('\n'), 16) for line in lines]
15+
16+
if mode == "FILE":
17+
fout = open(outfile_path, "w", encoding="utf-8")
18+
19+
sepa_lst = [separate(line) for line in lines]
20+
trans_lst = [trans(line) for line in sepa_lst]
21+
instr_txt_lst = []
22+
for line in trans_lst:
23+
instr = line[0]
24+
instr_fact = Instr_Factory(
25+
instr["instr"], instr["OP"], instr["Rs"],
26+
instr["Rt"], instr["Rd"], instr["SA"], instr["FUNCT"]
27+
)
28+
instr_txt_lst.append(instr_fact.instr_txt)
29+
30+
if mode == "FILE":
31+
# add '\n'
32+
instr_txt_lst = [instr_txt+'\n' for instr_txt in instr_txt_lst]
33+
fout.writelines(instr_txt_lst)
34+
elif mode == "STD":
35+
for instr_txt in instr_txt_lst:
36+
print(instr_txt)
37+
else:
38+
pass
39+
40+
parser = argparse.ArgumentParser()
41+
42+
choices = [
43+
{ "method": "-d", "vmethod": "--dsasmb", "help": "disassemble *.txt" },
44+
{ "method": "-o", "vmethod": "--output", "help": "output to *.txt" }
45+
]
46+
47+
for choice in choices:
48+
parser.add_argument(
49+
choice["method"], choice["vmethod"], type=str,
50+
help=choice["help"]
51+
)
52+
53+
args = parser.parse_args()
54+
55+
if __name__ == "__main__":
56+
infile_path = args.dsasmb
57+
outfile_path = args.output
58+
59+
assert infile_path is not None
60+
61+
if outfile_path is None:
62+
main("STD", infile_path)
63+
else:
64+
main("FILE", infile_path, outfile_path)

0 commit comments

Comments
 (0)