Example #1
0
 def regla17_aplicar(self, instrucciones):
     for x in range(0, len(instrucciones)):
         ins = instrucciones[x]
         if isinstance(ins, AsignacionC3D):
             id = ins.id
             res = ins.getOp1()
             if (isinstance(res, (float, int, str))):
                 pass
             else:
                 op1 = res.id
                 op2 = res.op2
                 operador = res.op1
                 if (id != op1 and operador == "*" and str(op2) == "0"):
                     antes = ins.getC3D()
                     cade = str(ins.id) + " = 0"
                     self.addItemReporte(
                         ErrorOpt(
                             "17",
                             "Simplificacion algebraica: X = Y * 0 ->  X = 0",
                             str(ins.linea), antes, cade))
                 elif id != op2 and operador == "*" and str(op1) == "0":
                     antes = ins.getC3D()
                     cade = str(ins.id) + " = 0"
                     self.addItemReporte(
                         ErrorOpt(
                             "17",
                             "Simplificacion algebraica: X = 0 * Y ->  X = 0",
                             str(ins.linea), antes, cade))
Example #2
0
    def regla7_aplicar(self, instrucciones):
        i = 0
        tam = len(instrucciones)
        band = False
        gotoFinal = ""
        gotoF = None
        ca = ""
        posIns = ""
        while i < tam:
            print(i)
            ins = instrucciones[i]
            if (isinstance(ins, SentenciaIf)):
                gotoV = instrucciones[i + 1]
                posIns = i + 1
                labelV = instrucciones[i + 2]
                if (isinstance(labelV, GotoC3D)):
                    return ""

                if (isinstance(labelV, LabelC3D)):
                    if (isinstance(gotoV, GotoC3D)):
                        band = True
                        i += 2

            if (band == True):
                if (isinstance(ins, LabelC3D)):
                    gotoV.valor = ins.valor
                    instrucciones[posIns] = gotoV
                    self.addItemReporte(
                        ErrorOpt("7", "goto Lv <instrucciones> Lv: goto Lf",
                                 ins.linea, ca, "se remplaza el goto;"))
                    band = False

            i += 1
Example #3
0
 def regla5_aplicar(self, instrucciones):
     i = 0
     tam = len(instrucciones)
     band = False
     gotoFinal = ""
     ca = ""
     while i < tam:
         print(i)
         ins = instrucciones[i]
         posIns = i
         if (isinstance(ins, SentenciaIf)):
             gotoV = instrucciones[i + 1]
             posV = i + 1
             gotoF = instrucciones[i + 2]
             posF = i - 1
             if (isinstance(gotoV, GotoC3D) and isinstance(gotoF, GotoC3D)):
                 if (ins.opIzq != ins.opDer and ins.relacional == "<>"):
                     instrucciones.pop(posF)
                     instrucciones.pop(posIns)
                     tam = tam - 2
                     self.addItemReporte(
                         ErrorOpt(
                             "5",
                             " if <cond> goto Lv; goto Lf; --> goto Lf",
                             ins.linea, ca, "se elimina el if y goto lv"))
                 #i+=2
         i += 1
Example #4
0
    def regla3_aplicar(self, instrucciones):
        i = 0
        tam = len(instrucciones)
        band = False
        gotoFinal = ""
        listaDeInstrucciones = []
        ca = ""
        posV = ""
        posF = ""
        posLV = ""
        while i < tam:
            print(i)
            ins = instrucciones[i]

            if (band == True):
                listaDeInstrucciones.append(ins)
                print("agregar regla")

            if (isinstance(ins, SentenciaIf) and tam > i + 3):
                gotoV = instrucciones[i + 1]
                posV = i + 1
                gotoF = instrucciones[i + 2]
                posF = i + 2
                LV = instrucciones[i + 3]
                posLV = i + 3
                if (isinstance(gotoV, GotoC3D) and isinstance(gotoF, GotoC3D)
                        and isinstance(LV, LabelC3D)):
                    if (gotoV.valor == LV.valor):
                        i += 3
                        band = True
                        gotoFinal = gotoF

            if (isinstance(ins, LabelC3D) and band == True):
                LF = instrucciones[i]
                if (LF.valor == gotoFinal.valor):
                    #aqui tenemos que cambiar el simbolo
                    #"aqui vamos a cambiar el goto"
                    #"aqui vamos a eliminar"
                    instrucciones.pop(posLV)
                    instrucciones.pop(posV)
                    self.addItemReporte(
                        ErrorOpt(
                            "3",
                            " if <cond> goto Lv; goto Lf; Lv: <instrucciones> Lf",
                            ins.linea, ca,
                            "if (!<cond>) goto Lf; <instrucciones> Lf: "))
                    band = False
                    tam = tam - 2
            i += 1
