-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (117 loc) · 3.61 KB
/
main.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
import csv
import copy
commands = []
command_dict = {}
with open('code.txt', 'r') as f:
code_txt = f.read()
line_code = code_txt.split('\n')
code_len = len(line_code)
for i in range(code_len):
in_line_code = line_code[i].split(' ')
if len(in_line_code) == 3:
commands.append([int(in_line_code[0]), in_line_code[1], in_line_code[2]])
else:
commands.append([int(in_line_code[0]), in_line_code[1]])
for i in range(code_len):
command_dict[commands[i][0]] = commands[i][1:]
print(commands)
data = [['ACC', '']]
data_dict = {}
with open('data.txt', 'r') as f:
data_txt = f.read()
line_data = data_txt.split('\n')
data_len = len(line_data)
for i in range(data_len):
in_line_data = line_data[i].split(' ')
data.append([in_line_data[0], in_line_data[1]])
data.append(['OUT', ''])
for i in range(data_len + 2):
data_dict[data[i][0]] = data[i][1]
# print(commands)
# print(data_dict)
count = 0
headers = ['line']
for i in data_dict:
headers.append(i)
rows = []
line = int(commands[0][0])
finish_line = int(commands[-1][0])
print(data_dict.items())
comp = False
while True:
# print(line, command_dict[line][0])
operand = ''
ix = ''
if command_dict[line][0] != 'OUT' and command_dict[line][0] != 'END':
operand = command_dict[line][1]
ix = data_dict['IX']
if command_dict[line][0] == 'LDM':
data_dict['ACC'] = operand
elif command_dict[line][0] == 'LDD':
data_dict['ACC'] = data_dict[operand]
elif command_dict[line][0] == 'LDI':
data_dict['ACC'] = data_dict[data_dict[operand]]
elif command_dict[line][0] == 'LDX':
data_dict['ACC'] = data_dict[str(int(operand) + int(ix))]
elif command_dict[line][0] == 'LDR':
data_dict['IX'] = operand
elif command_dict[line][0] == 'MOV':
data_dict[operand] = data_dict['ACC']
elif command_dict[line][0] == 'STO':
data_dict[operand] = data_dict['ACC']
elif command_dict[line][0] == 'ADD':
data_dict['ACC'] = str(int(data_dict['ACC']) + int(data_dict[operand]))
elif command_dict[line][0] == 'INC':
data_dict[operand] = str(int(data_dict[operand]) + 1)
elif command_dict[line][0] == 'DEC':
data_dict[operand] = str(int(data_dict[operand]) - 1)
elif command_dict[line][0] == 'CMP':
comp = (int(data_dict['ACC']) == int(data_dict[operand]))
elif command_dict[line][0] == 'JPE':
if comp:
line = int(operand)
continue
elif command_dict[line][0] == 'JPN':
if not comp:
line = int(operand)
continue
elif command_dict[line][0] == 'JMP':
line = int(operand)
continue
elif command_dict[line][0] == 'OUT':
data_dict['OUT'] = chr(int(data_dict['ACC']))
elif command_dict[line][0] == 'END':
break
else:
print('No opcode named ' + command_dict[line][0])
exit()
# store content of each line
content = [str(line)]
for i in data_dict:
content.append(data_dict[i])
rows.append(content)
count += 1
line += 1
# print(content)
#
# content.insert(0, line)
# add first line
content = ['']
for i in data:
content.append(i[1])
rows.insert(0, content)
# add the last one
content = [str(line)]
for i in data_dict:
content.append(data_dict[i])
rows.append(content)
rows_change = copy.deepcopy(rows)
for i in range(1, len(rows)):
for j in range(len(rows[i])):
if rows_change[i][j] == rows[i - 1][j]:
rows_change[i][j] = ' '
print(rows)
with open('table.csv', 'w') as f:
f_csv = csv.writer(f)
f_csv.writerow(headers)
f_csv.writerows(rows_change)