-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommander.py
316 lines (246 loc) · 10.6 KB
/
commander.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
from variables import *
from expressioner import *
from conditioner import *
from helping_functions import *
class Program:
variables = {}
iterators_stack = []
next_free_memory_location = -1
def __init__(self):
self.commands = []
def __init__(self, commands: list, variables_dict: dict, next_free_memory_location: int, output_file_name: str):
self.commands = commands
self.output_file_name = output_file_name
Program.variables = variables_dict
Program.next_free_memory_location = next_free_memory_location
def add_command(self, command):
self.commands.append(command)
def execute_commands(self):
self.commands.append(HaltCommand())
code = ""
for i in self.commands:
if isinstance(i, Command):
i.generate_code()
code += "\n".join(i.code) + "\n"
with open(self.output_file_name, "w") as file:
file.write(code)
class Command:
def __init__(self):
pass
def generate_code(self):
pass
class HaltCommand(Command):
def __init__(self):
self.code = []
def generate_code(self):
self.code = ["HALT"]
class WriteCommand(Command):
def __init__(self, variable):
self.variable = variable
self.code = []
def generate_code(self):
if type(self.variable) == int:
self.code = []
self.code.extend(load_value_to_register(self.variable, "b"))
self.code.extend(["RESET a", "STORE b a", "PUT a"])
elif type(self.variable) == Variable:
self.code = load_value_to_register(
self.variable.memory_location, "a")
self.code.append("PUT " + "a")
elif type(self.variable) == VariableOfArray:
self.code = load_value_to_register(
self.variable, "a", do_variablearray_memory=True)
# Actually.. it loaded only location of variable to register "a"
self.code.append("PUT a")
elif type(self.variable) == str:
self.code = load_value_to_register(
self.variable, "a", do_iterator_memory=True)
self.code.append("PUT a")
class ReadCommand(Command):
def __init__(self, variable):
self.variable = variable
self.code = []
def generate_code(self):
self.code = load_value_to_register(
self.variable.memory_location, "a")
self.code.append("GET " + "a")
class AssignCommand(Command):
def __init__(self, variable, expression: Expression):
self.variable = variable
self.expression = expression
self.code = []
def generate_code(self, register="b"):
if type(self.variable) == VariableOfArray:
self.code = load_value_to_register(
self.variable, "a", do_variablearray_memory=True)
else:
self.code = load_value_to_register(
self.variable.memory_location, "a")
self.code.extend(self.expression.generate_code("b"))
self.code.append("STORE b a")
class IfCommand(Command):
def __init__(self, condition: Condition, commands: list):
self.condition = condition
self.commands = commands
self.code = []
def generate_code(self, register="a"):
commands_code = []
self.code = []
for command in self.commands:
command.generate_code()
commands_code.extend(command.code)
self.code = self.condition.generate_code(len(commands_code)+1)
self.code.extend(commands_code)
class IfElseCommand(Command):
def __init__(self, condition: Condition, commands_if: list, commands_else: list):
self.condition = condition
self.commands_if = commands_if
self.commands_else = commands_else
self.code = []
def generate_code(self, register="a"):
commands_if_code = []
commands_else_code = []
for command in self.commands_if:
command.generate_code()
commands_if_code.extend(command.code)
for command in self.commands_else:
command.generate_code()
commands_else_code.extend(command.code)
self.code = self.condition.generate_code(len(commands_if_code)+2)
self.code.extend(commands_if_code)
self.code.append("JUMP " + str(len(commands_else_code) + 1))
self.code.extend(commands_else_code)
class WhileCommand(Command):
def __init__(self, condition: Condition, commands: list):
self.condition = condition
self.commands = commands
self.code = []
def generate_code(self, register="a"):
commands_code = []
for command in self.commands:
command.generate_code()
commands_code.extend(command.code)
self.code = self.condition.generate_code(len(commands_code)+2)
self.code.extend(commands_code)
self.code.append("JUMP " + str(-len(self.code)))
class RepeatCommand(Command):
def __init__(self, condition: Condition, commands: list):
self.condition = condition
self.commands = commands
self.code = []
def generate_code(self, register="a"):
commands_code = []
for command in self.commands:
command.generate_code()
commands_code.extend(command.code)
self.code = commands_code
self.code.extend(self.condition.generate_code(-len(self.code)))
class ForCommand(Command):
def __init__(self, pid: str, from_value: int, to_value: int, commands: list):
self.pid = pid
self.commands = commands
self.from_value = from_value
self.to_value = to_value
self.code = []
def contains_another_for(self):
return True
def generate_code(self, register="a"):
# optymalizacje: gdy jeden for; przeskoczenie pierwszego wczytania
self.code = load_value_to_register(self.from_value, "e")
self.code.extend(load_value_to_register(self.to_value, "f"))
self.code.append("INC f")
my_iterator = add_iterator(self.pid, self.from_value, self.to_value)
self.code.extend(load_value_to_register(
my_iterator.memory_location, "d"))
# e-poczatek iteratora, f-koniec iteratora +1, (+1 dla warunku czy wiekszy)
# d-memory iteratora, d+1 -> memory konca iteratora
self.code.extend(["STORE e d", "INC d", "STORE f d", "DEC d"])
# --- kod sprawdzenia warunku
condition_code = ["SUB f e"]
# --- kody podkomend + store iteratora
sub_commands_code = ["STORE e d"]
for command in self.commands:
command.generate_code()
sub_commands_code.extend(command.code)
# --- kod inkrementacji iteratora
incrementation_code = load_value_to_register(
my_iterator.memory_location, "d")
incrementation_code.extend(
["LOAD e d", "INC d", "LOAD f d", "DEC d", "INC e"])
# --- przeskok z warunku poza for'a
# +1 żeby poza kod, +1 żeby ponad jumpa
condition_code.append(
"JZERO f " + str(len(sub_commands_code)+len(incrementation_code)+2))
# --- przeskok do sprawdzania warunku
jump_code = "JUMP " + \
str(-(len(sub_commands_code) +
len(condition_code) + len(incrementation_code)))
self.code.extend(condition_code)
self.code.extend(sub_commands_code)
self.code.extend(incrementation_code)
self.code.append(jump_code)
remove_iterator(my_iterator)
class ForDownCommand(Command):
def __init__(self, pid: str, from_value: int, to_value: int, commands: list):
self.pid = pid
self.commands = commands
self.from_value = from_value
self.to_value = to_value
self.code = []
def contains_another_for(self):
return True
def generate_code(self, register="a"):
# Różnica z ForNormal: zawsze jest INC iteratora przy sprawdzaniu, zamiast dodac go na początku
# optymalizacje: gdy jeden for; przeskoczenie pierwszego wczytania
self.code = load_value_to_register(self.from_value, "e")
self.code.extend(load_value_to_register(self.to_value, "f"))
my_iterator = add_iterator(self.pid, self.from_value, self.to_value)
self.code.extend(load_value_to_register(
my_iterator.memory_location, "d"))
# e-poczatek iteratora, f-koniec iteratora +1, (+1 dla warunku czy wiekszy)
# d-memory iteratora, d+1 -> memory konca iteratora
self.code.extend(["STORE e d", "INC d", "STORE f d", "DEC d"])
# --- kod sprawdzenia warunku
condition_code = ["INC e", "SUB e f"]
# --- kody podkomend + store iteratora z przywróceniem
sub_commands_code = ["ADD e f", "DEC e", "STORE e d"]
for command in self.commands:
command.generate_code()
sub_commands_code.extend(command.code)
# --- kod inkrementacji iteratora
incrementation_code = load_value_to_register(
my_iterator.memory_location, "d")
incrementation_code.extend( # +1 poza kod, +1 poza jump_code, +4 poza kod w swojej tablicy
["LOAD e d", "JZERO e " + str(1+1+4), "INC d", "LOAD f d", "DEC d", "DEC e"])
# --- przeskok z warunku poza for'a
# +1 żeby poza kod, +1 żeby ponad jumpa
condition_code.append(
"JZERO e " + str(len(sub_commands_code) + len(incrementation_code) + 2))
# --- przeskok do sprawdzania warunku
jump_code = "JUMP " + \
str(-(len(sub_commands_code) +
len(condition_code) +
len(incrementation_code)))
self.code.extend(condition_code)
self.code.extend(sub_commands_code)
self.code.extend(incrementation_code)
self.code.append(jump_code)
remove_iterator(my_iterator)
def add_iterator(pid, start, end):
iterator = VariableIterator(
pid, Program.next_free_memory_location, start, end)
Program.next_free_memory_location += 2
if [el for el in commander.Program.iterators_stack if el.pid == pid]:
raise IteratorAlreadyExists
Program.iterators_stack.append(iterator)
if pid in Program.variables:
Program.variables[pid].is_shadowed = True
Program.variables[pid].reference_to_iterator = iterator
return iterator
def remove_iterator(iterator: VariableIterator):
Program.iterators_stack.remove(iterator)
if iterator.pid in Program.variables:
Program.variables[iterator.pid].is_shadowed = False
Program.variables[iterator.pid].reference_to_iterator = None
class IteratorAlreadyExists(Exception):
pass