Example #5
0
 def regla1_aplicar(self, instrucciones):
     i = 0
     tam = len(instrucciones)
     tam = tam - 1
     while i < tam:
         ins = instrucciones[i]
         ins2 = instrucciones[i + 1]
         if isinstance(ins, AsignacionC3D) and isinstance(
                 ins2, AsignacionC3D):
             if (ins.id[0] == ins2.op1):
                 if (ins.op1 == ins2.id[0]):
                     y = i + 1
                     print("aqui se optimizo Regla 1")
                     self.addItemReporte(
                         ErrorOpt("1",
                                  "t2 = b;  b = t2; -> t2 = b;", ins.linea,
                                  ins.getC3D(), "eliminaciĆ³n duplicada"))
                     instrucciones.pop(y)
                     tam -= 1
                     i += 1
         i += 1
Example #6
0
 def regla11_aplicar(self, instrucciones):
     y = 0
     for x in range(0, len(instrucciones)):
         ins = instrucciones[y]
         if isinstance(ins, AsignacionC3D):
             id = ins.id[0]
             res = ins.getOp1()
             if (isinstance(res, (float, int, str))):
                 pass
             else:
                 op1 = res.id
                 op2 = res.op2
                 operador = res.op1
                 if (id == op1 and str(op2) == "1" and operador == "/"):
                     self.addItemReporte(
                         ErrorOpt("11",
                                  "Simplificacion algebraica: X = X / 1",
                                  str(ins.linea), ins.getC3D(),
                                  "Instruccion eliminada"))
                     instrucciones.pop(x)
                     y -= 1
         y += 1
Example #7
0
    def regla6_aplicar(self, instrucciones):
        i = 0
        tam = len(instrucciones)
        bandG = False
        labelG = False
        gotoFinal = ""
        ApuntadorLabelG = ""
        ApuntadorBandera = ""
        ca = ""
        while i < tam:
            print(i)
            ins = instrucciones[i]

            if (bandG == True):
                print("agregar instruccion")

            if (isinstance(ins, LabelC3D)):
                labelG = True
                ApuntadorLabelG = ins.valor

            if (isinstance(ins, GotoC3D)):
                if (bandG == False):
                    bandG = True
                    ApuntadorBandera = ins
                    posiBandera = i
                    print("agregar instruccion")
                else:
                    if (bandG == True and labelG == True):
                        print("aqui se va a cambiar")
                        if (ApuntadorBandera):
                            ApuntadorBandera.valor = ins.valor
                            instrucciones[posiBandera] = ApuntadorBandera
                            self.addItemReporte(
                                ErrorOpt(
                                    "6", "goto Lv <instrucciones> Lv: goto Lf",
                                    ins.linea, ca,
                                    "se remplaza goto Lv por goto LF;"))
            i += 1
Example #8
0
    def regla2_aplicar(self, instrucciones):
        i = 1
        tam = len(instrucciones)
        regla2 = ""
        activada = False
        listaTemporal = []
        listaActual = ""
        while i < tam:
            ins = instrucciones[i - 1]
            ins2 = instrucciones[i]
            if isinstance(ins, GotoC3D):
                regla2 = ins.valor
                activada = True

            if (activada == True):
                if isinstance(ins2, LabelC3D):
                    if (regla2 == ins2.valor):
                        print("aqui se optimizo regla 2")
                        s = i - 1
                        listaTemporal.append(s)
                        ca = "se elimino la linea del Goto y la de instrucciones"
                        for k in reversed(range(0, len(listaTemporal))):
                            z = listaTemporal[k]
                            instrucciones.pop(z)
                            tam = tam - 1
                        self.addItemReporte(
                            ErrorOpt(
                                "2",
                                "goto .L1 <instrucciones> label .L1  --> label .L1",
                                ins.linea, ca,
                                "eliminaciĆ³n de goto e instrucciones"))
                        activada = False
                else:
                    s = i - 1
                    listaTemporal.append(s)
                    print("aqui agregamos a lista")
            i += 1
Example #9
0
 def regla8_aplicar(self, instrucciones):
     y = 0
     for x in range(0, len(instrucciones)):
         ins = instrucciones[y]
         if isinstance(ins, AsignacionC3D):
             id = ins.id[0]
             res = ins.getOp1()
             if (isinstance(res, (float, int, str))):
                 pass
             else:
                 op1 = res.id
                 op2 = res.op2
                 operador = res.op1
                 if (id == op1 and operador == "+"
                         and op2 == 0) or (id == op2 and operador == "+"
                                           and op1 == 0):
                     self.addItemReporte(
                         ErrorOpt("8",
                                  "Simplificacion algebraica: X = X + 0",
                                  ins.linea, ins.getC3D(),
                                  "Instruccion eliminada"))
                     self.instrucciones.pop(y)
                     y -= 1
         y += 1