Пример #1
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
        
Пример #2
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")
        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
Пример #3
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