Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import io | |
| import contextlib | |
| import re | |
| import traceback | |
| import math | |
| def preprocessar_codigos(portugol_code): | |
| replacements = { | |
| r"\bSENAO\b": "ELSE", | |
| r"\bFIMSE\b": "ENDIF", | |
| r"\bENTAO\b": "THEN", | |
| r"\bRAIZ\(": "math.sqrt(", | |
| r"(\w+)\^(\w+)": r"math.pow(\1, \2)", | |
| r"\bE\b": "and", | |
| r"\bOU\b": "or", | |
| r"\bNAO\b": "not", | |
| r"\bESCOLHA\b": "SWITCH", | |
| r"\bCASO\b": "CASE", | |
| r"\bCASO CONTRARIO\b": "DEFAULT", | |
| r"\bFIM-ESCOLHA\b": "ENDSWITCH", | |
| r"\bPARA\b": "FOR", | |
| r"\bATÉ\b": "TO", | |
| r"\bATE\b": "TO", | |
| r"\bFIM-PARA\b": "ENDFOR", | |
| r"\bESCREVER\b": "ESCREVA", | |
| r"\bENQUANTO\b": "WHILE", | |
| r"\bFIM-ENQUANTO\b": "ENDWHILE" | |
| } | |
| for pattern, replacement in replacements.items(): | |
| portugol_code = re.sub(pattern, replacement, portugol_code, flags=re.IGNORECASE) | |
| return portugol_code | |
| def processar_escreva(linha): | |
| padrao = r'ESCREVA\s*(.*)' | |
| match = re.match(padrao, linha, re.IGNORECASE) | |
| if match: | |
| conteudo = match.group(1).strip() | |
| conteudo_parts = conteudo.split('//', 1) | |
| conteudo = conteudo_parts[0].strip() | |
| comentario = f" # {conteudo_parts[1].strip()}" if len(conteudo_parts) > 1 else "" | |
| if conteudo.startswith('"') and conteudo.endswith('"'): | |
| return f"print({conteudo}){comentario}" | |
| else: | |
| return f"print({conteudo}){comentario}" | |
| return linha | |
| def verificar_tipo(var_name, valor, tipos_variaveis, linha): | |
| if var_name in tipos_variaveis: | |
| tipo_declarado = tipos_variaveis[var_name] | |
| if tipo_declarado == "INTEIRO" and "/" in valor: | |
| return f"# Atenção: Possível atribuição de valor real à variável inteira '{var_name}' na linha: {linha}" | |
| return None | |
| def identificar_variaveis_entrada(portugol_code): | |
| lines = portugol_code.splitlines() | |
| variaveis_entrada = [] | |
| tipos_variaveis = {} | |
| for line in lines: | |
| line = line.strip() | |
| if ":" in line and not line.upper().startswith("CASO"): | |
| var_name, var_type = line.split(":", 1) | |
| var_name = var_name.strip().lower() # Convertendo para minúsculas | |
| var_type = var_type.strip().upper() | |
| tipos_variaveis[var_name] = var_type | |
| elif line.upper().startswith("LEIA"): | |
| var_name = line.split("(")[1].split(")")[0].strip().lower() # Convertendo para minúsculas | |
| variaveis_entrada.append((var_name, tipos_variaveis.get(var_name, "STRING"))) | |
| return variaveis_entrada | |
| def traduzir_para_python(portugol_code): | |
| portugol_code = preprocessar_codigos(portugol_code) | |
| lines = portugol_code.splitlines() | |
| codigo_python = ["import math"] | |
| indentacao = 0 | |
| contexto_bloco = [] | |
| tipos_variaveis = {} | |
| avisos = [] | |
| in_switch = False | |
| switch_var = "" | |
| first_case = True | |
| for i, line in enumerate(lines, 1): | |
| line = line.strip() | |
| if not line or line.upper() in ["VARIAVEIS:", "ALGORITMO", "INICIO", "FIM"]: | |
| continue | |
| if ":" in line and not line.upper().startswith("CASE"): | |
| var_name, var_type = line.split(":", 1) | |
| var_name = var_name.strip() | |
| var_type = var_type.strip().upper() | |
| tipos_variaveis[var_name] = var_type | |
| if var_type == "INTEIRO": | |
| codigo_python.append(f"{var_name} = 0") | |
| elif var_type == "REAL": | |
| codigo_python.append(f"{var_name} = 0.0") | |
| elif var_type == "LOGICO": | |
| codigo_python.append(f"{var_name} = False") | |
| elif var_type in ["STRING", "CARACTERE"]: | |
| codigo_python.append(f"{var_name} = ''") | |
| elif line.upper().startswith("LEIA"): | |
| var_name = line.split("(")[1].split(")")[0].strip() | |
| if var_name in tipos_variaveis: | |
| tipo = tipos_variaveis[var_name] | |
| if tipo == "INTEIRO": | |
| codigo_python.append(f"{var_name} = int(input_values['{var_name.lower()}'])") | |
| elif tipo == "REAL": | |
| codigo_python.append(f"{var_name} = float(input_values['{var_name.lower()}'])") | |
| else: | |
| codigo_python.append(f"{var_name} = input_values['{var_name.lower()}']") | |
| else: | |
| codigo_python.append(f"{var_name} = input_values['{var_name.lower()}']") | |
| elif line.upper().startswith("SE"): | |
| condicao = line[2:].split("THEN")[0].strip() | |
| codigo_python.append(f"{' ' * indentacao}if {condicao}:") | |
| contexto_bloco.append("if") | |
| indentacao += 1 | |
| elif line.upper() == "ELSE": | |
| if contexto_bloco and contexto_bloco[-1] == "if": | |
| indentacao -= 1 | |
| codigo_python.append(f"{' ' * indentacao}else:") | |
| indentacao += 1 | |
| else: | |
| codigo_python.append(f"# Aviso: 'ELSE' sem 'SE' correspondente: {line}") | |
| elif line.upper() == "ENDIF": | |
| if contexto_bloco and contexto_bloco[-1] in ["if", "else"]: | |
| indentacao -= 1 | |
| contexto_bloco.pop() | |
| else: | |
| codigo_python.append(f"# Aviso: 'ENDIF' sem 'SE' correspondente: {line}") | |
| elif line.upper().startswith("ESCOLHA") or line.upper().startswith("SWITCH"): | |
| in_switch = True | |
| switch_var = line.split("(")[1].split(")")[0].strip() | |
| first_case = True | |
| elif line.upper().startswith("CASO") and in_switch: | |
| case_value = line.split(":")[0].split()[1] | |
| if first_case: | |
| codigo_python.append(f"{' ' * indentacao}if {switch_var} == {case_value}:") | |
| first_case = False | |
| else: | |
| indentacao -= 1 | |
| codigo_python.append(f"{' ' * indentacao}elif {switch_var} == {case_value}:") | |
| indentacao += 1 | |
| elif (line.upper().startswith("CASO CONTRARIO") or line.upper() == "DEFAULT") and in_switch: | |
| indentacao -= 1 | |
| codigo_python.append(f"{' ' * indentacao}else:") | |
| indentacao += 1 | |
| elif line.upper() == "FIM-ESCOLHA" or line.upper() == "ENDSWITCH": | |
| in_switch = False | |
| indentacao -= 1 | |
| elif line.upper().startswith("PARA") or line.upper().startswith("FOR"): | |
| match = re.match(r"(PARA|FOR)\s+(\w+)\s*=\s*(\d+)\s*(ATE|TO)\s*(\d+)", line, re.IGNORECASE) | |
| if match: | |
| _, var, start, _, end = match.groups() | |
| codigo_python.append(f"{' ' * indentacao}for {var} in range({start}, {int(end)+1}):") | |
| indentacao += 1 | |
| contexto_bloco.append("for") | |
| else: | |
| codigo_python.append(f"# Aviso: Estrutura 'PARA' mal formatada: {line}") | |
| elif line.upper() == "FIM-PARA" or line.upper() == "ENDFOR": | |
| if contexto_bloco and contexto_bloco[-1] == "for": | |
| indentacao -= 1 | |
| contexto_bloco.pop() | |
| else: | |
| codigo_python.append(f"# Aviso: 'FIM-PARA' sem 'PARA' correspondente: {line}") | |
| elif line.upper().startswith("ENQUANTO") or line.upper().startswith("WHILE"): | |
| condicao = line.split(None, 1)[1].strip() | |
| codigo_python.append(f"{' ' * indentacao}while {condicao}:") | |
| indentacao += 1 | |
| contexto_bloco.append("while") | |
| elif line.upper() == "FIM-ENQUANTO" or line.upper() == "ENDWHILE": | |
| if contexto_bloco and contexto_bloco[-1] == "while": | |
| indentacao -= 1 | |
| contexto_bloco.pop() | |
| else: | |
| codigo_python.append(f"# Aviso: 'FIM-ENQUANTO' sem 'ENQUANTO' correspondente: {line}") | |
| elif line.upper().startswith("ESCREVA") or line.upper().startswith("ESCREVER"): | |
| codigo_python.append(f"{' ' * indentacao}{processar_escreva(line)}") | |
| elif "=" in line: | |
| var_name, valor = line.split("=", 1) | |
| var_name = var_name.strip() | |
| valor = valor.strip() | |
| aviso = verificar_tipo(var_name, valor, tipos_variaveis, i) | |
| if aviso: | |
| avisos.append(aviso) | |
| codigo_python.append(f"{' ' * indentacao}{line}") | |
| return "\n".join(codigo_python), avisos | |
| def interpretador(portugol_code, **input_values): | |
| codigo_python, avisos = traduzir_para_python(portugol_code) | |
| # Adicionando input_values ao escopo de execução | |
| locals_dict = {'input_values': input_values} | |
| output = io.StringIO() | |
| with contextlib.redirect_stdout(output): | |
| exec(codigo_python, globals(), locals_dict) | |
| resultado = f"Saída:\n{output.getvalue().strip()}" | |
| if avisos: | |
| resultado = "Avisos:\n" + "\n".join(avisos) + "\n\n" + resultado | |
| return resultado | |
| def interpretar(portugol_code, **kwargs): | |
| variaveis_entrada = identificar_variaveis_entrada(portugol_code) | |
| input_values = {var[0]: kwargs.get(var[0], '') for var in variaveis_entrada} | |
| return interpretador(portugol_code, **input_values) | |
| def execute_code(code, *args): | |
| variaveis_entrada = identificar_variaveis_entrada(code) | |
| input_values = {var[0]: arg for var, arg in zip(variaveis_entrada, args) if arg} | |
| return interpretar(code, **input_values) | |
| def criar_interface(): | |
| with gr.Blocks() as iface: | |
| gr.Markdown(description_html) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| portugol_code = gr.Textbox(lines=20, label="Pseudocódigo") | |
| with gr.Column(visible=False) as input_column: | |
| input_boxes = [gr.Textbox(visible=False, label=f"Entrada {i+1}") for i in range(10)] | |
| run_button = gr.Button("Executar") | |
| exemplo_button = gr.Button("Carregar Exemplo de Fatorial") | |
| with gr.Column(scale=1): | |
| output = gr.Textbox(label="Saída", lines=20) | |
| def update_interface(portugol_code): | |
| variaveis_entrada = identificar_variaveis_entrada(portugol_code) | |
| return [gr.update(visible=True, label=f"Valor para {var_name} ({var_type})") for var_name, var_type in variaveis_entrada] + \ | |
| [gr.update(visible=False) for _ in range(10 - len(variaveis_entrada))] | |
| exemplo_code = """VARIAVEIS | |
| n: INTEIRO | |
| fat: INTEIRO | |
| INICIO | |
| LEIA (n) | |
| fat = 1 | |
| ENQUANTO n > 1 | |
| fat = fat * n | |
| n = n - 1 | |
| FIM-ENQUANTO | |
| ESCREVA fat | |
| FIM""" | |
| portugol_code.change( | |
| update_interface, | |
| inputs=[portugol_code], | |
| outputs=input_boxes | |
| ).then( | |
| lambda: gr.update(visible=True), | |
| outputs=[input_column] | |
| ) | |
| exemplo_button.click( | |
| lambda: exemplo_code, | |
| outputs=[portugol_code] | |
| ).then( | |
| update_interface, | |
| inputs=[portugol_code], | |
| outputs=input_boxes | |
| ).then( | |
| lambda: gr.update(visible=True), | |
| outputs=[input_column] | |
| ) | |
| run_button.click( | |
| execute_code, | |
| inputs=[portugol_code] + input_boxes, | |
| outputs=output | |
| ) | |
| return iface | |
| description_html = """ | |
| <p>Interpretador de pseudocódigo:</p> | |
| <p>Ramon Mayor Martins: <a href="https://rmayormartins.github.io/" target="_blank">Website</a> | <a href="https://huggingface.co/rmayormartins" target="_blank">Spaces</a></p> | |
| <p><a href="https://huggingface.co/spaces/rmayormartins/pseudocodelab/blob/main/Manual_PseudocodeLab.pdf" target="_blank">Clique aqui para visualizar o Manual do PseudocodeLab</a></p> | |
| """ | |
| iface = criar_interface() | |
| if __name__ == "__main__": | |
| iface.launch(debug=True) |