Esempio n. 1
0
def putVarValues(entry: str, environment: Ambito):
    variables = environment.getAllVarIds()
    temp = None
    entry_lower = entry.lower()
    for variable in variables:
        print(f"variable: {variable}")
        if variable in entry_lower:
            split = entry_lower.split(variable)
            newValue = environment.getVar(variable)
            temp = ThreeAddressCode().newTemp()
            newString = ''
            #OBTENIENDO VALOR Y PASARLO A UN TEMPORAL
            ThreeAddressCode().addCode(f"{temp} = Stack[{newValue.position}]")
            temp_ant = ''
            for idx, val in enumerate(split):

                if (idx < len(split) - 1):
                    temp_ant = temp
                    temp = ThreeAddressCode().newTemp()
                    ThreeAddressCode().addCode(
                        f"{temp} = \"{val}\" + str({temp_ant})")
                    newString += f"{val}{newValue.value}"

            temp_ant = temp
            temp = ThreeAddressCode().newTemp()
            ThreeAddressCode().addCode(f"{temp} = {temp_ant} + \";\"")
            newString += ';'

            print("valor nuevo string: ", newString)

    if temp is None: return entry
    else: return temp
Esempio n. 2
0
    def print(self, environment):
        if ThreeAddressCode().searchFunction(self.id):
            return None

        ThreeAddressCode().newFunction(self.id)
        newAmbito = Ambito(environment)
        pos = 0
        var_array = []
        for var in self.params:
            pos = ThreeAddressCode().stackCounter
            var_array.append(newAmbito.addVar(var.id, var.data_type, None,
                                              pos, var.line, var.column))
            ThreeAddressCode().incStackCounter()

        pos = ThreeAddressCode().stackCounter
        #Generando etiqueta de salida para la funcion
        lbl_exit = ThreeAddressCode().newLabel()
        newAmbito.lbl_return = lbl_exit

        #Agregando cuerpo de la funcion
        self.body.compile(newAmbito)
        # Agregando etiqueta de salida
        ThreeAddressCode().addCode(f"label .{lbl_exit}")  
        # Imprime primera variable declarada, NO parametro
        # ThreeAddressCode().addCode(f"print(Stack[{pos}])")

        ThreeAddressCode().createFunction(self.id, self.params, var_array)
        return var_array
Esempio n. 3
0
    def compile(self, environment: Ambito):
        var_search = environment.getVar(self.id)

        if isinstance(self.value, Select):
            val = self.value.compile(environment)
            ThreeAddressCode().addCode(f"Stack[{var_search.position}] = {val}")
            return
        else:
            val = self.value.compile(environment)

        if var_search == None:
            print("VARIABLE NO DECLARADA ")
            print(self.id)
            return

        if isinstance(self.value, ObjectReference): #Buscar variable
            val = self.value.compile(environment)
            if isinstance(val, PrimitiveData):
                ThreeAddressCode().addCode(f"Stack[{var_search.position}] = {val.alias}")
                return
                
            val = environment.getVar(val)
            if val is None: 
                print("VARIABLE NO DECLARADA")
                return

            position = val.position
            temporal = ThreeAddressCode().newTemp()
            ThreeAddressCode().addCode(f"{temporal} = Stack[{position}]")
            ThreeAddressCode().addCode(f"Stack[{var_search.position}] = {temporal}")
        else:
            ThreeAddressCode().addCode(f"Stack[{var_search.position}] = {val.value}")
Esempio n. 4
0
 def compile(self, environment):
     newAmbito = Ambito(environment)
     pos = 0
     for var in self.params:
         pos = ThreeAddressCode().stackCounter
         newAmbito.addVar(var.id, var.data_type, None, pos, var.line, var.column)
     lbl_exit = ThreeAddressCode().newLabel()
     self.body.compile(newAmbito)
     
     ThreeAddressCode().addCode(f"label .{lbl_exit}") #Agregando etiqueta
     ThreeAddressCode().addCode(f"print(\"GraciasDios, SALI\")")
Esempio n. 5
0
    def generar_tac(self):
        global report_error
        global report_ast

        DataWindow().clearConsole()
        SymbolTable().destroy()
        ThreeAddressCode().destroy()

        texto = self.entrada.get('1.0', END)
        result = parse(texto)
        print(result)  # Imprime el AST
        report_error = ReportError()

        if len(ErrorController().getList()) > 0:
            messagebox.showerror('ERRORES', 'Se encontraron errores')
        else:

            ambito = Ambito(None)
            for inst in result:
                inst.compile(ambito)

            DataWindow().consoleText(ThreeAddressCode().getCode())
            ThreeAddressCode().writeFile()

            result2 = parse2(texto)  #AST GRAFICO
            report_ast = result2
Esempio n. 6
0
    def compile(self, environment: Ambito):
        var_search = environment.getVar(self.id)
        val = self.value.compile(environment)

        if var_search == None:
            print("VARIABLE NO DECLARADA " + self.id)
            return

        if isinstance(self.value, ObjectReference):  #Buscar variable
            # val = self.value.compile(environment)
            # val = environment.getVar(val)
            # if val is None:
            #     print("VARIABLE NO DECLARADA")
            #     return

            # position = val.position
            # temporal = ThreeAddressCode().newTemp()
            # ThreeAddressCode().addCode(f"{temporal} = Stack[{position}]")
            ThreeAddressCode().addCode(
                f"Stack[{var_search.position}] = {temporal}")
        else:
            print(self.value)
            ThreeAddressCode().addCode(
                f"Stack[{var_search.position}] = {val.value}")
