Analisis-Lenguaje_Robot / codigo_intermedio.py
Ethgoin's picture
Rename codigo_intermedio to codigo_intermedio.py
1bc6064 verified
raw
history blame contribute delete
828 Bytes
class GeneradorIntermedio:
def generar(self, ast):
codigo = []
temp_count = 0
def nueva_temp():
nonlocal temp_count
temp = f"t{temp_count}"
temp_count += 1
return temp
for nodo in ast:
if nodo["type"] == "assign":
temp = nueva_temp()
codigo.append(f"{temp} = {nodo['value']['value']}")
codigo.append(f"{nodo['var']} = {temp}")
elif nodo["type"] == "function":
if nodo["arg"] is not None:
temp = nueva_temp()
codigo.append(f"PARAM {nodo['arg']['value']}")
codigo.append(f"CALL {nodo['name']}")
else:
codigo.append(f"CALL {nodo['name']}")
return codigo