예제 #1
0
    def traducir(self, tabla, arbol):
        super().traducir(tabla,arbol)
        retorno = Nodo3D()
        if self.tipo.tipo == Tipo_Dato.BOOLEAN:
            if self.valor:
                retorno.temporalAnterior = "1"
            else:
                retorno.temporalAnterior = "0"
            return retorno

        if self.tipo.tipo == Tipo_Dato.INTEGER:
              retorno.temporalAnterior = str(self.valor)
              return retorno
        if self.tipo.tipo == Tipo_Dato.VARCHAR:
              retorno.temporalAnterior ="'"+ str(self.valor)+"'"
              return retorno
        if self.tipo.tipo == Tipo_Dato.CHAR:
              retorno.temporalAnterior ="'"+ str(self.valor)+"'" 
              return retorno
        if self.tipo.tipo == Tipo_Dato.TEXT:
              retorno.temporalAnterior ="'"+ str(self.valor)+"'" 
              return retorno
        if self.tipo.tipo == Tipo_Dato.DATE:
              retorno.temporalAnterior ="'"+ str(self.valor)+"'"
              return retorno
        if self.tipo.tipo == Tipo_Dato.TIME:
              retorno.temporalAnterior ="'"+ str(self.valor)+"'" 
              return retorno



        retorno.temporalAnterior = str(self.valor)
        return retorno
예제 #2
0
 def traducir(self, tabla, arbol):
     super().traducir(tabla,arbol)
     retorno = Nodo3D()
     funcion = tabla.getSimboloFuncion(self.id)
     arbol.addComen("Simulando el paso de parámetros")
     temporal1 = tabla.getTemporal()
     arbol.addc3d(f"{temporal1} = P + {funcion.tamanio}")
     
     arbol.addComen("Asignación de parámetros")
     if funcion.tipo.tipo == Tipo_Dato.VOID:
         for i in range(0,len(self.lista_expresion)):
             valor = self.lista_expresion[i].traducir(tabla, arbol)
             temporal2 = tabla.getTemporal()
             arbol.addc3d(f"{temporal2} = {temporal1} + {i}")
             arbol.addc3d(f"Pila[{temporal2}] = \"{valor.temporalAnterior}\"")
     else:
         for i in range(0,len(self.lista_expresion)):
             valor = self.lista_expresion[i].traducir(tabla, arbol)
             temporal2 = tabla.getTemporal()
             arbol.addc3d(f"{temporal2} = {temporal1} + {i+1}")
             arbol.addc3d(f"Pila[{temporal2}] = {valor.temporalAnterior}")
     
     temporal3 = tabla.getTemporal()
     temporal4 = tabla.getTemporal()
     arbol.addComen("Cambio de ámbito")
     arbol.addc3d(f"P = P + {funcion.tamanio}")
     arbol.addComen("Llamada a la función")
     arbol.addc3d(f"{self.id}()")
     if funcion.tipo.tipo != Tipo_Dato.VOID:
         arbol.addComen("Posición del return en el ámbito de la función")
         arbol.addc3d(f"{temporal3} = {temporal1} + 0")
         arbol.addc3d(f"{temporal4} = Pila[{temporal3}]")        
     arbol.addc3d(f"P = P - {funcion.tamanio}")
     retorno.temporalAnterior = temporal4
     return retorno