Esempio n. 7
0
def putVarValues(entry: str, temps_array: list, environment: Ambito):
    # entry = entry.replace("))", ")")
    variables = environment.getAllVarIds()
    temp = None
    entry_lower = entry
    diccionario = {}
    hay_variables = False
    for variable in variables:

        if variable in entry_lower:
            first_letter = entry_lower.index(variable)
            next_last_letter = first_letter + len(variable)

            if first_letter - 1 > 0:
                if entry_lower[first_letter - 1].isalpha():
                    continue
            if next_last_letter < len(entry_lower):
                if entry_lower[next_last_letter].isalpha():
                    continue
            hay_variables = True
            print(f"variable: {variable}")
            newValue = environment.getVar(variable)
            #OBTENIENDO VALOR Y PASARLO A UN TEMPORAL
            temp = ThreeAddressCode().newTemp()
            ThreeAddressCode().addCode(f"{temp} = Stack[{newValue.position}]")
            diccionario[variable] = temp

    if hay_variables:
        contador_temporales = ThreeAddressCode().tempCounter
        for variable in variables:
            cambiarVariable(variable, entry_lower, diccionario)
        numbers = int(temp[1:])
        temp = f"t{numbers+1}"
        for r in range(contador_temporales,
                       ThreeAddressCode().tempCounter - 1):
            temp_ant = temp
            temp = ThreeAddressCode().newTemp()
            if temp_ant is None:
                ThreeAddressCode().addCode(f"{temp} = t{r} + t{r+1}")
            else:
                ThreeAddressCode().addCode(f"{temp} = {temp_ant} + t{r+1}")

        if temp is None:
            dif = ThreeAddressCode().tempCounter - contador_temporales
            if dif > 0:
                temp = f"t{contador_temporales + 1}"

    if len(temps_array) > 0:
        funciones = Procedures().getProceduresIDs()
        contador_funciones = 0
        for func in funciones:
            print(f"funcion: {func}")
            # func = func.lower()

            if func in entry_lower:
                contador_temporales = ThreeAddressCode().tempCounter
                #separando string
                contador_funciones = hacerUnSoloCambio(func, entry_lower,
                                                       temps_array,
                                                       contador_funciones)

                for r in range(contador_temporales,
                               ThreeAddressCode().tempCounter - 1):
                    temp_ant = temp
                    temp = ThreeAddressCode().newTemp()
                    if temp_ant is None:
                        ThreeAddressCode().addCode(f"{temp} = t{r} + t{r+1}")
                    else:
                        ThreeAddressCode().addCode(
                            f"{temp} = {temp_ant} + t{r+1}")

                if temp is None:
                    dif = ThreeAddressCode().tempCounter - contador_temporales
                    if dif > 0:
                        temp = f"t{contador_temporales + 1}"

    if temp is None: return entry
    else: return temp
Esempio n. 8
0
def putVarValues(entry: str, temps_array: [], environment: Ambito):
    entry = entry.replace("))", ")")
    variables = environment.getAllVarIds()
    temp = None
    entry_lower = entry
    for variable in variables:
        print(f"variable: {variable}")
        # variable = f" {variable} "
        if variable in entry_lower:
            first_letter = entry_lower.index(variable)
            next_last_letter = first_letter + len(variable)

            if first_letter - 1 > 0:
                if entry_lower[first_letter - 1].isalpha():
                    continue
            if next_last_letter < len(entry_lower):
                if entry_lower[next_last_letter].isalpha():
                    continue

            split = entry_lower.split(variable)
            newValue = environment.getVar(variable)
            temp = ThreeAddressCode().newTemp()
            newString = ''
            #OBTENIENDO VALOR Y PASARLO A UN TEMPORAL
            ThreeAddressCode().addCode(f"{temp} = Stack[{newValue.position}]")
            temp_ant = ''
            for idx, val in enumerate(split):

                if (idx < len(split) - 1):
                    temp_ant = temp
                    temp = ThreeAddressCode().newTemp()
                    ThreeAddressCode().addCode(
                        f"{temp} = \"{val}\" + str({temp_ant})")
                    newString += f"{val}{newValue.value}"

            temp_ant = temp
            temp = ThreeAddressCode().newTemp()
            ThreeAddressCode().addCode(f"{temp} = {temp_ant} + \";\"")
            newString += ';'

            print("valor nuevo string: ", newString)

    if len(temps_array) > 0:
        funciones = Procedures().getProceduresIDs()
        contador_funciones = 0
        for func in funciones:
            print(f"funcion: {func}")
            # func = func.lower()

            if func in entry_lower:
                contador_temporales = ThreeAddressCode().tempCounter
                #separando string
                contador_funciones = hacerUnSoloCambio(func, entry_lower,
                                                       temps_array,
                                                       contador_funciones)

                for r in range(contador_temporales,
                               ThreeAddressCode().tempCounter - 1):
                    temp_ant = temp
                    temp = ThreeAddressCode().newTemp()
                    if temp_ant is None:
                        ThreeAddressCode().addCode(f"{temp} = t{r} + t{r+1}")
                    else:
                        ThreeAddressCode().addCode(
                            f"{temp} = {temp_ant} + t{r+1}")

                if temp is None:
                    dif = ThreeAddressCode().tempCounter - contador_temporales
                    if dif > 0:
                        temp = f"t{contador_temporales + 1}"

                # temp_ant = temp
                # temp = ThreeAddressCode().newTemp()
                # ThreeAddressCode().addCode(f"{temp} = {temp_ant} + \";\"")
                # newString +=  f"{val}"
                # entry_lower = newString.lower()

                # print("valor nuevo string: ", newString)

    if temp is None: return entry
    else: return temp