-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvirtual.py
370 lines (350 loc) · 17.7 KB
/
virtual.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
from memory import memory_table, memory,constant_table, init_memory,create_func_memory, call_stack, function_list,func_memory, GLOBAL_START, LOCAL_START, TEMPORAL_START, CONSTANT_START, temporal_bool, temporal_object
from time import sleep
import math
import random
from collections import deque
import pygame
cont = 0 # Quadruple counter
current_context = func_memory() # We use this to handle the current context
visitedFuncs = deque() # We use this to handle visited functions
screen = None
clock = pygame.time.Clock()
colors ={
"\"BLUE\"": (0,0,255),
"\"GREEN\"": (0,255,0),
"\"RED\"": (255,0,0),
"\"YELLOW\"": (255,255,0),
"\"ORANGE\"": (255,153,51),
"\"PINK\"": (255,102,178),
"\"GREY\"": (96,96,96),
"\"WHITE\"": (255,255,255),
"\"BLACK\"": (0,0,0),
"\"CYAN\"":(0,255,255),
}
# This is used when drawing a figure
class capi_object():
def __init__(self, screen,color, x, y, w, h, address):
self.screen = screen
self.color = color
self.x = x
self.y = y
self.w = w
self.h = h
self.address = address
def draw_object():
pygame.draw.rect(self.screen,self.color,(self.x,self.y,self.w,self.h))
self.draw_object = draw_object
def __str__(self):
return f'Color: {self.color}, x: {self.x}, y: {self.y}, w: {self.w}, h:{self.h}, Address: {self.address}\n'
def __repr__(self):
return f'Color: {self.color}, x: {self.x}, y: {self.y}, w: {self.w}, h:{self.h}, Address: {self.address}\n'
# Inits the virtual machine
def init_virtual(quadruples, func_dir):
global current_context,cont
# for i, q in enumerate(quadruples):
# print(i, " ", q)
init_memory(func_dir)
current_context = call_stack[-1]
# We use this to loop through the quadruples
while cont < len(quadruples):
action(quadruples[cont])
cont+=1
# We handle the quadruple with this function
def action(quadruple):
global cont, current_context, screen, clock
#print("Running quad: ", cont, " ", quadruple)
if quadruple.operator == '+':
if quadruple.isptr:
temp = get_value_visited_func(quadruple.left_operand).value + get_value_visited_func(quadruple.right_operand).value
value_from_pointer = get_value_visited_func(temp + 1).value
visitedFuncs[-1].memory_list[quadruple.temp] = memory(value_from_pointer, quadruple.temp)
else:
temp = get_value_visited_func(quadruple.left_operand).value + get_value_visited_func(quadruple.right_operand).value
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '-':
temp = get_value_visited_func(quadruple.left_operand).value - get_value_visited_func(quadruple.right_operand).value
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '*':
temp = get_value_visited_func(quadruple.left_operand).value * get_value_visited_func(quadruple.right_operand).value
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '/':
temp = get_value_visited_func(quadruple.left_operand).value / get_value_visited_func(quadruple.right_operand).value
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '&&':
temp = get_value_visited_func(quadruple.left_operand).value and get_value_visited_func(quadruple.right_operand).value
if temp == 0:
temp = False
elif temp == 1:
temp = True
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '||':
temp = get_value_visited_func(quadruple.left_operand).value or get_value_visited_func(quadruple.right_operand).value
if temp == 0:
temp = False
elif temp == 1:
temp = True
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '>=':
temp = get_value_visited_func(quadruple.left_operand).value >= get_value_visited_func(quadruple.right_operand).value
if temp == 0:
temp = False
elif temp == 1:
temp = True
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '<=':
temp = get_value_visited_func(quadruple.left_operand).value <= get_value_visited_func(quadruple.right_operand).value
if temp == 0:
temp = False
elif temp == 1:
temp = True
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '>':
temp = get_value_visited_func(quadruple.left_operand).value > get_value_visited_func(quadruple.right_operand).value
if temp == 0:
temp = False
elif temp == 1:
temp = True
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '<':
temp = get_value_visited_func(quadruple.left_operand).value < get_value_visited_func(quadruple.right_operand).value
if temp == 0:
temp = False
elif temp == 1:
temp = True
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '!=':
temp = get_value_visited_func(quadruple.left_operand).value != get_value_visited_func(quadruple.right_operand).value
if temp == 0:
temp = False
elif temp == 1:
temp = True
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == '==':
temp = get_value_visited_func(quadruple.left_operand).value == get_value_visited_func(quadruple.right_operand).value
if temp == 0:
temp = False
elif temp == 1:
temp = True
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
elif quadruple.operator == 'VERIFY':
index = get_value_visited_func(quadruple.left_operand).value
lower_bound = get_value_visited_func(quadruple.right_operand).value
upper_bound = get_value_visited_func(quadruple.temp).value
if index >= upper_bound or index < lower_bound:
raise Exception("Index out of bounds.")
elif quadruple.operator == '=':
if quadruple.isptr:
# We obtain the list address
list_address = quadruple.temp
# We get the value of the list addres and that is our address of our list.
index = get_value_visited_func(list_address).value + 1 # we add up one to get into the correct address.
if index not in visitedFuncs[-1].memory_list:
set_global_var(index, get_value_visited_func(quadruple.left_operand).value)
else:
visitedFuncs[-1].memory_list[index].value = get_value_visited_func(quadruple.left_operand).value
else:
if quadruple.temp >= GLOBAL_START and quadruple.temp <= LOCAL_START - 1:
set_global_var(quadruple.temp, get_value_visited_func(quadruple.left_operand).value)
else:
temp = get_value_visited_func(quadruple.left_operand).value
if quadruple.temp >= temporal_bool and quadruple.temp <= temporal_object - 1:
if temp == 0:
temp = False
elif temp == 1:
temp = True
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(temp, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = temp
elif quadruple.operator == 'SIZE':
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(get_value_visited_func(quadruple.left_operand).value, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = get_value_visited_func(quadruple.left_operand).value
elif quadruple.operator == 'HEAD':
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(get_value_visited_func(quadruple.left_operand + 1).value, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = get_value_visited_func(quadruple.left_operand + 1).value
elif quadruple.operator == 'FIND':
start = get_value_visited_func(quadruple.left_operand[0]).value
end = get_value_visited_func(quadruple.left_operand[1]).value
find = get_value_visited_func(quadruple.right_operand).value
result = False
for i in range(1,end + 1):
value = get_value_visited_func(start + i).value
if value == find:
result = True
break
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(result, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = result
elif quadruple.operator == 'LAST':
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(get_value_visited_func(quadruple.left_operand + get_value_visited_func(quadruple.right_operand).value).value, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = get_value_visited_func(quadruple.left_operand + get_value_visited_func(quadruple.right_operand).value).value
elif quadruple.operator == 'INIT':
print("Capi was initialized")
pygame.init()
elif quadruple.operator == 'QUIT':
print('Thank you for using Capi :)')
pygame.quit()
exit()
elif quadruple.operator == 'SET_DIM':
screen = pygame.display.set_mode((get_value_visited_func(quadruple.left_operand).value,get_value_visited_func(quadruple.right_operand).value))
elif quadruple.operator == 'SET_TITLE':
if type(get_value_visited_func(quadruple.left_operand).value) is not str:
raise Exception("Type mismatch!")
pygame.display.set_caption(removeQuotes(get_value_visited_func(quadruple.left_operand).value))
elif quadruple.operator == 'SET_FILL':
screen.fill((get_value_visited_func(quadruple.left_operand).value, get_value_visited_func(quadruple.right_operand).value, get_value_visited_func(quadruple.temp).value))
elif quadruple.operator == 'UPDATE':
pygame.display.flip() #Draws the objects in pygame
pygame.display.update() # Updates the frames
elif quadruple.operator =='CREATE_TEXT':
font = pygame.font.Font('freesansbold.ttf', 16)
color = colors[get_value_visited_func(quadruple.right_operand[0]).value]
x = get_value_visited_func(quadruple.right_operand[1]).value
y = get_value_visited_func(quadruple.right_operand[2]).value
text = get_value_visited_func(quadruple.left_operand).value
if type(text) is int or type(text) is float:
text = str(text)
if type(text) is bool:
raise Exception("Type mismatch!")
text_obj = font.render(removeQuotes(text), True, color)
screen.blit(text_obj,(x,y))
elif quadruple.operator == 'DRAW':
color = colors[get_value_visited_func(quadruple.left_operand).value]
x = get_value_visited_func(quadruple.right_operand[0]).value
y = get_value_visited_func(quadruple.right_operand[1]).value
w = get_value_visited_func(quadruple.right_operand[2]).value
h = get_value_visited_func(quadruple.right_operand[3]).value
c = capi_object(screen, color, x,y,w,h,quadruple.temp)
# We add our object to our memory list
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(c, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = c
# We draw the object
c.draw_object()
elif quadruple.operator == 'POW':
num = get_value_visited_func(quadruple.left_operand).value
pot = get_value_visited_func(quadruple.right_operand).value
result = math.pow(num,pot)
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(result, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = result
elif quadruple.operator == 'RAND':
inf_num = get_value_visited_func(quadruple.left_operand).value
sup_num = get_value_visited_func(quadruple.right_operand).value
rand_num = random.randint(inf_num, sup_num)
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(rand_num, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = rand_num
elif quadruple.operator == 'SQRT':
num = get_value_visited_func(quadruple.left_operand).value
result = math.sqrt(num)
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(result, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = result
elif quadruple.operator == 'GET_EVENT':
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
handle_event(quadruple, "\"KEYLEFT\"")
if event.key == pygame.K_RIGHT:
handle_event(quadruple, "\"KEYRIGHT\"")
if event.key == pygame.K_UP:
handle_event(quadruple, "\"KEYUP\"")
if event.key == pygame.K_DOWN:
handle_event(quadruple, "\"KEYDOWN\"")
if event.key == pygame.K_ESCAPE:
handle_event(quadruple,"\"KEYESCAPE\"")
else:
handle_event(quadruple, "\"NULL\"")
# We use this so that the user can quit the game
if event.type == pygame.QUIT:
print("Game terminated. Thank you for using Capi :)")
exit()
elif quadruple.operator == 'WINDOW_H':
x,y = screen.get_size()
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(y, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = y
elif quadruple.operator == 'WINDOW_W':
x,y = screen.get_size()
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(x, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = x
elif quadruple.operator == 'print':
print(get_value_visited_func(quadruple.temp).value)
elif quadruple.operator == 'GOTO':
cont = quadruple.temp - 1
elif quadruple.operator == 'GOTO_F':
if not get_value_visited_func(quadruple.left_operand).value:
cont = quadruple.temp - 1
elif quadruple.operator == 'PARAM':
param_index = int(quadruple.temp.split(" ")[1]) - 1
call_stack[-1].params[param_index].value = get_value_visited_func(quadruple.left_operand).value
elif quadruple.operator == 'ERA':
# We push the context into the call_stack
new_func = create_func_memory(quadruple.left_operand)
call_stack.append(new_func)
elif quadruple.operator == 'ENDFUNC':
# It checks if the function is not run and start so that the cont does not reset.
if visitedFuncs:
visitedFuncs.pop()
current_context = call_stack[-1] #-1 -> top()
if current_context.function_name != "run" and current_context.function_name != "start" :
cont = current_context.prev
call_stack.pop()
elif quadruple.operator == 'GOSUB':
visitedFuncs.append(call_stack[-1])
current_context = call_stack[-1]
current_context.prev = cont
cont = current_context.cont
def handle_event(quadruple, action):
if quadruple.temp not in visitedFuncs[-1].memory_list:
visitedFuncs[-1].memory_list[quadruple.temp] = memory(action, quadruple.temp)
else:
visitedFuncs[-1].memory_list[quadruple.temp].value = action
# Function used to get value from different scopes
def get_value_visited_func(address):
global current_context
if address >= CONSTANT_START:
return memory_table[address]
elif address >= LOCAL_START and address <= CONSTANT_START - 1:
memory_list = visitedFuncs[-1].memory_list
params_list = visitedFuncs[-1].params
if address not in memory_list:
for p in params_list.values():
if p.address == address:
return p
# This is used when the assign is created at the end of the function call
if address not in memory_list.keys():
return get_global_var(address)
return memory_list[address]
elif address >= GLOBAL_START and address <= LOCAL_START - 1:
value = None
for p in call_stack:
if p.function_name == "global":
return p.memory_list[address]
return value
# Function used in assign to set in global scope when the variables does not exist in current.
def set_global_var(address,value):
for p in call_stack:
if p.function_name == "global":
p.memory_list[address] = memory(value,address)
def removeQuotes(s):
return s.replace('"','')
def get_global_var(address):
for p in call_stack:
if p.function_name == "global":
return p.memory_list[address]