def llenarTS(self, tabla, arbol): existe = tabla.getSimboloFuncion(self.id) if existe != None: if self.replace != None: # Espacio para el return arbol.contador += 1 # Espacio para los parámetros arbol.contador += len(self.parametros) existe.rol = "Metodo" existe.funcion = self existe.tamanio = arbol.contador arbol.contador = 0 #print("se limpió? ------------>",arbol.contador, existe.tamanio) return else: error = Excepcion("42723", "Semantico", f"La función {self.id} ya existe.", self.linea, self.columna) arbol.excepciones.append(error) arbol.consola.append(error.toString()) return error if self.tipo.tipo != Tipo_Dato.VOID: # Espacio para el return arbol.contador += 1 # Espacio para los parámetros arbol.contador += len(self.parametros) f = Simbolo(self.id, self.tipo, "", self.linea, self.columna) f.rol = "Metodo" f.funcion = self f.tamanio = arbol.contador tabla.agregarSimbolo(f) arbol.contador = 0
def traducir(self, tabla, arbol): super().traducir(tabla,arbol) tablaLocal = Tabla(None) arbol.addc3d('\r@with_goto # Decorador necesario.') arbol.addc3d(f"\rdef {self.id}():") arbol.addc3d("global P") arbol.addc3d("global Pila") if self.tipo.tipo != Tipo_Dato.VOID: variable = Simbolo("return", self.tipo, None, self.linea, self.columna) variable.rol = "Variable Local" variable.posicion = tablaLocal.stack variable.tamanio = 1 tabla.agregarSimbolo(variable) tablaLocal.stack += 1 for i in self.parametros: i.traducir(tablaLocal, arbol) tablaLocal.anterior = tabla for i in self.instrucciones: i.traducir(tablaLocal, arbol) # Se llena el reporte de la tabla de símbolo for i in tablaLocal.variables: i.ambito = self.id tabla.agregarReporteSimbolo(i) arbol.addc3d(f"\n\treturn\n") return
def ejecutar(self, ts, arbol): super().ejecutar(ts, arbol) #el id es para guardarlo en la tabla exp = Simbolo(self.identificador, self.operacion, self.valor, self.linea, self.columna) ts.setVariable(exp) print("imprimir_declaracion")
def validacionesCheck(self, nombreColumna, tipo, val, expresion, tabla, arbol): #print("Validaciones CHECK-----------------",nombreColumna,tipo.toString(),val) variable = Simbolo(nombreColumna,tipo,val,self.linea,self.columna) tabla.setVariable(variable) resultado = expresion.ejecutar(tabla, arbol) #print("RESULTADO---------------------------------",resultado) if isinstance(resultado, Excepcion): return resultado return resultado
def traducir(self, tabla, arbol): super().traducir(tabla, arbol) tablaLocal = Tabla(None) arbol.addc3d('\r@with_goto # Decorador necesario.') arbol.addc3d(f"\rdef {self.id}():") arbol.addc3d("global P") arbol.addc3d("global Pila") arbol.tamanio_actual = self.size arbol.etiqueta_fin = tablaLocal.getEtiqueta() if self.tipo.tipo != Tipo_Dato.VOID: variable = Simbolo("return", self.tipo, None, self.linea, self.columna) variable.rol = "Variable Local" variable.posicion = tablaLocal.stack variable.tamanio = 1 tabla.agregarSimbolo(variable) tablaLocal.stack += 1 for i in self.parametros: i.traducir(tablaLocal, arbol) tablaLocal.anterior = tabla for i in self.declaraciones: i.traducir(tablaLocal, arbol) for i in self.instrucciones: i.traducir(tablaLocal, arbol) # Se llena el reporte de la tabla de símbolo for i in tablaLocal.variables: i.ambito = self.id tabla.agregarReporteSimbolo(i) arbol.addComen("Etiqueta de salida función") arbol.addc3d(f"label .{arbol.etiqueta_fin}\n") arbol.tamanio_actual = None return
def ejecutar(self, tabla, arbol): super().ejecutar(tabla, arbol) prim = None strGram = "" if self.tipo.tipo == Tipo_Dato.SMALLINT: prim = Primitivo(0, Tipo("", Tipo_Dato.SMALLINT), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.INTEGER: prim = Primitivo(0, Tipo("", Tipo_Dato.INTEGER), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.BIGINT: prim = Primitivo(0, Tipo("", Tipo_Dato.BIGINT), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.DECIMAL: prim = Primitivo(0, Tipo("", Tipo_Dato.DECIMAL), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.NUMERIC: prim = Primitivo(0, Tipo("", Tipo_Dato.NUMERIC), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.REAL: prim = Primitivo(0, Tipo("", Tipo_Dato.REAL), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.DOUBLE_PRECISION: prim = Primitivo(0, Tipo("", Tipo_Dato.DOUBLE_PRECISION), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.MONEY: prim = Primitivo(0, Tipo("", Tipo_Dato.MONEY), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.DATE: prim = Primitivo('1900-01-01', Tipo("", Tipo_Dato.DATE), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.TIMESTAMP: prim = Primitivo('1900-01-01', Tipo("", Tipo_Dato.TIMESTAMP), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.TIME: prim = Primitivo('1900-01-01', Tipo("", Tipo_Dato.DATE), strGram, 0, 0) elif self.tipo.tipo == Tipo_Dato.BOOLEAN: prim = Primitivo(True, Tipo("", Tipo_Dato.BOOLEAN), strGram, 0, 0) variable = Simbolo(self.nombre, self.tipo, prim.valor, 0, 0) resultadoInsertar = tabla.setVariable(variable) if resultadoInsertar != None: error = Excepcion("100", "Semantico", "La columna " + self.nombre + " yo existe", self.linea, self.columna) arbol.excepciones.append(error) arbol.consola.append(error.toString()) return error if self.expresion != None: resultado = self.expresion.ejecutar(tabla, arbol) if isinstance(resultado, Excepcion): return resultado return True
def analizar(self, tabla, arbol): super().analizar(tabla, arbol) existe = tabla.getSimboloVariable(self.id) if existe != None: error = Excepcion("42723", "Semantico", f"La variable {self.id} ya ha sido declarada", self.linea, self.columna) arbol.excepciones.append(error) arbol.consola.append(error.toString()) return error if self.expresion == None: variable = Simbolo(self.id, self.tipo, None, self.linea, self.columna) if self.constante: variable.rol = "Variable Local" else: variable.rol = "Constante" tabla.agregarSimbolo(variable) else: resultado = self.expresion.analizar(tabla, arbol) if isinstance(resultado, Excepcion): return resultado comprobar = self.comprobarTipo(self.tipo, resultado, None) if not comprobar: error = Excepcion("42723", "Semantico", f"Los tipos de datos no coinciden", self.linea, self.columna) arbol.excepciones.append(error) arbol.consola.append(error.toString()) return error variable = Simbolo(self.id, self.tipo, None, self.linea, self.columna) if self.constante: variable.rol = "Variable Local" else: variable.rol = "Constante" tabla.agregarSimbolo(variable)
def ejecutar(self, tabla, arbol): super().ejecutar(tabla, arbol) prim = None if self.tipo.tipo == Tipo_Dato.INTEGER: prim = Primitivo(0, Tipo_Dato.INTEGER, 0, 0) elif self.tipo.tipo == Tipo_Dato.NUMERIC: prim = Primitivo(0.0, Tipo_Dato.NUMERIC, 0, 0) variable = Simbolo(self.nombre, self.tipo, prim, 0, 0) resultadoInsertar = tabla.setVariable(variable) if resultadoInsertar != None: error = Excepcion("100", "Semantico", "La columna " + self.nombre + " yo existe", self.linea, self.columna) arbol.excepciones.append(error) arbol.consola.append(error.toString()) return error resultado = self.expresion.ejecutar(tabla, arbol) if isinstance(resultado, Excepcion): return resultado return True
def ejecutar(self, tabla, arbol): super().ejecutar(tabla,arbol) if(self.lcol == "*"): #vamos a mostrar todos #haremos un for val = "" tablaSelect = "" tablaSelect2 = "" tablaX = [] for x in range(0,len(self.lcol2)): if(x==0): # Cuando viene un Alias if isinstance(self.lcol2[x], SelectLista.Alias): #print(self.lcol2[x].id,self.lcol2[x].expresion.id) val = self.lcol2[x].expresion.devolverTabla(tabla,arbol) variable = Simbolo(self.lcol2[x].id,None,val,0,0) tabla.setVariable(variable) # Cuando viene una columna normal else: val = self.lcol2[x].devolverTabla(tabla,arbol) tablaSelect = extractTable(arbol.getBaseDatos(),val) else: if isinstance(self.lcol2[x], SelectLista.Alias): #print(self.lcol2[x].id,self.lcol2[x].expresion.id) val = self.lcol2[x].expresion.devolverTabla(tabla,arbol) variable = Simbolo(self.lcol2[x].id,None,val,0,0) tabla.setVariable(variable) # Cuando viene una columna normal else: val = self.lcol2[x].devolverTabla(tabla,arbol) tablaSelect2 = extractTable(arbol.getBaseDatos(),val) tablaSelect = self.unirTablas(tablaSelect,tablaSelect2) if(self.where != None): arbol.setTablaActual(tablaSelect) arbol.setColumnasActual(arbol.devolverColumnasTabla(val)) tablaSelect = self.where.ejecutar(tabla,arbol) columnas = self.devolverColumnas(tabla,arbol) if(self.lrows != None): for x in range(0,len(self.lrows)): resultado = self.lrows[x] #BUENO AQUI VIENE LO OTRO (O-w-O) #ORDER_BY #aqui va por orden #FROM---> WHERE--->GROUPBY---->HAVING---->SELECT--->DISTINCT--->ORDERBY---->LIMIT if isinstance(resultado, GroupBy): print("BIENVENIDO A GROUP-BY") grupo = self.lrows[x].ejecutar(tabla,arbol) tablaSelect = self.agruparTabla(tablaSelect,grupo) if isinstance(resultado, Having): print("BIENVENIDO A HAVING-BY") lrows = self.lrows[x].ejecutar(tabla,arbol) if isinstance(resultado, OrderBy): print("BIENVENIDO A ORDER-BY") orden = self.lrows[x].ejecutar(tabla,arbol) tablaSelect = self.devolverOrderBy(tablaSelect,orden,arbol) if isinstance(resultado, Limit): print("BIENVENIDO A LIMIT") limite = self.lrows[x].ejecutar(tabla,arbol) tablaSelect = self.devolverTablaLimite(tablaSelect,limite) return tablaSelect else: #vamos a mostrar por columna print("mostrar por columna") val = "" tablaSelect = [] tablaSelect2 = [] #Aqui se devuelve toda la tabla for x in range(0,len(self.lcol2)): if(x == 0): # Cuando viene un Alias if isinstance(self.lcol2[x], SelectLista.Alias): #print(self.lcol2[x].id,self.lcol2[x].expresion.id) val = self.lcol2[x].expresion.devolverTabla(tabla,arbol) variable = Simbolo(self.lcol2[x].id,None,val,0,0) tabla.setVariable(variable) # Cuando viene una columna normal else: val = self.lcol2[x].devolverTabla(tabla,arbol) arbol.setNombreTabla(val) tablaSelect = extractTable(arbol.getBaseDatos(),val) ''' val = self.lcol2[x].devolverTabla(tabla,arbol) arbol.setNombreTabla(val) tablaSelect = extractTable(arbol.getBaseDatos(),val) ''' else: if isinstance(self.lcol2[x], SelectLista.Alias): #print(self.lcol2[x].id,self.lcol2[x].expresion.id) val = self.lcol2[x].expresion.devolverTabla(tabla,arbol) variable = Simbolo(self.lcol2[x].id,None,val,0,0) tabla.setVariable(variable) # Cuando viene una columna normal else: val = self.lcol2[x].devolverTabla(tabla,arbol) tablaSelect2 = extractTable(arbol.getBaseDatos(),val) tablaSelect = self.unirTablas(tablaSelect,tablaSelect2) arbol.setTablaActual(tablaSelect) #aqui vamos a dividir por columnas data = np.array((tablaSelect)) res = [] esExpresion = False tamAnterior = 0 arr = [] for x in range(0,len(self.lcol)): #obtener nombre de columna if isinstance(self.lcol[x],Identificador): nombreColumna = self.lcol[x].devolverId(tabla,arbol) nombreTabla = val #obtener la posicion posicion = arbol.devolverOrdenDeColumna(nombreTabla,nombreColumna) arr.append(posicion) elif isinstance(self.lcol[x], SelectLista.Alias): print(self.lcol[x].id,self.lcol[x].expresion) valor = tabla.getVariable(self.lcol[x].id) #print(valor) valores = arbol.devolverColumnasTabla(valor.valor) #valores = self.lcol[x].expresion.devolverTabla(tabla,arbol) #valores = self.lcol[x].ejecutar(tabla, arbol) if isinstance(valores, Excepcion): return valores #arr = [] if(x == 0): if(self.lcol[x].expresion == "*"): for m in range(0,len(valores)): tal = valores[m].orden arr.append(tal) else: for m in range(0,len(valores)): if(valores[m].nombre == self.lcol[x].expresion): tal = valores[m].orden arr.append(tal) tamAnterior = len(valores) else: if(self.lcol[x].expresion == "*"): #arr = arr.copy() for m in range(0,len(valores)): tal = tamAnterior + valores[m].orden arr.append(tal) else: #arr = arr.copy() for m in range(0,len(valores)): if(valores[m].nombre == self.lcol[x].expresion): tal = tamAnterior + valores[m].orden arr.append(tal) else: # Si la columna es una expresión esExpresion = True valores = self.lcol[x].ejecutar(tabla, arbol) if isinstance(valores, Excepcion): return valores res.append(str(valores)) if esExpresion: return res else: res = arr tablaSelect = data[:, res] if(self.where != None): arbol.setTablaActual(tablaSelect) columnas = self.devolverColumnas(tabla,arbol) arbol.setColumnasActual(columnas) tablaSelect = self.where.ejecutar(tabla,arbol) if(self.lrows != None): for x in range(0,len(self.lrows)): resultado = self.lrows[x] #BUENO AQUI VIENE LO OTRO (O-w-O) #ORDER_BY #aqui va por orden #FROM---> WHERE--->GROUPBY---->HAVING---->SELECT--->DISTINCT--->ORDERBY---->LIMIT if isinstance(resultado, GroupBy): print("BIENVENIDO A GROUP-BY") #TO DO VALIDAR EL NUMERO DE COLUMNAS DEL GROUP grupo = self.lrows[x].ejecutar(tabla,arbol) tablaSelect = self.agruparTabla(grupo) if isinstance(resultado, Having): print("BIENVENIDO A HAVING-BY") lrows = self.lrows[x].ejecutar(tabla,arbol) if isinstance(resultado, OrderBy): # print("BIENVENIDO A ORDER-BY") self.lrows[x].setTabla(val) orden = self.lrows[x].ejecutar(tabla,arbol) #orden = self.lrows[x].ejecutar(tabla,arbol) tablaSelect = self.devolverOrderBy(tablaSelect,orden,arbol) if isinstance(resultado, Limit): print("BIENVENIDO A LIMIT") limite = self.lrows[x].ejecutar(tabla,arbol) tablaSelect = self.devolverTablaLimite(tablaSelect,limite) return tablaSelect
def traducir(self, tabla, arbol): super().traducir(tabla, arbol) retorno = Nodo3D() if self.expresion == None: s = Simbolo(self.id, self.tipo, None, self.linea, self.columna) s.posicion = tabla.stack tabla.stack += 1 s.rol = "Variable Local" s.tamanio = 1 tabla.agregarSimbolo(s) return else: if self.tipo.tipo != Tipo_Dato.BOOLEAN: s = Simbolo(self.id, self.tipo, None, self.linea, self.columna) s.posicion = tabla.stack s.rol = "Variable Local" s.tamanio = 1 tabla.agregarSimbolo(s) temporal1 = tabla.getTemporal() temporal2 = tabla.getTemporal() arbol.addComen(f"Declaración local: {self.id}") arbol.addc3d(f"{temporal1} = P + {tabla.stack}") arbol.addComen(f"Traduce la expresión") valor = self.expresion.traducir(tabla, arbol) arbol.addComen(f"Asignación") arbol.addc3d(f"{temporal2} = {valor.temporalAnterior}") arbol.addc3d(f"Pila[{temporal1}] = {temporal2}") tabla.stack += 1 return else: s = Simbolo(self.id, self.tipo, None, self.linea, self.columna) s.posicion = tabla.stack s.rol = "Variable Local" s.tamanio = 1 tabla.agregarSimbolo(s) temporal1 = tabla.getTemporal() temporal2 = tabla.getTemporal() arbol.addComen(f"Declaración local: {self.id}") arbol.addc3d(f"{temporal1} = P + {tabla.stack}") arbol.addComen(f"Traduce la expresión") valor = self.expresion.traducir(tabla, arbol) arbol.addComen(f"Asignación") if valor.temporalAnterior != "1" and valor.temporalAnterior != "0": retorno.imprimirEtiquetDestino(arbol, valor.etiquetaTrue) arbol.addc3d(f"{temporal2} = 1") etiqueta1 = tabla.getEtiqueta() arbol.addc3d(f"goto .{etiqueta1}") retorno.imprimirEtiquetDestino(arbol, valor.etiquetaFalse) arbol.addc3d(f"{temporal2} = 0") arbol.addc3d(f"label .{etiqueta1}") else: arbol.addc3d(f"{temporal2} = {valor.temporalAnterior}") arbol.addc3d(f"{temporal2} = {valor.temporalAnterior}") arbol.addc3d(f"Pila[{temporal1}] = {temporal2}") tabla.stack += 1 return