예제 #3
0
    def traducir(self, tabla, arbol):
        super().traducir(tabla, arbol)
        retorno = Nodo3D()
        variable = tabla.getSimboloVariable(self.id)
        arbol.addComen(f"Inicia asignación: {self.id}")
        temporal1 = tabla.getTemporal()
        temporal2 = tabla.getTemporal()
        arbol.addc3d(f"{temporal1} = P + {variable.posicion}")
        arbol.addComen("Se obtiene el valor")

        if isinstance(self.expresion, SelectLista2):
            arbol.addc3d("arbol.expre_query = True")
            exp = self.expresion.c3d(tabla, arbol)
        else:
            exp = self.expresion.traducir(tabla, arbol)

        if variable.tipo.tipo == Tipo_Dato.BOOLEAN and exp.temporalAnterior != "1" and exp.temporalAnterior != "0":
            retorno.imprimirEtiquetDestino(arbol, exp.etiquetaTrue)
            arbol.addc3d(f"{temporal2} = 1")
            etiqueta1 = tabla.getEtiqueta()
            arbol.addc3d(f"goto .{etiqueta1}")
            retorno.imprimirEtiquetDestino(arbol, exp.etiquetaFalse)
            arbol.addc3d(f"{temporal2} = 0")
            arbol.addc3d(f"label .{etiqueta1}")
        else:
            arbol.addc3d(f"{temporal2} = {exp.temporalAnterior}")
        arbol.addc3d(f"Pila[{temporal1}] = {temporal2}")
        if isinstance(self.expresion, SelectLista2):
            arbol.addc3d("arbol.expre_query = False")
        arbol.addComen("Fin Asignación")
예제 #4
0
파일: Elsif.py 프로젝트: sandymerida/tytus
    def traducir(self, tabla: Tabla, arbol: Arbol):
        super().traducir(tabla, arbol)
        retorno = Nodo3D()
        arbol.addc3d("# Inicia Elsif")
        condicion = self.condicion.traducir(tabla, arbol)
        if condicion.temporalAnterior == "0":
            etiqueta1 = tabla.getEtiqueta()
            arbol.addc3d(f"goto .{etiqueta1}")
            condicion.etiquetaTrue = ""
            condicion.etiquetaFalse = etiqueta1
        elif condicion.temporalAnterior == "1":
            etiqueta1 = tabla.getEtiqueta()
            arbol.addc3d(f"goto .{etiqueta1}")
            condicion.etiquetaTrue = etiqueta1
            condicion.etiquetaFalse = ""

        etiquetaFin = tabla.getEtiqueta()
        condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaTrue)
        # Se traducen todas las funciones dentro del if
        for i in self.instrucciones:
            i.traducir(tabla, arbol)
        arbol.addc3d(f"goto .{etiquetaFin}")
        condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaFalse)
        retorno.temporalAnterior = f"label .{etiquetaFin}"
        return retorno
예제 #5
0
파일: Logica.py 프로젝트: sandymerida/tytus
    def concatenar(self, tabla: Tabla, arbol: Arbol):
        super().traducir(tabla,arbol)
        retorno = Nodo3D()
        #print(self.opIzq)
        resultadoIzq = self.opIzq.concatenar(tabla,arbol)
        if self.operador == 'AND':
            if isinstance(resultadoIzq, str):
                cadena = f"{resultadoIzq} AND "
                if isinstance(self.opDer, str):
                    cadena += self.opDer + " "
                else:
                    cadena += self.opDer.concatenar(tabla,arbol)
                return cadena

        elif self.operador == 'OR':
            if isinstance(resultadoIzq, str):
                cadena = f"{resultadoIzq} OR "
                if isinstance(self.opDer, str):
                    cadena += self.opDer + " "
                else:
                    cadena += self.opDer.concatenar(tabla,arbol)
                return cadena
        elif self.operador == 'NOT':
            if isinstance(resultadoIzq, str):
                cadena = f"NOT {resultadoIzq}"
                return cadena
