Ejemplo n.º 1
0
 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 + ":")
Ejemplo n.º 2
0
    def traducir_regla4(self, entorno: Tabla_de_simbolos, arbol: Arbol):
        Bv = arbol.getLabel()
        Bf = arbol.getLabel()
        validacion = str(self.exp.traducir(entorno, arbol))

        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('label .' + Bf)

        # optimizacion ---------------------------
        # Regla no.4:
        original = "if " + validacion + " goto " + str(Bv) + ' goto ' + str(Bf)
        optimizado = "goto " + str(Bv)
        reportero = ReporteOptimizacion('Regla 4', original, optimizado,
                                        str(self.linea), str(self.columna))
        arbol.ReporteOptimizacion.append(reportero)
        # ----------------------------------------------------------------

        return
Ejemplo n.º 3
0
    def traducir(self, entorno: Tabla_de_simbolos, arbol: Arbol):

        # codigo soporte para TS
        if arbol.existFun(str(self.header.getID())):

            if arbol.lFun_isINACTIVE(str(self.header.getID())):
                arbol.covertInactivaeTOactivate(str(self.header.getID()))
                return

            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)

        if self.stmt_body == None:
            arbol.addC3D('pass')
        else:
            for item in self.stmt_body:
                item.traducir(entorno, arbol)

        arbol.resetIdentacion_funciones()
        arbol.switchC3Dmain()

        return
Ejemplo n.º 4
0
 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)