Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Update codigo_intermedio
Browse files- codigo_intermedio +19 -59
    	
        codigo_intermedio
    CHANGED
    
    | @@ -1,64 +1,24 @@ | |
| 1 | 
             
            class GeneradorIntermedio:
         | 
| 2 | 
            -
                def __init__(self):
         | 
| 3 | 
            -
                    self.codigo = []
         | 
| 4 | 
            -
                    self.temp_id = 1
         | 
| 5 | 
            -
             | 
| 6 | 
            -
                def nuevo_temp(self):
         | 
| 7 | 
            -
                    temp = f"t{self.temp_id}"
         | 
| 8 | 
            -
                    self.temp_id += 1
         | 
| 9 | 
            -
                    return temp
         | 
| 10 | 
            -
             | 
| 11 | 
             
                def generar(self, ast):
         | 
| 12 | 
            -
                     | 
| 13 | 
            -
             | 
| 14 | 
            -
                    return self.codigo
         | 
| 15 |  | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
                         | 
| 20 | 
            -
                        self.codigo.append(f"{nodo['var']} = {temp}")
         | 
| 21 | 
            -
                    elif tipo == "function":
         | 
| 22 | 
            -
                        arg = self.generar_expresion(nodo["arg"]) if nodo["arg"] else None
         | 
| 23 | 
            -
                        if arg:
         | 
| 24 | 
            -
                            self.codigo.append(f"PARAM {arg}")
         | 
| 25 | 
            -
                        self.codigo.append(f"CALL {nodo['name']}")
         | 
| 26 | 
            -
                    elif tipo == "if":
         | 
| 27 | 
            -
                        cond = self.generar_expresion(nodo["condition"])
         | 
| 28 | 
            -
                        etiqueta = self.nueva_etiqueta()
         | 
| 29 | 
            -
                        self.codigo.append(f"IF NOT {cond} GOTO {etiqueta}")
         | 
| 30 | 
            -
                        for instr in nodo["body"]:
         | 
| 31 | 
            -
                            self.generar_instruccion(instr)
         | 
| 32 | 
            -
                        self.codigo.append(f"{etiqueta}:")
         | 
| 33 | 
            -
                    elif tipo == "while":
         | 
| 34 | 
            -
                        inicio = self.nueva_etiqueta()
         | 
| 35 | 
            -
                        fin = self.nueva_etiqueta()
         | 
| 36 | 
            -
                        self.codigo.append(f"{inicio}:")
         | 
| 37 | 
            -
                        cond = self.generar_expresion(nodo["condition"])
         | 
| 38 | 
            -
                        self.codigo.append(f"IF NOT {cond} GOTO {fin}")
         | 
| 39 | 
            -
                        for instr in nodo["body"]:
         | 
| 40 | 
            -
                            self.generar_instruccion(instr)
         | 
| 41 | 
            -
                        self.codigo.append(f"GOTO {inicio}")
         | 
| 42 | 
            -
                        self.codigo.append(f"{fin}:")
         | 
| 43 | 
            -
             | 
| 44 | 
            -
                def generar_expresion(self, expr):
         | 
| 45 | 
            -
                    tipo = expr["type"]
         | 
| 46 | 
            -
                    if tipo in ("num", "var", "bool", "string"):
         | 
| 47 | 
            -
                        return expr["value"]
         | 
| 48 | 
            -
                    elif tipo == "binop":
         | 
| 49 | 
            -
                        izq = self.generar_expresion(expr["left"])
         | 
| 50 | 
            -
                        der = self.generar_expresion(expr["right"])
         | 
| 51 | 
            -
                        temp = self.nuevo_temp()
         | 
| 52 | 
            -
                        self.codigo.append(f"{temp} = {izq} {expr['op']} {der}")
         | 
| 53 | 
            -
                        return temp
         | 
| 54 | 
            -
                    elif tipo == "call":
         | 
| 55 | 
            -
                        return self.generar_expresion(expr["arg"])
         | 
| 56 | 
            -
                    else:
         | 
| 57 | 
            -
                        temp = self.nuevo_temp()
         | 
| 58 | 
            -
                        self.codigo.append(f"{temp} = ???")
         | 
| 59 | 
             
                        return temp
         | 
| 60 |  | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
             
            class GeneradorIntermedio:
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 2 | 
             
                def generar(self, ast):
         | 
| 3 | 
            +
                    codigo = []
         | 
| 4 | 
            +
                    temp_count = 0
         | 
|  | |
| 5 |  | 
| 6 | 
            +
                    def nueva_temp():
         | 
| 7 | 
            +
                        nonlocal temp_count
         | 
| 8 | 
            +
                        temp = f"t{temp_count}"
         | 
| 9 | 
            +
                        temp_count += 1
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 10 | 
             
                        return temp
         | 
| 11 |  | 
| 12 | 
            +
                    for nodo in ast:
         | 
| 13 | 
            +
                        if nodo["type"] == "assign":
         | 
| 14 | 
            +
                            temp = nueva_temp()
         | 
| 15 | 
            +
                            codigo.append(f"{temp} = {nodo['value']['value']}")
         | 
| 16 | 
            +
                            codigo.append(f"{nodo['var']} = {temp}")
         | 
| 17 | 
            +
                        elif nodo["type"] == "function":
         | 
| 18 | 
            +
                            if nodo["arg"] is not None:
         | 
| 19 | 
            +
                                temp = nueva_temp()
         | 
| 20 | 
            +
                                codigo.append(f"PARAM {nodo['arg']['value']}")
         | 
| 21 | 
            +
                                codigo.append(f"CALL {nodo['name']}")
         | 
| 22 | 
            +
                            else:
         | 
| 23 | 
            +
                                codigo.append(f"CALL {nodo['name']}")
         | 
| 24 | 
            +
                    return codigo
         |