예제 #6
0
 def traducir(self, tabla, arbol):
     super().traducir(tabla, arbol)
     retorno = Nodo3D()
     # Operación con dos operadores
     if (self.opDer != None):
         resultadoIzq = self.opIzq.traducir(tabla, arbol)
         resultadoDer = self.opDer.traducir(tabla, arbol)
         temporal = tabla.getTemporal()
         # Comprobamos el tipo de operador
         if self.operador == '+':
             retorno.temporalAnterior = temporal
             arbol.addc3d(
                 f"{temporal} = {resultadoIzq.temporalAnterior} + {resultadoDer.temporalAnterior}"
             )
             return retorno
         elif self.operador == '-':
             retorno.temporalAnterior = temporal
             arbol.addc3d(
                 f"{temporal} = {resultadoIzq.temporalAnterior} - {resultadoDer.temporalAnterior}"
             )
             return retorno
         elif self.operador == '*':
             retorno.temporalAnterior = temporal
             arbol.addc3d(
                 f"{temporal} = {resultadoIzq.temporalAnterior} * {resultadoDer.temporalAnterior}"
             )
             return retorno
         elif self.operador == '/':
             retorno.temporalAnterior = temporal
             arbol.addc3d(
                 f"{temporal} = {resultadoIzq.temporalAnterior} / {resultadoDer.temporalAnterior}"
             )
             return retorno
         elif self.operador == '^':
             retorno.temporalAnterior = temporal
             arbol.addc3d(
                 f"{temporal} = {resultadoIzq.temporalAnterior} ** {resultadoDer.temporalAnterior}"
             )
             return retorno
         elif self.operador == '%':
             retorno.temporalAnterior = temporal
             arbol.addc3d(
                 f"{temporal} = {resultadoIzq.temporalAnterior} % {resultadoDer.temporalAnterior}"
             )
             return retorno
     # Operación unaria
     else:
         resultadoIzq = self.opIzq.traducir(tabla, arbol)
         if self.operador == '-':
             temporal1 = tabla.getTemporal()
             arbol.addc3d(f"{temporal1} = -1")
             temporal2 = tabla.getTemporal()
             arbol.addc3d(
                 f"{temporal2} = {temporal1} * {resultadoIzq.temporalAnterior}"
             )
             retorno.temporalAnterior = temporal2
             return retorno
예제 #7
0
파일: Case.py 프로젝트: Sohanyuuu/tytus
    def traducir(self, tabla: Tabla, arbol: Arbol):
        super().traducir(tabla, arbol)
        retorno = Nodo3D()
        arbol.addc3d("# Inicia When")

        # Se traducen todas las funciones dentro del case
        for i in self.instrucciones:
            i.traducir(tabla, arbol)

        return
예제 #8
0
    def c3d(self, tabla, arbol):
        retorno = Nodo3D()
        #print("SELECT LISTA")
        cadena = ""
        cadena += "f\"SELECT "

        for query in self.lista:
            if isinstance(query, Llamada):
                cadena += query.concatenar(tabla, arbol)
            elif isinstance(query, Primitivo):
                #print("SELECT",query.traducir(tabla, arbol).temporalAnterior)
                cadena += query.traducir(tabla, arbol).temporalAnterior
            elif isinstance(query, Select):
                cadena += f"{query.traducir(tabla,arbol)}"
            elif isinstance(query, Aritmetica.Aritmetica):
                cadena += f"{query.concatenar(tabla,arbol)}"
            else:
                cadena += f"{query.traducir(tabla,arbol)}"
            if self.lista.index(query) == len(self.lista) - 1:
                cadena += " "
            else:
                cadena += ", "

        cadena += ";\""

        if (arbol.getRelacionales() == False):
            arbol.addComen("Asignar cadena")
            temporal1 = tabla.getTemporal()
            arbol.addc3d(f"{temporal1} = { cadena }")

            arbol.addComen("Entrar al ambito")
            temporal2 = tabla.getTemporal()
            arbol.addc3d(f"{temporal2} = P+2")
            temporal3 = tabla.getTemporal()
            arbol.addComen("parametro 1")
            arbol.addc3d(f"{temporal3} = { temporal2}+1")
            arbol.addComen("Asignacion de parametros")
            arbol.addc3d(f"Pila[{temporal3}] = {temporal1}")

            arbol.addComen("Llamada de funcion")
            arbol.addc3d(f"P = P+2")
            arbol.addc3d(f"funcionintermedia()")

            arbol.addComen("obtener resultado")
            temporalX = tabla.getTemporal()
            arbol.addc3d(f"{temporalX} = P+2")
            temporalR = tabla.getTemporal()
            arbol.addc3d(f"{temporalR} = Pila[{ temporalX }]")

            arbol.addComen("Salida de funcion")
            arbol.addc3d(f"P = P-2")
            retorno.temporalAnterior = temporalR
            return retorno
        else:
            return cadena
