-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuadruples.py
220 lines (169 loc) · 6.6 KB
/
Quadruples.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
from collections import deque
from cube import SEMANTIC
import virtualAddress
from Directory import ConstantsTable
from error import SemanticError
class Quadruple:
def __init__(self):
self.quadruples = []
self.poper = deque() # poper = PILA OPERADORES
self.pilaO = deque() # pilaO = PILA DE OPERANDOS
self.pTypes = deque() # pTypes = PILA DE TIPOS
self.pSaltos = deque() # pSaltos = PILA DE SALTOS
self.pilaDIM = deque() # pilaDIM = PILA DE DIMENSIONES DE ARRAYS
self.counter = 666 #
self.quad_counter = 0 # CONT. PARA LISTA DE CUADRUPLOS
def increment_counter(self):
self.counter = self.counter + 1
# ADD TO STACKS
def push_poper(self, value):
self.poper.append(value)
def push_pilaO(self, value):
self.pilaO.append(value)
def push_pTypes(self, value):
self.pTypes.append(value)
def push_pSaltos(self, value):
self.pSaltos.append(value)
# Get stacks
def get_poper_stack(self):
return self.poper
def get_pilaO_stack(self):
return self.pilaO
def get_pilaTypes_stack(self):
return self.pTypes
def get_pilaSaltos_stack(self):
return self.pSaltos
def get_quads_list(self):
return self.quadruples
# Get TOP from stacks
def poper_top(self):
if(len(self.poper) > 0):
return self.poper[len(self.poper)-1]
else:
return None
def pilaO_top(self):
if(len(self.pilaO) > 0):
return self.pilaO[len(self.pilaO)-1]
else:
return None
def pTypes_top(self):
if(len(self.pTypes) > 0):
return self.pTypes[len(self.pTypes)-1]
else:
return None
def pSaltos_top(self):
if(len(self.pSaltos) > 0):
return self.pSaltos[len(self.pSaltos)-1]
else:
return None
def pDim_top(self):
if(len(self.pilaDIM) > 0):
return self.pilaDIM[len(self.pilaDIM)-1]
else:
return None
# When an operator is found it creates the quadruple
def found_operator(self, operator, location):
operator = self.poper.pop()
r_type = self.pTypes.pop()
l_type = self.pTypes.pop()
try:
res_type = SEMANTIC[l_type][r_type][operator]
self.pTypes.append(res_type)
r_operand = self.pilaO.pop()
l_operand = self.pilaO.pop()
va = virtualAddress.setAddress(res_type, f'temp{location}')
self.generateQuad(operator, l_operand, r_operand, va)
self.pilaO.append(va)
except:
raise SemanticError("Type mismatched")
# When the call is returned, it creates an quadruple with equal.
def found_equal(self):
operator = self.poper.pop()
r_type = self.pTypes.pop()
l_type = self.pTypes.pop()
try:
res_type = SEMANTIC[l_type][r_type][operator]
r_operand = self.pilaO.pop()
l_operand = self.pilaO.pop()
self.generateQuad(operator, r_operand, None, l_operand)
except:
raise SemanticError("Type mismatched")
# QUADRUPLE GENERATION AND FILL FUNCTIONS (GOTOs)
def generateQuad(self, operator, left_operand, right_operand, result):
self.quadruples.append([operator, left_operand, right_operand, result])
self.quad_counter = self.quad_counter + 1
def fillQuad(self, index, val):
self.quadruples[index][3] = val
# FUNCTIONS FOR IF, ELSE
def createIf(self, tag):
if(self.pTypes.pop() != "bool"):
raise SemanticError("Type mismatched expected a boolean exp")
oper = self.pilaO.pop()
self.pSaltos.append(self.quad_counter)
self.generateQuad(tag, oper, None, None)
def if_end(self):
# 1 - Pop pSaltos
jump = self.pSaltos.pop()
# 2 - Guarda contador actual en pSaltos
self.pSaltos.append(self.quad_counter)
# 3 - Genera GOTO para el else
self.generateQuad('GOTO', None, None, None)
# 4 - Rellena jump: GOTOF original con valor del contador actual
self.fillQuad(jump, self.quad_counter)
def else_end(self):
jump = self.pSaltos.pop()
self.fillQuad(jump, self.quad_counter)
# FOR FUNCTIONS
def for_equal_exp(self):
exp_type = self.pTypes.pop()
exp_res = self.pilaO.pop()
# Debe ser solo entera
if not ( numerical(exp_type) ):
raise SemanticError(f"Type mismatched {exp_type} is not numeric")
print(f"Variable \"{exp_type}\" no numerica ")
exit()
v_control = self.pilaO_top()
v_control_type = self.pTypes_top()
self.pilaO.append(v_control) # Para poder luego incrementarla
self.pTypes.append(v_control_type) # Para poder luego incrementarla
res_type = SEMANTIC[v_control_type][exp_type]["="] # Error if result not found
self.generateQuad("=", exp_res, None, v_control)
# < comparison
def for_comparison(self, location):
exp_type = self.pTypes.pop()
exp_res = self.pilaO.pop()
if not(numerical(exp_type)):
raise SemanticError(f"Type mismatched {exp_type} is not numeric")
# CREATE v_final
v_final_va = virtualAddress.setAddress(exp_type, f'temp{location}')
self.generateQuad('=', exp_res, None, v_final_va)
# COMPARE v_control with v_final
v_control = self.pilaO_top()
va = virtualAddress.setAddress('bool', f'temp{location}')
self.generateQuad('<', v_control, v_final_va, va)
self.pSaltos.append(self.quad_counter-1)
self.generateQuad('GOTOF', va , None, None)
self.pSaltos.append(self.quad_counter-1)
# for loop end
def for_end(self, location, address_one):
v_control = self.pilaO.pop()
v_control_type = self.pTypes.pop()
va = virtualAddress.setAddress('bool', f'temp{location}')
# USING VIRTUAL ADDRESS
self.generateQuad('+', v_control, address_one, va)
self.generateQuad('=', va, None, v_control)
self.generateQuad('=', va, None, self.pilaO_top())
end = self.pSaltos.pop()
ret = self.pSaltos.pop()
self.generateQuad('GOTO', None, None, ret)
self.fillQuad(end, self.quad_counter)
# ELIM COUNTER
self.pilaO.pop()
self.pTypes.pop()
################ AUX FUNCTIONS ################
def numerical(val):
if (val != "int" and val != "float"):
return False
else:
return True
################ END OF AUX FUNCTIONS ################