def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): temp = arbol.getTemp() if self.data.lower() == "true": arbol.addC3D(temp + " = " + "True") elif self.data.lower() == "false": arbol.addC3D(temp + " = " + "False") return temp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): tmp = arbol.getTemp() arbol.addC3D(tmp + " = " + str(self.identificador)) val_exp: Valor = Valor(TIPO.CADENA, 'NULL') simbol: Simbolo = Simbolo(str(self.identificador), val_exp.tipo, val_exp) simbol.setTemp(str(tmp)) entorno.insertar_variable(simbol)
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): where = '' if self.wherexp is not None: where = ' WHERE ' + self.wherexp.getString(entorno, arbol) arbol.addC3D('heap = ' + '\'' + 'DELETE FROM ' + str(self.identificador) + where + ';\'') temp = arbol.getTemp() arbol.addC3D(temp + ' = inter()') return temp
def traducir(self, entorno: Tabla_de_simbolos, arbol:Arbol): try: tmp = self.getValueAbstract(entorno, arbol) arbol.addC3D("print(str(" + str(tmp) + "))") except: arbol.addC3D("print(str(Invalid))") descripcion = 'Expresion invalida para el print' reportero = ErroresSemanticos(descripcion, str(self.linea), str(self.columna), 'Raise complex') arbol.ErroresSemanticos.append(reportero) return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): # verificar si la fun esta activa: if arbol.lFun_isINACTIVE(str(self.identificador)): descripcion = 'Funcion ' + str( self.identificador) + ' esta inactivado, no puede usarse' reportero = ErroresSemanticos(descripcion, str(self.linea), str(self.columna), 'lappel') arbol.ErroresSemanticos.append(reportero) temp = arbol.getTemp() arbol.addC3D(temp + ' = \'\'') return temp #------------------------------------------ argumentos = '' contador = 0 try: for i in self.expres: if contador == 0: argumentos = argumentos + i.getString(entorno, arbol) contador = 1 else: argumentos = argumentos + ',' + i.getString(entorno, arbol) except: argumentos = '' temp = arbol.getTemp() if str(self.identificador) == 'now': ahora = '\'' + str(datetime.now()) + '\'' arbol.addC3D(temp + ' = ' + ahora) else: arbol.addC3D(temp + ' = ' + str(self.identificador) + '(' + argumentos + ')') return temp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): if arbol.existFun(str(self.identificador)): pass else: nota = 'Funcion ' + str(self.identificador) + ' no existe' reportero = ErroresSemanticos(nota, self.linea, self.columna, 'dropfun') arbol.ErroresSemanticos.append(reportero) return arbol.dropFuncione(str(self.identificador)) print('NH -> funcion ' + str(self.identificador) + ' eliminada exitosamente') return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): if self.tipoInsert == 1: argumentos = '' contador = 0 for i in self.expres: if contador == 0: argumento = i.getString(entorno, arbol) if str(argumento) == 'now()': ahora = '\'' + str(datetime.now()) + '\'' argumentos = argumentos + ahora else: argumentos = argumentos + argumento contador = 1 else: argumento = i.getString(entorno, arbol) if str(argumento) == 'now': ahora = '\'' + str(datetime.now()) + '\'' argumentos = argumentos + ',' + ahora else: argumentos = argumentos + ',' + argumento arbol.addC3D('heap = \'' + 'insert into ' + str(self.identificador) + ' values (' + argumentos + ');' + '\'') temp = arbol.getTemp() arbol.addC3D(temp + ' = inter()') return temp else: lista_id = '' contador_1 = 0 for i in self.idlist: if contador_1 == 0: lista_id = lista_id + str(i) contador_1 = 1 else: lista_id = lista_id + ',' + str(i) argumentos = '' contador = 0 for i in self.expres: if contador == 0: argumentos = argumentos + i.getString(entorno, arbol) contador = 1 else: argumentos = argumentos + ',' + i.getString(entorno, arbol) arbol.addC3D('heap = \'' + 'insert into ' + str(self.identificador) + ' (' + lista_id + ') values (' + argumentos + ');' + '\'') temp = arbol.getTemp() arbol.addC3D(temp + ' = inter()') return temp
def traducir_regla5(self, entorno: Tabla_de_simbolos, arbol: Arbol): Bv = arbol.getLabel() Bf = arbol.getLabel() validacion = str(self.exp.traducir(entorno, arbol)) arbol.addC3D('label ' + Bf) # optimizacion --------------------------- # Regla no.5: original = "if " + validacion + " goto " + str(Bv) + ' goto ' + str(Bf) optimizado = "goto " + str(Bf) reportero = ReporteOptimizacion('Regla 5', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ---------------------------------------------------------------- return
def getValueAbstract(self, entorno: Tabla_de_simbolos, arbol:Arbol): expresion_: Valor = self.exp.getValueAbstract(entorno, arbol) expresion: str = str(expresion_.data) expresion_spliteada = expresion.split("%") if len(expresion_spliteada) == 2: #var = str(self.ID) # <-- aqui va codigo de acceso a la TS para getVar #val:Valor = entorno.obtener_varibale(str(self.ID)) var = str(entorno.obtener_temporal_deVar(str(self.ID))) #var = str(val.data) tmp = arbol.getTemp() arbol.addC3D(tmp + " = " + '\'' + str(expresion_spliteada[0]) + '\' + ' + var + ' + \'' + str(expresion_spliteada[1]) + "\'") return tmp else: # ERROR -> no hay simbolo de acceso para incrustar var pass return ""
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): argumentos, where = '', '' contador = 0 for i in self.columns: if contador == 0: argumentos = argumentos + i.getString(entorno, arbol) contador = 1 else: argumentos = argumentos + ',' + i.getString(entorno, arbol) if self.wherexp is not None: where = ' WHERE ' + self.wherexp.getString(entorno, arbol) arbol.addC3D('heap = \'' + 'UPDATE ' + str(self.id) + ' SET ' + argumentos + where + ';' + '\'') temp = arbol.getTemp() arbol.addC3D(temp + ' = inter()') return temp
def traducir_optimizado(self, entorno: Tabla_de_simbolos, arbol: Arbol): try: if str(self.izq.getString(entorno, arbol)) == '0': derecho = [] derecho.append(self.der.traducir(entorno, arbol)) derecho.append('8-12') return derecho elif str(self.der.getString(entorno, arbol)) == '0': izquierdo = [] izquierdo.append(self.izq.traducir(entorno, arbol)) izquierdo.append('8-12') return izquierdo except: tmp = arbol.getTemp() arbol.addC3D(tmp + " = 0") return tmp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): if self.esNecesarioOptimizar(entorno, arbol): return self.traducir_optimizado(entorno, arbol) izquierdo = self.izq.traducir(entorno, arbol) # <-- tiene un temporal derecho = self.der.traducir(entorno, arbol) # <-- tiene un temporal if izquierdo == 0: '''ERROR SEMANTICO NO SE PUEDE DIVIDIR ENTRE 0''' else: if self.analizar_semanticamente(entorno, arbol) == 0: tmp = arbol.getTemp() arbol.addC3D(tmp + " = int(" + izquierdo + ") / int(" + derecho + ")") return tmp elif self.analizar_semanticamente(entorno, arbol) == 1: tmp = arbol.getTemp() arbol.addC3D(tmp + " = float(" + izquierdo + ") / float(" + derecho + ")") return tmp elif self.analizar_semanticamente(entorno, arbol) == 2: tmp = arbol.getTemp() #ERROR SEMANTICO DE TIPOS NO SE PUEDEN OPERAR TIPOS CADENA return tmp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): try: if self.exp == None: arbol.addC3D("return") else: tmp = self.exp.traducir(entorno, arbol) arbol.addC3D("return " + str(tmp) + "") except: arbol.addC3D("return") return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): try: izquierdo = self.izq.traducir(entorno, arbol) # <-- tiene un temporal tmp = arbol.getTemp() arbol.addC3D(tmp + " = " + str(self.tipo) + str(izquierdo)) return tmp except: tmp = arbol.getTemp() arbol.addC3D(tmp + " = 0") return tmp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): try: if self.exp == None: arbol.addC3D("return") else: try: tmp = self.exp.traducir(entorno, arbol) arbol.addC3D("return " + str(tmp) + "") except: arbol.addC3D("return") descripcion = 'Expresion invalida para el retorn' reportero = ErroresSemanticos(descripcion, str(self.linea), str(self.columna), 'retorn') arbol.ErroresSemanticos.append(reportero) except: arbol.addC3D("return") descripcion = 'Expresion invalida para el retorn' reportero = ErroresSemanticos(descripcion, str(self.linea), str(self.columna), 'retorn') arbol.ErroresSemanticos.append(reportero) return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): arbol.addC3D('heap = ' + '\'' + str(self.string) + ';\'') temp = arbol.getTemp() arbol.addC3D(temp + ' = inter()') return temp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): if self.esNecesarioOptimizar(entorno, arbol): return self.traducir_optimizado(entorno, arbol) izquierdo = self.izq.traducir(entorno, arbol) # <-- tiene un temporal derecho = self.der.traducir(entorno, arbol) # <-- tiene un temporal try: if self.analizar_semanticamente(entorno, arbol) == 0: tmp = arbol.getTemp() arbol.addC3D(tmp + " = int(" + izquierdo + ") + int(" + derecho + ")") return tmp elif self.analizar_semanticamente(entorno, arbol) == 1: tmp = arbol.getTemp() arbol.addC3D(tmp + " = float(" + izquierdo + ") + float(" + derecho + ")") return tmp elif self.analizar_semanticamente(entorno, arbol) == 2: tmp = arbol.getTemp() arbol.addC3D(tmp + " = str(" + izquierdo + ") + str(" + derecho + ")") return tmp else: tmp = arbol.getTemp() arbol.addC3D(tmp + " = str(" + izquierdo + ") + str(" + derecho + ")") return tmp except: tmp = arbol.getTemp() arbol.addC3D(tmp + " = str(" + izquierdo + ") + str(" + derecho + ")") return tmp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): # Se genera la etiqueta de inicio, final, temporal y la variable de expresion si la hay startlabel = arbol.getLabel() finallabel = arbol.getLabel() templabel = arbol.getLabel() var_add = " " # Si el case trae una expresion if self.expression is not None: # Se obtiene la variable y se genera la comparacion var_add = " " + self.expression.traducir(entorno, arbol) + " = " # Se asigna la expresion al siguiente caso del case si no es un else if isinstance(self.elsecase, WhenElse) or isinstance( self.elsecase, When): self.elsecase.expression = self.expression arbol.addC3D("if" + var_add + self.condition.traducir(entorno, arbol) + " goto " + str(startlabel)) arbol.addC3D("goto " + finallabel) arbol.addC3D(startlabel + ":") arbol.addIdentacion() for instruction in self.instructions: instruction.traducir(entorno, arbol) arbol.popIdentacion() arbol.addC3D("goto " + templabel) arbol.addC3D(finallabel + ":") if isinstance(self.elsecase, WhenElse) or isinstance( self.elsecase, When): arbol.addC3D(self.elsecase.traducir(entorno, arbol)) else: for ins in self.elsecase: ins.traducir(entorno, arbol) arbol.addC3D(templabel + ":")
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): # REGLA 4 if self.exp.validador_Regla4(entorno, Arbol): return self.traducir_regla4(entorno, arbol) # REGLA 5 if self.exp.validador_Regla5(entorno, Arbol): return self.traducir_regla5(entorno, arbol) Bv = arbol.getLabel() Bf = arbol.getLabel() validacion = str(self.exp.traducir(entorno, arbol)) Btemporal = arbol.getLabel() arbol.addC3D("if " + validacion + ':') arbol.addIdentacion() arbol.addC3D("goto " + str(Bv)) arbol.popIdentacion() arbol.addC3D('else:') arbol.addIdentacion() arbol.addC3D("goto " + Bf) arbol.popIdentacion() arbol.addC3D('label .' + Bv) for item in self.body: item.traducir(entorno, arbol) arbol.addC3D("goto " + Btemporal) arbol.addC3D('label .' + Bf) for item in self.contrabody: item.traducir(entorno, arbol) arbol.addC3D('label .' + Btemporal) # optimizacion --------------------------- # Regla no.3: original = "if " + validacion + " goto " + str(Bv) + ' goto ' + str(Bf) optimizado = "if " + validacion + " goto " + str(Bf) reportero = ReporteOptimizacion('Regla 3', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ---------------------------------------------------------------- return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): temp = arbol.getTemp() arbol.addC3D(temp + " = " + '\'' + str(self.data) + '\'') return temp
def traducir(self, entorno: Tabla_de_simbolos, arbol:Arbol): if self.exp != None: expres = self.exp.traducir(entorno, arbol) # modulo de insercion a TS try: val_exp = self.exp.getValueAbstract(entorno, arbol) simbol: Simbolo = Simbolo(str(self.identificador), val_exp.tipo, val_exp) if str(expres[1]) == '8-12' or str(expres[1]) == '9-13' or str(expres[1]) == '10-14' or \ str(expres[1]) == '11-15' or str(expres[1]) == '16' or str(expres[1]) == '17' \ or str(expres[1]) == '18': simbol.setTemp(str(expres[0])) else: simbol.setTemp(str(expres)) entorno.insertar_variable(simbol) except: val_exp = Valor(2, 'DML') simbol: Simbolo = Simbolo(str(self.identificador), val_exp.tipo, val_exp) if str(expres[1]) == '8-12' or str(expres[1]) == '9-13' or str(expres[1]) == '10-14' or \ str(expres[1]) == '11-15' or str(expres[1]) == '16' or str(expres[1]) == '17' \ or str(expres[1]) == '18': simbol.setTemp(str(expres[0])) else: simbol.setTemp(str(expres)) entorno.insertar_variable(simbol) # --> # Modulo de rporteria: reportero = ReporteTS(str(self.identificador), str(self.identificador), 'Variable', 'N/A', str(self.linea), str(self.columna)) arbol.ReporteTS.append(reportero) # --> # ---------------------------------------------------------------------------------- # modulo de insercion a TS optimizado try: tmp = str(self.identificador) if str(expres[1]) == '8-12': # Regla no. 9 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' + 0' optimizado = str(tmp) + ' = ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 12', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '9-13': # Regla no. 9 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' - 0' optimizado = str(tmp) + ' = ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 13', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '10-14': # Regla no. 9 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 1' optimizado = str(tmp) + ' = ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 14', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '11-15': # Regla no. 9 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' / 1' optimizado = str(tmp) + ' = ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 15', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '16': # Regla no. 16 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 2' optimizado = str(tmp) + ' = ' + str(expres[0]) + ' + ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 16', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '17': # Regla no. 17 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 0' optimizado = str(tmp) + ' = 0' reportero = ReporteOptimizacion('Regla 17', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '18': # Regla no. 18 ----------------------------- original = str(tmp) + ' = 0 / ' + str(expres[0]) optimizado = str(tmp) + ' = 0' reportero = ReporteOptimizacion('Regla 18', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return except: pass # ---------------------------------------------------------------------------------- else: tmp = arbol.getTemp() arbol.addC3D(tmp + " = '' #" + str(self.identificador)) val_exp:Valor = Valor(2, 'NULL') simbol:Simbolo = Simbolo(str(self.identificador), val_exp.tipo, val_exp) simbol.setTemp(str(tmp)) entorno.insertar_variable(simbol) # --> # Modulo de rporteria: reportero = ReporteTS(str(self.identificador), str(self.identificador), 'Variable', 'N/A', str(self.linea), str(self.columna)) arbol.ReporteTS.append(reportero) # --> #self.analizar_semanticamente(entorno, arbol) return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): # codigo soporte para TS if arbol.existFun(str(self.header.getID())): nota = 'Funcion ' + str( self.header.getID()) + ' ya existe, no soporte a sobrecarga' reportero = ErroresSemanticos(nota, self.linea, self.columna, 'funexecute') arbol.ErroresSemanticos.append(reportero) return reportero = ReporteTS_forFunction(str(self.header.getID()), str(self.header.getID()), 'Funcion', 'ACTIVO', str(self.linea), str(self.columna)) arbol.ReporteTS_Funciones.append(reportero) # ------------------------------- arbol.switchC3Dfunciones() argumentos_puros = self.header.getArgumentos() arbol.addC3D("def " + str(self.header.getID()) + "(" + str(argumentos_puros) + "):") arbol.resetIdentacion_funciones() arbol.addIdentacion() self.header.traducir(entorno, arbol) if self.stmt_declare == None: pass else: for item in self.stmt_declare: item.traducir(entorno, arbol) for item in self.stmt_body: item.traducir(entorno, arbol) arbol.resetIdentacion_funciones() arbol.switchC3Dmain() return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): arbol.addC3D('heap = ' + '\'' + 'SHOW DATABASES;\'') temp = arbol.getTemp() arbol.addC3D(temp + ' = inter()') return temp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): # Se genera la etiqueta de inicio, final y la variable de expresion si la hay startlabel = '.' + str(arbol.getLabel()) finallabel = '.' + str(arbol.getLabel()) var_add = " " # Si el case trae una expresion if self.expression is not None: # Se obtiene la variable y se genera la comparacion var_add = " " + self.expression.traducir(entorno, arbol) + " = " arbol.addC3D("if" + var_add + self.condition.traducir(entorno, arbol) + " goto " + str(startlabel)) arbol.addC3D("goto " + finallabel) arbol.addC3D('label ' + startlabel) arbol.addIdentacion() for instruction in self.instructions: instruction.traducir(entorno, arbol) arbol.popIdentacion() arbol.addC3D('label ' + finallabel)
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): try: izquierdo = self.izq.traducir(entorno, arbol) # <-- tiene un temporal derecho = self.der.traducir(entorno, arbol) # <-- tiene un temporal if self.analizar_semanticamente(entorno, arbol) == 0: tmp = arbol.getTemp() arbol.addC3D(tmp + " = pow(int(" + izquierdo + ") , int(" + derecho + "))") return tmp elif self.analizar_semanticamente(entorno, arbol) == 1: tmp = arbol.getTemp() arbol.addC3D(tmp + " = pow(int(" + izquierdo + ") , int(" + derecho + "))") return tmp else: # Error de tipos desc = 'Parametros no validos en EXPONENTE' reportero = ErroresSemanticos(desc, str(self.linea), str(self.columna), 'EXPONENTE') arbol.ErroresSemanticos.append(reportero) # ------------------------------------------------------------- tmp = arbol.getTemp() arbol.addC3D(tmp + " = 0") return tmp except: tmp = arbol.getTemp() arbol.addC3D(tmp + " = 0") return tmp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): if entorno.varibaleExiste(str(self.identificador)): pass else: return var_declaracion(str(self.identificador), 0, self.exp, self.linea, self.columna) tmp = entorno.obtener_temporal_deVar(str(self.identificador)) #val:Valor = self.exp.getValueAbstract(entorno, arbol) expres = self.exp.traducir(entorno, arbol) try: if str(tmp) == str(expres[0]): if str(expres[1]) == '8-12': # Regla no. 8 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' + 0' optimizado = 'Se elimina la instruccion' reportero = ReporteOptimizacion('Regla 8', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '9-13': # Regla no. 8 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' - 0' optimizado = 'Se elimina la instruccion' reportero = ReporteOptimizacion('Regla 9', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '10-14': # Regla no. 8 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 1' optimizado = 'Se elimina la instruccion' reportero = ReporteOptimizacion('Regla 10', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '11-15': # Regla no. 11 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' / 1' optimizado = 'Se elimina la instruccion' reportero = ReporteOptimizacion('Regla 11', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '16': arbol.addC3D(tmp + ' = ' + str(expres[0]) + ' + ' + str(expres[0])) # Regla no. 16 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 2' optimizado = str(tmp) + ' = ' + str( expres[0]) + ' + ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 16', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '17': arbol.addC3D(tmp + ' = 0') # Regla no. 17 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 0' optimizado = str(tmp) + ' = 0' reportero = ReporteOptimizacion('Regla 17', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '18': arbol.addC3D(tmp + ' = 0') # Regla no. 18 ----------------------------- original = str(tmp) + ' = 0 / ' + str(expres[0]) optimizado = str(tmp) + ' = 0' reportero = ReporteOptimizacion('Regla 18', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '8-12': arbol.addC3D(tmp + ' = ' + str(expres[0])) # Regla no. 9 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' + 0' optimizado = str(tmp) + ' = ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 12', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '9-13': arbol.addC3D(tmp + ' = ' + str(expres[0])) # Regla no. 9 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' - 0' optimizado = str(tmp) + ' = ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 13', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '10-14': arbol.addC3D(tmp + ' = ' + str(expres[0])) # Regla no. 9 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 1' optimizado = str(tmp) + ' = ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 14', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '11-15': arbol.addC3D(tmp + ' = ' + str(expres[0])) # Regla no. 9 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' / 1' optimizado = str(tmp) + ' = ' + str(expres[0]) reportero = ReporteOptimizacion('Regla 15', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '16': arbol.addC3D(tmp + ' = ' + str(expres[0]) + ' + ' + str(expres[0])) # Regla no. 16 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 2' optimizado = str(tmp) + ' = ' + str(expres[0]) + ' + ' + str( expres[0]) reportero = ReporteOptimizacion('Regla 16', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '17': arbol.addC3D(tmp + ' = 0') # Regla no. 17 ----------------------------- original = str(tmp) + ' = ' + str(expres[0]) + ' * 0' optimizado = str(tmp) + ' = 0' reportero = ReporteOptimizacion('Regla 17', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return elif str(expres[1]) == '18': arbol.addC3D(tmp + ' = 0') # Regla no. 18 ----------------------------- original = str(tmp) + ' = 0 / ' + str(expres[0]) optimizado = str(tmp) + ' = 0' reportero = ReporteOptimizacion('Regla 18', original, optimizado, str(self.linea), str(self.columna)) arbol.ReporteOptimizacion.append(reportero) # ----------------------------------------------------------------------------- return except: pass arbol.addC3D(tmp + ' = ' + expres) '''if str(val.tipo) == '2': arbol.addC3D(tmp + ' = ' + '\'' + str(val.data) + '\'') else: arbol.addC3D(tmp + ' = ' + str(val.data))''' return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): tmp = self.getValueAbstract(entorno, arbol) arbol.addC3D("print(str(" + str(tmp) + "))") return
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): arbol.addC3D('heap = ' + '\'' + 'DROP TABLE ' + str(self.identificador) + ';\'') temp = arbol.getTemp() arbol.addC3D(temp + ' = inter()') return temp
def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol): tmp = self.exp.traducir(entorno, arbol) #temp = arbol.getTemp() #arbol.addC3D(temp + " = " + '\'' + str(value) + '\'') arbol.addC3D("print(str(" + str(tmp) + "))") return
def traducir(self, entorno: Tabla_de_simbolos, arbol:Arbol): try: if self.esNecesarioOptimizar(entorno, arbol): return self.traducir_optimizado(entorno, arbol) izquierdo = self.izq.traducir(entorno, arbol) # <-- tiene un temporal derecho = self.der.traducir(entorno, arbol) # <-- tiene un temporal try: if self.analizar_semanticamente(entorno, arbol) == 0: tmp = arbol.getTemp() arbol.addC3D(tmp + " = int(" + izquierdo + ") + int(" + derecho + ")") return tmp elif self.analizar_semanticamente(entorno, arbol) == 1: tmp = arbol.getTemp() arbol.addC3D(tmp + " = float(" + izquierdo + ") + float(" + derecho + ")") return tmp elif self.analizar_semanticamente(entorno, arbol) == 2: tmp = arbol.getTemp() arbol.addC3D(tmp + " = str(" + izquierdo + ") + str(" + derecho + ")") return tmp else: # Error de tipos desc = 'Parametros no validos en SUMA' reportero = ErroresSemanticos(desc, str(self.linea), str(self.columna), 'SUMA') arbol.ErroresSemanticos.append(reportero) # ------------------------------------------------------------- tmp = arbol.getTemp() arbol.addC3D(tmp + " = str(" + izquierdo + ") + str(" + derecho + ")") return tmp except: tmp = arbol.getTemp() arbol.addC3D(tmp + " = str(" + izquierdo + ") + str(" + derecho + ")") return tmp except: tmp = arbol.getTemp() arbol.addC3D(tmp + " = 0") return tmp