-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadrupleManager.py
More file actions
187 lines (156 loc) · 7.96 KB
/
QuadrupleManager.py
File metadata and controls
187 lines (156 loc) · 7.96 KB
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
# ----------------------------------------------------------- #
# ____ _____ _ #
# / __ \ | __ \ (_) #
# | | | |_ __ ___ _ __ | |__) |___ ___ _ __ _ ___ #
# | | | | '_ \ / _ \ '_ \ | _ // _ \ / _ \| '_ \| |/ _ \ #
# | |__| | |_) | __/ | | | | | \ \ (_) | (_) | | | | | __/ #
# \____/| .__/ \___|_| |_| |_| \_\___/ \___/|_| |_|_|\___| #
# | | #
# |_| #
# #
# Antonio Carlos Vargas Torres A00813182 #
# Rubén Eugenio Cantu Vota A00814298 #
# ----------------------------------------------------------- #
import Enums as Enums
import SemanticCube as SemanticCube
import MemoryManager as MemoryManager
import FunctionDirectory as FunctionDirectory
class QuadrupleManagerClass:
def __init__(self):
# import objs
self.Enums = Enums.Enums().Instance
self.SemanticCube = SemanticCube.SemanticCube().Instance
self.MemoryManager = MemoryManager.MemoryManager().Instance
self.FunctionDirectory = FunctionDirectory.FunctionDirectory().Instance
# Dict
self.Operations = self.Enums.Operations
self.Functions = self.Enums.FunctionsQuadruples
# Reset
self.resetQuadruples()
def resetQuadruples(self):
self.QuadrupleList = []
return True
def showQuadruples(self):
print('')
print('{0: <4}'.format('#'),
'{0: <7}'.format('Name'),
'{0: >21}'.format('Quadruple'))
for i in range(0, len(self.QuadrupleList)):
print('{0: <4}'.format(str(i)),
'{0: <7}'.format(str(list(self.Operations.keys())[list(self.Operations.values()).index(self.QuadrupleList[i][0])])), '[',
'{0: >3}'.format(str(self.QuadrupleList[i][0])),
'{0: >7}'.format(str(self.QuadrupleList[i][1])),
'{0: >7}'.format(str(self.QuadrupleList[i][2])),
'{0: >7}'.format(str(self.QuadrupleList[i][3])), ']')
return True
def addQuadruple(self, Op, VirDir1, VirDir2, FuncName):
try:
IndexOP = self.Operations[Op]
except ValueError:
print("\nERROR SINTAXIS / SEMANTICA. Operacion no reconocida: ", Op)
return None
if str(VirDir1).startswith('*'): Type1 = 'int'
else: Type1 = self.MemoryManager.getEntryType(VirDir1) if VirDir1 != None else None
if str(VirDir2).startswith('*'): Type2 = 'int'
else: Type2 = self.MemoryManager.getEntryType(VirDir2) if VirDir2 != None else None
return eval("self."+self.Functions[IndexOP])(Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName)
# + - * / > < >= <= <> == | &
def literalOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
ResultingType = self.SemanticCube.getResultingType(Type1, Type2, Op)
if ResultingType != None :
ResultVirDir = self.FunctionDirectory.addTemporalVariable(FuncName, None, ResultingType, [])
self.QuadrupleList.append([IndexOP, VirDir1, VirDir2, ResultVirDir])
return ResultVirDir
# Asignacion (A = b -> [= , b, None, A])
def assignOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
ResultingType = self.SemanticCube.getResultingType(Type1, Type2, '=')
if ResultingType != None :
self.QuadrupleList.append([IndexOP, VirDir1, None, VirDir2])
return True
else:
print("\nERROR SEMANTICA. Tipo de dato:", Type1,'no puede ser asignado a una variable de tipo:', Type2)
return None
# Print (print(A) -> [print , None, None, A])
def printOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
self.QuadrupleList.append([IndexOP, None, None, VirDir1])
return True
# Read
def readOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
self.QuadrupleList.append([IndexOP, None, None, VirDir1])
return True
# Random
def randomOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
if Type1 == 'int' and Type2 == 'int':
ResultVirDir = self.FunctionDirectory.addTemporalVariable(FuncName, None, 'int', [])
self.QuadrupleList.append([IndexOP, VirDir1, VirDir2, ResultVirDir])
return ResultVirDir
else:
print("\nERROR SEMANTICA. La funcion random solo puede recibir como parametros numeros enteros.")
return None
# gotoT -> [gotoT, BoolVirDir, None, Pendiente]
def gotoTOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
if (Type1 == 'bool'):
self.QuadrupleList.append([IndexOP, VirDir1, None, None])
return len(self.QuadrupleList)-1
else:
print("\nERROR SEMANTICA. La condicion en un if o un ciclo debe ser de tipo bool.")
return None
# gotoF -> [gotoF, BoolVirDir, None, Pendiente]
def gotoFOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
if (Type1 == 'bool'):
self.QuadrupleList.append([IndexOP, VirDir1, None, None])
return len(self.QuadrupleList)-1
else:
print("\nERROR SEMANTICA. La condicion en un if o un ciclo debe ser de tipo bool.")
return None
# goto -> (goto, None, None, Pendiente)
def gotoOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
self.QuadrupleList.append([IndexOP, None, None, None])
return len(self.QuadrupleList)-1
# goSub -> (goto, None, None, #CuadruploDeLaSubrutina)
def goSubOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
self.QuadrupleList.append([IndexOP, None, None, None])
return len(self.QuadrupleList)-1
# Params (A = b -> [= , b, None, A])
def paramsOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
ResultingType = self.SemanticCube.getResultingType(Type1, Type2, '=')
if ResultingType != None :
self.QuadrupleList.append([IndexOP, VirDir1, None, VirDir2])
return True
else:
print("\nERROR SEMANTICA. Tipo de dato:", Type1,'no puede ser asignado a una variable de tipo:', Type2)
return None
# return
def returnOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
self.QuadrupleList.append([IndexOP, None, None, None])
return True
# era
def eraOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
self.QuadrupleList.append([IndexOP, None, None, None])
return True
# verify
def verifyOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
self.QuadrupleList.append([IndexOP, VirDir1, 0, None])
return True
# end
def endOp(self, Op, IndexOP, VirDir1, VirDir2, Type1, Type2, FuncName):
self.QuadrupleList.append([IndexOP, None, None, None])
return True
def updateReturnReference(self, index, quadIndex):
if index < len(self.QuadrupleList):
Op = list(self.Operations.keys())[list(self.Operations.values()).index(self.QuadrupleList[index][0])]
if Op in ['gotoT', 'gotoF', 'goto', 'goSub', 'era', 'return', 'verify']:
self.QuadrupleList[index][3] = quadIndex;
return True
else:
print("\nERROR. El cuadruplo que intentas modificar no es un gotoX.")
return None
else:
print("\nERROR. Indice de Cuadruplo fuera de los limites.")
return None
def getQuadrupleListLength(self):
return len(self.QuadrupleList)
class QuadrupleManager:
Instance = QuadrupleManagerClass()
def __init__(self):
pass