-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunMaeStar.py
297 lines (221 loc) · 7.83 KB
/
runMaeStar.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
import pickle
# We need to first load the previous objects
with open('quadruples', 'rb') as file_object:
quadruple_dict = pickle.load(file_object)
with open('variableTables', 'rb') as file_object:
variable_tables = pickle.load(file_object)
with open('procedureDirectory', 'rb') as file_object:
procedure_directory = pickle.load(file_object)
with open('availDict', 'rb') as file_object:
avail_dict = pickle.load(file_object)
virtual_machine = dict()
PC_stack = list() # Stack for program counter
PC = next(iter(quadruple_dict.keys())) # Program Counter as first element on the list
EOF = len(quadruple_dict.keys()) # End Of File
avail_flag = False
recursive_value = 0
# Convert variable_tables to virtual_machine
for _,values in variable_tables.items():
# values ex. [['int', 0], ['symbol', 'x']]
var_id = values[1][1]
var_type = values[0][0]
var_val = values[0][1]
virtual_machine[var_id] = [var_type,var_val]
"""
This method will return the dimensions of an operand
"""
def get_dim1(string):
start = string.index('[') + 1
end = string.index(']')
index_str = string[start:end]
get_val_from_oper(index_str)
dim1 = recursive_value
return dim1
"""
This method will return the dimensions of an operand
"""
def get_dim2(string):
start = string.index('[') + 1
end = string.index(']')
index_str = string[start:end]
get_val_from_oper(index_str)
dim1 = recursive_value
aux_string = string[end+1:]
end = aux_string.index(']')
index_str2 = aux_string[1:end]
get_val_from_oper(index_str2)
dim2 = recursive_value
return dim1, dim2
def getval_from_dimension(string):
dimension = string.count('[')
if dimension == 1:
dim1 = get_dim1(string)
id_result = string.partition("[")[0]
get_val_from_oper(virtual_machine[id_result][1][dim1])
val = recursive_value
elif dimension == 2:
dim1, dim2 = get_dim2(string)
id_result = string.partition("[")[0]
get_val_from_oper(virtual_machine[id_result][1][dim1][dim2])
val = recursive_value
return val
"""virtual_machine = {'i': ['int', 0], 'j': ['int', 0] }
avail_dict = {'T1': 0, 'T2': 0}
But we could end with reference to reference, therefore we must obtain the value by recursion"""
def get_val_from_oper(oper):
global recursive_value
if isinstance(oper, float):
recursive_value = float(oper)
return
elif isinstance(oper, int) or oper.isdigit():
recursive_value = int(oper)
return
elif oper in avail_dict:
get_val_from_oper(avail_dict[oper])
elif oper in virtual_machine:
get_val_from_oper(virtual_machine[oper][1])
elif '[' in oper:
oper_val_from_dim = getval_from_dimension(oper)
get_val_from_oper(oper_val_from_dim)
return
def store_value(oper_result, aux_result):
global avail_flag
if avail_flag:
avail_dict[oper_result] = aux_result
else:
virtual_machine[oper_result][1] = aux_result
def operation(program_counter):
global recursive_value
global avail_flag
op_code, oper1, oper2, oper_result = quadruple_dict[program_counter]
avail_flag = True if oper_result in avail_dict else False # check if it's from an avail
# print("avail: ", avail_flag)
# print(program_counter, " ",quadruple_dict[program_counter])
# print("*"*10)
# Get value of operand 1
get_val_from_oper(oper1)
val1 = recursive_value
# Get value of operand 2
get_val_from_oper(oper2)
val2 = recursive_value
# print(val1, " ", val2)
# Check if it's a dimension variable
dimension_flag = False
if isinstance(oper_result, str):
if '[' in oper_result:
dimension_flag = True
# Operations TODO call to function
if op_code == 'plus' or op_code == '++':
aux_result = val1 + val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == '-' or op_code == '--':
aux_result = val1 - val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == '*':
aux_result = val1 * val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == '/':
aux_result = val1 / val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == '%':
aux_result = val1 % val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == '<':
aux_result = val1 < val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == '>':
aux_result = val1 > val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == '==':
aux_result = val1 == val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == '!=':
aux_result = val1 != val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == 'and':
aux_result = val1 and val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code == 'or':
aux_result = val1 or val2
store_value(oper_result, aux_result)
program_counter += 1
elif op_code in ('goto','gotoFor'):
program_counter = int(oper_result)
elif op_code == 'gotoF': # goto False
if avail_dict[oper1]:
program_counter += 1
else:
program_counter = int(oper_result)
elif op_code == 'gotoT': # goto True
if avail_dict[oper1]:
program_counter = int(oper_result)
else:
program_counter += 1
elif op_code == 'call':
PC_stack.append(program_counter + 1)
program_counter = int(oper_result)
elif op_code == 'Return': # Methods
program_counter = int(PC_stack.pop())
elif op_code == 'read':
aux_result = input('>> ')
var1_type = virtual_machine[oper_result][0]
virtual_machine[oper_result][1] = int(aux_result) if var1_type == 'int' else float(aux_result)
program_counter += 1
elif op_code == 'write':
if oper_result[:2] == '__':
# Printing message
print(">> ", str(oper_result))
else:
get_val_from_oper(oper_result)
to_print = recursive_value
print(">> ", str(to_print))
program_counter += 1
elif op_code == '=':
# EX. ['=', 'num', '_', 'arr[i]'] arr[i] = num;
if dimension_flag:
dimension = oper_result.count('[') # Get how many dimensions
if dimension == 1:
dim1 = get_dim1(oper_result)
id_result = oper_result.partition("[")[0]
get_val_from_oper(oper1)
virtual_machine[id_result][1][dim1] = recursive_value
elif dimension == 2:
dim1, dim2 = get_dim2(oper_result)
id_result = oper_result.partition("[")[0]
get_val_from_oper(oper1)
virtual_machine[id_result][1][dim1][dim2] = recursive_value
# ['=', 'x', '_', 'y'] y = x;
else:
get_val_from_oper(oper1)
result_value = recursive_value
virtual_machine[oper_result][1] = result_value
program_counter += 1
return program_counter
while PC != EOF:
PC = operation(PC)
# time.sleep(.1)
"""
print()
print(virtual_machine)
print()
print(avail_dict)
print()"""
print()
for vm in virtual_machine:
if vm in avail_dict:
print("{0}: {1}".format(vm,avail_dict[virtual_machine]))
else:
print("{0}: {1}".format(vm,virtual_machine[vm]))
print()
print(avail_dict)
# print(quadruple_dict,variable_tables,procedure_directory, sep='\n\n\n')