예제 #9
0
    def traducir(self, tabla, arbol):
        super().traducir(tabla, arbol)
        variable = tabla.getSimboloVariable(self.id)
        retorno = Nodo3D()
        temporal1 = tabla.getTemporal()
        arbol.addc3d(f"{temporal1} = P + {variable.posicion}")
        temporal2 = tabla.getTemporal()
        arbol.addc3d(f"{temporal2} = Pila[{temporal1}]")
        retorno.temporalAnterior = temporal2

        return retorno
예제 #10
0
파일: Return.py 프로젝트: Sohanyuuu/tytus
 def traducir(self, tabla, arbol):
     super().traducir(tabla, arbol)
     if self.expresion != None:
         arbol.addComen("Se asigna el valor a la posición de return")
         r = tabla.getSimboloVariable("return")
         exp = self.expresion.traducir(tabla, arbol)
         temporal = tabla.getTemporal()
         arbol.addc3d(f"{temporal} = P + {r.posicion}")
         arbol.addc3d(f"Pila[{temporal}] = {exp.temporalAnterior}")
         arbol.addc3d(f"return")
     return Nodo3D()
예제 #11
0
 def traducir(self, tabla, arbol):
     super().traducir(tabla,arbol)
     retorno = Nodo3D()
     if self.tipo.tipo == Tipo_Dato.BOOLEAN:
         if self.valor:
             retorno.temporalAnterior = "1"
         else:
             retorno.temporalAnterior = "0"
         return retorno
     retorno.temporalAnterior = str(self.valor)
     return retorno
