-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
385 lines (289 loc) · 12.1 KB
/
interpreter.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
# done in python 3.9
import time
labels_list = ['A', 'B', 'C', 'D', 'E'] #List of Labels, ordered based on Davis's Book
vars_list = ['X', 'Z'] #List of Variables, Ordered based on Davis's Book
def find_all_vars(list_of_command_codes, count_vars, var_values) -> tuple:
"""
Input:
- list_of_command_codes (list, str): a list of codes, each representing a line of program
- count_vars (int): the number of input variables
- var_values (list, int): values of inputs, assumed in order (i.e. X1, X2, ...)
Output:
- outputs_dict (dict, str:int): variables, (in order, each category sorted based on index)
inputs, locals, output
- labels_line (dict, str:int): labels and their respective lines, to change the pointer
accordingly.
Note: The variables list consists of inputs from X1 to X{max_x_id} - e. g. if the program includes only
X1 and X3, and X2 is just given in the inputs, without being mentioned throughout the program,
X2 is also included in outputs_dict; or if it only includes X3 and X1 and X2 are inputs without
being mentioned, they are still included in outputs_dict. The same goes for the local variables.
Function:
Passes through the program, and extracts variables and labels. Then assigns each variable
its respective value (locals and output initiated to zero, inputs being assigned if given)
and finds the respective lines of the labels, to set the pointer when reaching the conditional
branches.
Example:
Input: [45 34 350 2 46], 2, [2, 1]
Output: {X1: 2, X2: 1, X3: 0, Z1: 0, Z2: 0, Y: 0}
"""
outputs_dict = {} #contains the variables
labels_line = {} # labels and their appearance in the lines
max_x_id = -1 #storing the max id of Xs
max_z_id = -1 #storing the max id of Zs
# Setting max ids and labels_line
for i in range(len(list_of_command_codes)):
code = list_of_command_codes[i]
a, _, c = decompose(code) # the operation matters not to us here, hence we discard b with an underline.
l = ""
if a != 0: # only add the label if existent, to reduce redunduncy.
l = label(a)
labels_line[l] = i + 1
if c + 1 > 1: # Y is included in the end.
if (c + 1) % 2 == 0: # finding the maximum id of X. More on ids in find_var() doc.
if int((c + 1)/2) > max_x_id:
max_x_id = int((c + 1)/2)
else: # finding the maximum id of Z. More on ids in find_var() doc.
if int((c + 1)/2) > max_z_id:
max_z_id = int((c + 1)/2)
# Traverse the list of Xs, assign values.
for i in range(1, max_x_id + 1):
if i <= count_vars: # if mentioned in the inputs...
outputs_dict[f"X{i}"] = var_values[i - 1]
else:
outputs_dict[f"X{i}"] = 0
# Assigning values to Zs
for i in range(1, max_z_id + 1):
outputs_dict[f"Z{i}"] = 0
outputs_dict["Y"] = 0 # add Y
return outputs_dict, labels_line
def execute(command_code, pointer, variable_dict, labels_line, program_length) -> tuple:
"""
Inputs:
- command_code (int): the code of a line in the program.
- pointer (int): # of the line to be executed
- variable_dict (str:int): a dictionary of variables, to be updated after the execution of the
a given command
- labels_line (str:int): a dictionary of labels and their respective lines,
to decide what to do post-execution of a conditional branch.
- program_length (int): # of program's lines, to determine where to stop execution.
Outputs:
- continue_execution (boolean): Whether the program stops after the execution of a given command.
- no_output (boolean): Kind of a dummy thing, to stop the duplication of the final lines in looping
programs.
- new_pointer (int): the next line that has to be executed.
- variable_dict (str:int): the updated values of the variables post-execution.
Function:
Interprets a line, executes it, sets the next line, updates the variables
until termination.
"""
# interpret the code: what variable, which operation, ignore its label, find the destination of a
# conditional branch.
variable, operation, _, forward_label = transform_to_executable(command_code)
new_pointer = -1
continue_execution = True # by default, the game is on!
no_output = False # by default, we print the output of the snapshot
if operation == "+1": # V <- V + 1
variable_dict[variable] += 1
if pointer == program_length:
# in case it is the final line - plus, I didn't add one to the
# pointer to make the final snapshot's i = n + 1, per directions by
# TA. Anyways the final snapshot must have i = n + 1, do please forgive this small bug
new_pointer = pointer # + 1
continue_execution = False
else:
new_pointer = pointer + 1
elif operation == "-1": # V <- V - 1
if variable_dict[variable] != 0:
variable_dict[variable] -= 1
if pointer == program_length: # in case it is the final line
new_pointer = pointer # + 1
continue_execution = False
else:
new_pointer = pointer + 1
elif operation == "assign": # V <- V
if pointer == program_length: # in case it is the final line
new_pointer = pointer # + 1
continue_execution = False
else:
new_pointer = pointer + 1
else: # IF V != 0 GOTO L
if forward_label in labels_line.keys():
if variable_dict[variable] == 0: # if V == 0
if pointer == program_length: # in case it is the final line
new_pointer = program_length
continue_execution = False
no_output = True # no duplication of the final snapshot
else:
new_pointer = pointer + 1 # Go to the next line
else:
new_pointer = labels_line[forward_label] # jump to the destination
else: # the label is not in the program, i.e. Exit
if variable_dict[variable] == 0: # in case it is the final line
if pointer == program_length:
new_pointer = program_length
continue_execution = False
no_output = True
else:
new_pointer = pointer + 1 # Goto the next line
else:
new_pointer = labels_line[forward_label] # jump to the destination
return continue_execution, no_output, new_pointer, variable_dict
def transform_to_executable(command_code) -> tuple:
"""
Input:
command_code (int): the code of a line of the program
Outputs:
var (str): The variable the command operates on.
operation (str): The operation done on the operand, that can be:
- V += 1
- V -= 1
- V = V
- conditional branch
l (str): the label of the line
forward_label (str): The destination label, in case of having a conditional branch.
Function:
Receives a line's code and extracts the outputs for use.
Example:
Input: 10
Output: X1, "+1", "A1" , ""(empty string)
"""
a, b, c = decompose(command_code) # <a, <b, c>>
l, var, operation, forward_label = "", "", "", ""
# find the instruction's label
if a != 0:
l = label(a)
# Find the operand
var = find_var(c + 1)
# find the operation
operation = ""
if b == 0: # V <- V
operation = "assign"
elif b == 1: # V <- V + 1
operation = "+1"
elif b == 2: # V <- V - 1
operation = "-1"
else: # IF V != 0 GOTO L
forward_label = label(b - 2)
operation = "GOTO"
return var, operation, l, forward_label
def decompose(program_code):
"""
Input (int): program_code - a command's respective code
Output (string): a structure in the form of <a, <b, c>, which represent label,
command type and variable, respectively.
Calculates a, b, c based on the pairing function 2^x(2y + 1) = z + 1
Example:
input: 21
output: 1, 1, 1 (<1, <1, 1>>)
"""
a, b, c = 0, 0, 0 #<a, <b, c>>
# Extracting a and <b, c>
temp_code = program_code + 1
twos = 0
T = 0 # <b, c>
while temp_code % 2 == 0:
twos += 1
temp_code = int(temp_code/2)
a = twos
twos = 0
T = int((temp_code - 1)/2)
# Extracting b and c
T += 1
while T % 2 == 0:
twos += 1
T = int(T/2)
b = twos
c = int((T - 1)/2)
return a, b, c
def label(n):
"""
Input (int): Receives a label's place in their ordering
Output (string): The Label
Based on s1 ch4 of the book. The labels can be ordered like below:
A1 B1 C1 D1 E1
A2 B2 C2 D2 E2
A3 B3 C3 D3 E3
.
.
.
As they have a one-to-one relation to the natural numbers, the above ordering
becomes as follows:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
.
.
.
Paying attention, we can notice a pattern:
i) Indices of A_i % 5 = 1
ii) Indices of B_i % 5 = 2
iii) Indices of C_i % 5 = 3
iv) Indices of D_i % 5 = 4
v) Indices of E_i % 5 = 0
Using the above knowledge, we can find the correct label.
Example:
Input: 1
Output: A1
"""
if n % 5 == 0:
return f"E{int(n/5)}"
else:
num_label = int(n/5) + 1
alph_label = labels_list[int(n%5) - 1]
return f"{alph_label}{num_label}"
def find_var(n):
"""
Input (int): A variable's place in their ordering
Output (string): The variable
Based on Davis s1 ch4. The variables can be ordered like below:
Y
X1 Z1
X2 Z2
X3 Z3
.
.
.
.
As they have a one-to-one relation to the natural numbers, we can write:
1
2 3
4 5
6 7
.
.
.
The pattern is obvious: Indices of X_i is even, and Z_i's are odd.
Example:
Input: 1
Output: 'Y'
Input: 2
Output: X1
Input: 3
Output: Z1
"""
if n == 1:
return 'Y'
else:
return f"{vars_list[int(n%2)]}{int(n/2)}"
##############################MAIN SECTION##############################
program_lines = list(map(int, input().split()))
inputs = list(map(int, input().split()))
variables_dict, labels_line = find_all_vars(program_lines, len(inputs), inputs) # find variables and labels
pointer = 1 # go to the first line
# print the names of the vars in a line with the inital pointer: i [X_i] [Z_i] Y (i = 1, 2, ...)
print("i", end=" ")
var_line = " ".join(variables_dict.keys())
print(var_line)
print("-"*(len(var_line) + 3)) # for more beauty :)
# print the init state
print(pointer, end=" ")
print(" ".join(map(str, variables_dict.values())))
while(True): # Execute till die!
continue_execution, no_output, new_pointer, variables_dict = execute(program_lines[pointer - 1], pointer, variables_dict, labels_line, len(program_lines))
pointer = new_pointer
time.sleep(3) # added a timer so that the infinite output is easier to see.
if no_output == False: # avoid duplication
print(pointer, end=" ")
print(" ".join(map(str, variables_dict.values())))
if continue_execution == False:
break