예제 #12
0
파일: Trunc.py 프로젝트: sandymerida/tytus
 def traducir(self, tabla, arbol):
     super().traducir(tabla, arbol)
     retorno = Nodo3D()
     resultado = self.valor.traducir(tabla, arbol)
     temporal1 = tabla.getTemporal()
     temporal2 = tabla.getTemporal()
     arbol.addc3d(f"{temporal1} = {resultado.temporalAnterior}")
     arbol.addc3d(f"{temporal2} = math.trunc({temporal1})")
     retorno.temporalAnterior = temporal2
     return retorno
     '''
예제 #13
0
    def traducir(self, tabla, arbol):
        super().traducir(tabla, arbol)
        retorno = Nodo3D()
        temp = self.expresion.traducir(tabla, arbol)
        arbol.addc3d("#Inicio print")
        if (self.expresion.tipo.tipo == Tipo_Dato.SMALLINT
                or self.expresion.tipo.tipo == Tipo_Dato.INTEGER
                or self.expresion.tipo.tipo == Tipo_Dato.BIGINT
                or self.expresion.tipo.tipo == Tipo_Dato.DECIMAL
                or self.expresion.tipo.tipo == Tipo_Dato.NUMERIC
                or self.expresion.tipo.tipo == Tipo_Dato.REAL
                or self.expresion.tipo.tipo == Tipo_Dato.DOUBLE_PRECISION
                or self.expresion.tipo.tipo == Tipo_Dato.MONEY):
            arbol.addc3d(f"print({temp.temporalAnterior})")
        if (self.expresion.tipo.tipo == Tipo_Dato.BOOLEAN):
            temporal1 = tabla.getTemporal()
            if (temp.temporalAnterior == ""):
                etiqueta1 = tabla.getEtiqueta()
                retorno.imprimirEtiquetDestino(arbol, temp.etiquetaTrue)
                arbol.addc3d(f"{temporal1} = 1")
                arbol.addc3d(f"goto .{etiqueta1}")
                retorno.imprimirEtiquetDestino(arbol, temp.etiquetaFalse)
                arbol.addc3d(f"{temporal1} = 0")
                arbol.addc3d(f"goto .{etiqueta1}")
                arbol.addc3d(f"label .{etiqueta1}")
            else:
                arbol.addc3d(f"{temporal1} = {temp.temporalAnterior}")

            etiqueta1 = tabla.getEtiqueta()
            etiqueta2 = tabla.getEtiqueta()
            etiqueta3 = tabla.getEtiqueta()
            arbol.addc3d(f"if({temporal1} == 1):\n\t\tgoto .{etiqueta1}")
            arbol.addc3d(f"goto .{etiqueta2}")
            arbol.addc3d(f"#True")
            arbol.addc3d(f"label .{etiqueta1}")
            arbol.addc3d("print(True)")
            arbol.addc3d(f"goto .{etiqueta3}")

            arbol.addc3d(f"#False")
            arbol.addc3d(f"label .{etiqueta2}")
            arbol.addc3d("print(False)")
            arbol.addc3d(f"label .{etiqueta3}")

        arbol.addc3d("#Fin print")
        return retorno
예제 #14
0
    def traducir(self, tabla, arbol):
        super().traducir(tabla, arbol)
        retorno = Nodo3D()
        exp = self.expresion.traducir(tabla, arbol)
        arbol.addc3d("#Inicio print")
        if self.expresion.tipo.tipo == Tipo_Dato.ID:
            self.expresion.tipo = self.tipo
        if (self.expresion.tipo.tipo == Tipo_Dato.BOOLEAN):
            temporal1 = tabla.getTemporal()
            if (exp.temporalAnterior == ""):
                etiqueta1 = tabla.getEtiqueta()
                retorno.imprimirEtiquetDestino(arbol, exp.etiquetaTrue)
                arbol.addc3d(f"{temporal1} = 1")
                arbol.addc3d(f"goto .{etiqueta1}")
                retorno.imprimirEtiquetDestino(arbol, exp.etiquetaFalse)
                arbol.addc3d(f"{temporal1} = 0")
                arbol.addc3d(f"goto .{etiqueta1}")
                arbol.addc3d(f"label .{etiqueta1}")
            else:
                arbol.addc3d(f"{temporal1} = {exp.temporalAnterior}")

            etiqueta1 = tabla.getEtiqueta()
            etiqueta2 = tabla.getEtiqueta()
            etiqueta3 = tabla.getEtiqueta()
            arbol.addc3d(f"if({temporal1} == 1):\n\t\tgoto .{etiqueta1}")
            arbol.addc3d(f"goto .{etiqueta2}")
            arbol.addc3d(f"#True")
            arbol.addc3d(f"label .{etiqueta1}")
            arbol.addc3d("print(True)")
            arbol.addc3d(f"goto .{etiqueta3}")

            arbol.addc3d(f"#False")
            arbol.addc3d(f"label .{etiqueta2}")
            arbol.addc3d("print(False)")
            arbol.addc3d(f"label .{etiqueta3}")
        else:
            arbol.addc3d(f"print({exp.temporalAnterior})")
        arbol.addc3d("#Fin print")
        return retorno
예제 #15
0
파일: Logica.py 프로젝트: sandymerida/tytus
 def traducir(self, tabla: Tabla, arbol: Arbol):
     super().traducir(tabla,arbol)
     retorno = Nodo3D()
     resultadoIzq = self.opIzq.traducir(tabla,arbol)
     if self.operador == 'AND':
         if isinstance(resultadoIzq, str):
             cadena = f"{resultadoIzq} AND "
             if isinstance(self.opDer, str):
                 cadena += self.opDer + " "
             else:
                 cadena += self.opDer.traducir(tabla,arbol)
             return cadena
         if resultadoIzq.temporalAnterior == "0":
             etiqueta1 = tabla.getEtiqueta()
             arbol.addc3d(f"goto .{etiqueta1}")
             resultadoDer = self.opDer.traducir(tabla,arbol)
             if resultadoDer.temporalAnterior == "0":
                 # False and False
                 etiqueta2 = tabla.getEtiqueta()
                 arbol.addc3d(f"goto .{etiqueta2}")
                 retorno.etiquetaTrue = ''
                 retorno.etiquetaFalse = f"{etiqueta1},{etiqueta2}"
             elif resultadoDer.temporalAnterior == "1":
                 # False and True
                 etiqueta2 = tabla.getEtiqueta()
                 arbol.addc3d(f"goto .{etiqueta2}")
                 arbol.addc3d(f"label .{etiqueta2}")
                 retorno.etiquetaTrue = ""
                 retorno.etiquetaFalse = f"{etiqueta1}"
             else:
                 # False and Operación Relacional
                 retorno.etiquetaTrue = resultadoDer.etiquetaTrue
                 retorno.etiquetaFalse = f"{etiqueta1},{resultadoDer.etiquetaFalse}"
             return retorno
         elif resultadoIzq.temporalAnterior == "1":
             etiqueta1 = tabla.getEtiqueta()
             arbol.addc3d(f"goto .{etiqueta1}")
             arbol.addc3d(f"label .{etiqueta1}")
             resultadoDer = self.opDer.traducir(tabla,arbol)
             if resultadoDer.temporalAnterior == "0":
                 # True and False
                 etiqueta2 = tabla.getEtiqueta()
                 arbol.addc3d(f"goto .{etiqueta2}")
                 retorno.etiquetaTrue = ''
                 retorno.etiquetaFalse = f"{etiqueta2}"
             elif resultadoDer.temporalAnterior == "1":
                 # True and True
                 etiqueta2 = tabla.getEtiqueta()
                 arbol.addc3d(f"goto .{etiqueta2}")
                 retorno.etiquetaTrue = f"{etiqueta2}"
                 retorno.etiquetaFalse = ""
             else:
                 # True and Operación Relacional
                 retorno.etiquetaTrue = resultadoDer.etiquetaTrue
                 retorno.etiquetaFalse = resultadoDer.etiquetaFalse
             return retorno
         
         retorno.imprimirEtiquetDestino(arbol, resultadoIzq.etiquetaTrue)
         resultadoDer = self.opDer.traducir(tabla, arbol)
         if resultadoDer.temporalAnterior == "0":
             # Operación Relacional and False
             etiqueta1 = tabla.getEtiqueta()
             arbol.addc3d(f"goto .{etiqueta1}")
             retorno.etiquetaTrue = ''
             retorno.etiquetaFalse = f"{resultadoIzq.etiquetaFalse},{etiqueta1}"
             return retorno
         elif resultadoDer.temporalAnterior == "1":
             # Operación Relacional and True
             etiqueta1 = tabla.getEtiqueta()
             arbol.addc3d(f"goto .{etiqueta1}")
             retorno.etiquetaTrue = f"{etiqueta1}"
             retorno.etiquetaFalse = f"{resultadoIzq.etiquetaFalse}"
             return retorno
         # Operación Relacional and Operación Relacional
         retorno.etiquetaTrue = resultadoDer.etiquetaTrue
         retorno.etiquetaFalse = f"{resultadoIzq.etiquetaFalse},{resultadoDer.etiquetaFalse}"
         return retorno
     elif self.operador == 'OR':
         if resultadoIzq.temporalAnterior == "0":
             etiqueta1 = tabla.getEtiqueta()
             arbol.addc3d(f"goto .{etiqueta1}")
             arbol.addc3d(f"label .{etiqueta1}")
             resultadoDer = self.opDer.traducir(tabla,arbol)
             if resultadoDer.temporalAnterior == "0":
                 # False or False
                 etiqueta2 = tabla.getEtiqueta()
                 arbol.addc3d(f"goto .{etiqueta2}")
                 retorno.etiquetaTrue = ""
                 retorno.etiquetaFalse = etiqueta2
             elif resultadoDer.temporalAnterior == "1":
                 # False or True
                 etiqueta2 = tabla.getEtiqueta()
                 arbol.addc3d(f"goto .{etiqueta2}")
                 retorno.etiquetaTrue = etiqueta2
                 retorno.etiquetaFalse = ""
             else:
                 # False or Operación Relacional
                 retorno.etiquetaTrue = resultadoDer.etiquetaTrue
                 retorno.etiquetaFalse = resultadoDer.etiquetaFalse
             return retorno
         elif resultadoIzq.temporalAnterior == "1":
             etiqueta1 = tabla.getEtiqueta()
             arbol.addc3d(f"goto .{etiqueta1}")
             resultadoDer = self.opDer.traducir(tabla, arbol)
             if resultadoDer.temporalAnterior == "0":
                 # True or False
                 etiqueta2 = tabla.getEtiqueta()
                 arbol.addc3d(f"goto .{etiqueta2}")
                 retorno.etiquetaTrue = etiqueta1
                 retorno.etiquetaFalse = etiqueta2
             elif resultadoDer.temporalAnterior == "1":
                 # True or True
                 etiqueta2 = tabla.getEtiqueta()
                 arbol.addc3d(f"goto .{etiqueta2}")
                 retorno.etiquetaTrue = f"{etiqueta1},{etiqueta2}"
                 retorno.etiquetaFalse = ""
             else:
                 # True or Operación Relacional
                 retorno.etiquetaTrue = f"{etiqueta1},{resultadoDer.etiquetaTrue}"
                 retorno.etiquetaFalse = resultadoDer.etiquetaFalse
             return retorno
         retorno.imprimirEtiquetDestino(arbol, resultadoIzq.etiquetaFalse)
         resultadoDer = self.opDer.traducir(tabla, arbol)
         if resultadoDer.temporalAnterior == "0":
             # Operación Relacional or False
             etiqueta1 = tabla.getEtiqueta()
             arbol.addc3d(f"goto .{etiqueta1}")
             retorno.etiquetaTrue = resultadoIzq.etiquetaTrue
             retorno.etiquetaFalse = etiqueta1
             return retorno
         elif resultadoDer.temporalAnterior == "1":
             # Operación Relacional or True
             etiqueta1 = tabla.getEtiqueta()
             arbol.addc3d(f"goto .{etiqueta1}")
             retorno.etiquetaTrue = f"{resultadoIzq.etiquetaTrue},{etiqueta1}"
             retorno.etiquetaFalse = ""
             return retorno
         # Operación Relacional or Operación Relacional
         retorno.etiquetaTrue = f"{resultadoIzq.etiquetaTrue},{resultadoDer.etiquetaTrue}"
         retorno.etiquetaFalse = resultadoDer.etiquetaFalse
         return retorno
     elif self.operador == 'NOT':
         if resultadoIzq.temporalAnterior == "0":
             # False
             retorno.temporalAnterior = "1"
         elif resultadoIzq.temporalAnterior == "1":
             # True
             retorno.temporalAnterior = "0"
         else:
             # Operación Relacional
             retorno.etiquetaTrue = resultadoIzq.etiquetaFalse
             retorno.etiquetaFalse = resultadoIzq.etiquetaTrue
         return retorno
예제 #16
0
    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
예제 #17
0
    def traducir(self, tabla: Tabla, arbol: Arbol):
        super().traducir(tabla, arbol)
        retorno = Nodo3D()
        arbol.addc3d("# Inicia If")
        condicion = self.condicion.traducir(tabla, arbol)
        # If
        if len(self.l_if) == 0 and len(self.instrucciones_else) == 0:
            if condicion.temporalAnterior == "0":
                etiqueta1 = tabla.getEtiqueta()
                arbol.addc3d(f"goto .{etiqueta1}")
                condicion.etiquetaTrue = ""
                condicion.etiquetaFalse = etiqueta1
            elif condicion.temporalAnterior == "1":
                etiqueta1 = tabla.getEtiqueta()
                arbol.addc3d(f"goto .{etiqueta1}")
                condicion.etiquetaTrue = etiqueta1
                condicion.etiquetaFalse = ""

            etiquetaFin = tabla.getEtiqueta()
            condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaTrue)
            # Se traducen todas las funciones dentro del if
            for i in self.instrucciones:
                i.traducir(tabla, arbol)
            arbol.addc3d(f"goto .{etiquetaFin}")
            condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaFalse)
            arbol.addc3d(f"label .{etiquetaFin}")
        # If - Else
        elif len(self.l_if) == 0 and len(self.instrucciones_else) != 0:
            if condicion.temporalAnterior == "0":
                etiqueta1 = tabla.getEtiqueta()
                arbol.addc3d(f"goto .{etiqueta1}")
                condicion.etiquetaTrue = ""
                condicion.etiquetaFalse = etiqueta1
            elif condicion.temporalAnterior == "1":
                etiqueta1 = tabla.getEtiqueta()
                arbol.addc3d(f"goto .{etiqueta1}")
                condicion.etiquetaTrue = etiqueta1
                condicion.etiquetaFalse = ""

            etiquetaFin = tabla.getEtiqueta()
            condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaTrue)

            # Se traducen todas las instrucciones dentro del if
            for i in self.instrucciones:
                i.traducir(tabla, arbol)
            arbol.addc3d(f"goto .{etiquetaFin}")
            condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaFalse)

            # Else
            # Se traducen todas las instrucciones dentro del else
            for i in self.instrucciones_else:
                i.traducir(tabla, arbol)

            arbol.addc3d(f"label .{etiquetaFin}")
        # If ... Elsif
        elif len(self.l_if) > 0 and len(self.instrucciones_else) == 0:
            if condicion.temporalAnterior == "0":
                etiqueta1 = tabla.getEtiqueta()
                arbol.addc3d(f"goto .{etiqueta1}")
                condicion.etiquetaTrue = ""
                condicion.etiquetaFalse = etiqueta1
            elif condicion.temporalAnterior == "1":
                etiqueta1 = tabla.getEtiqueta()
                arbol.addc3d(f"goto .{etiqueta1}")
                condicion.etiquetaTrue = etiqueta1
                condicion.etiquetaFalse = ""

            etiquetaFin = tabla.getEtiqueta()
            condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaTrue)

            # Se traducen todas las instrucciones dentro del if
            for i in self.instrucciones:
                i.traducir(tabla, arbol)
            arbol.addc3d(f"goto .{etiquetaFin}")
            condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaFalse)

            # Elsif
            # Se traducen todas las instrucciones dentro del elsif
            etiquetasSalida = []
            for elseif in self.l_if:
                resultado = elseif.traducir(tabla, arbol)
                etiquetasSalida.append(resultado.temporalAnterior)

            arbol.addc3d(f"label .{etiquetaFin}")
            # Se agregan las etiquetas de salida de los elsif
            for etiqueta in etiquetasSalida:
                arbol.addc3d(etiqueta)

        # If ... Elsif ... Else
        elif len(self.l_if) > 0 and len(self.instrucciones_else) > 0:
            if condicion.temporalAnterior == "0":
                etiqueta1 = tabla.getEtiqueta()
                arbol.addc3d(f"goto .{etiqueta1}")
                condicion.etiquetaTrue = ""
                condicion.etiquetaFalse = etiqueta1
            elif condicion.temporalAnterior == "1":
                etiqueta1 = tabla.getEtiqueta()
                arbol.addc3d(f"goto .{etiqueta1}")
                condicion.etiquetaTrue = etiqueta1
                condicion.etiquetaFalse = ""

            etiquetaFin = tabla.getEtiqueta()
            condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaTrue)

            # Se traducen todas las instrucciones dentro del if
            for i in self.instrucciones:
                i.traducir(tabla, arbol)
            arbol.addc3d(f"goto .{etiquetaFin}")
            condicion.imprimirEtiquetDestino(arbol, condicion.etiquetaFalse)

            # Elsif
            # Se traducen todas las instrucciones dentro del elsif
            etiquetasSalida = []
            for elseif in self.l_if:
                resultado = elseif.traducir(tabla, arbol)
                etiquetasSalida.append(resultado.temporalAnterior)

            # Else
            # Se traducen todas las instrucciones dentro del else
            for i in self.instrucciones_else:
                i.traducir(tabla, arbol)

            arbol.addc3d(f"label .{etiquetaFin}")
            # Se agregan las etiquetas de salida de los elsif
            for etiqueta in etiquetasSalida:
                arbol.addc3d(etiqueta)

        return retorno