#!/usr/bin/env python3 import sys import random import time # Memória da máquina memory = [0] * 256 # 256 endereços possíveis def parse_line(line): # Remove comentários após "//" line = line.split('//')[0].strip() if not line: return None tokens = [t.strip() for t in line.split(';') if t.strip()] if len(tokens) != 4: print(f"[ERRO] Linha inválida (esperado 4 valores): {line}") return None try: return [int(t, 16) for t in tokens] # HEX -> int except ValueError: print(f"[ERRO] Valores inválidos na linha: {line}") return None def to_hex(val): return format(val, 'X') def run_program(program, debug=False): pc = 0 while pc < len(program): instr = program[pc] cmd = format(instr[0], 'X') if debug: hex_instr = [to_hex(x) for x in instr] hex_mem = [to_hex(x) for x in memory[:16]] print(f"[DEBUG] PC={to_hex(pc+1)}, CMD={hex_instr}, MEM[0..F]={hex_mem}") if cmd == "0": # HALT print("[INFO] Programa finalizado.") break elif cmd == "1": # INPUT a, b = instr[1], instr[2] if b != 0: memory[a] = b else: print("[INFO] Waiting for input...") try: value = input(f"Digite valor HEX para mem[{to_hex(a)}]: ").strip() memory[a] = int(value, 16) except KeyboardInterrupt: print("\n[INFO] Entrada cancelada pelo usuário (Ctrl+C).") sys.exit(0) except ValueError: print("[ERRO] Valor inválido! Use HEX (ex.: A, F, 1F)") return None elif cmd == "2": # OUTPUT addr = instr[1] print(f"[OUTPUT] {to_hex(memory[addr])}") elif cmd == "3": # ADD a, b, c = instr[1], instr[2], instr[3] memory[c] = memory[a] + memory[b] elif cmd == "4": # SUB a, b, c = instr[1], instr[2], instr[3] memory[c] = memory[a] - memory[b] elif cmd == "5": # DIV a, b, c = instr[1], instr[2], instr[3] memory[c] = memory[a] // memory[b] if memory[b] != 0 else 0 elif cmd == "6": # MUL a, b, c = instr[1], instr[2], instr[3] memory[c] = memory[a] * memory[b] elif cmd == "7": # COND JUMP addr, val, target = instr[1], instr[2], instr[3] if memory[addr] == val: pc = target - 1 continue elif cmd == "8": # JUMP pc = instr[1] - 1 continue elif cmd == "9": # CLEAR addr = instr[1] memory[addr] = 0 elif cmd == "A": # RANDOM addr = instr[1] memory[addr] = random.randint(0, 15) elif cmd == "B": # CMP GREATER x, y, out = instr[1], instr[2], instr[3] memory[out] = 1 if memory[x] > memory[y] else 0 elif cmd == "C": # CMP LESS x, y, out = instr[1], instr[2], instr[3] memory[out] = 1 if memory[x] < memory[y] else 0 elif cmd == "D": # MOVE src, dst = instr[1], instr[2] memory[dst] = memory[src] elif cmd == "E": # INC/DEC addr, flag = instr[1], instr[2] if flag == 1: memory[addr] += 1 elif flag == 0: memory[addr] -= 1 else: print(f"[ERRO] Flag inválida: {to_hex(flag)}") return None elif cmd == "F": # WAIT delay = instr[1] print(f"[INFO] Esperando {delay} segundos...") time.sleep(delay) else: print(f"[ERRO] Comando inválido: {cmd}") return None pc += 1 def repl(): print("Modo REPL RedDust (digite 'exit' para sair)") program = [] line_num = 1 while True: try: line = input(f"linha {line_num}> ").strip() if line.lower() == "exit": print("[INFO] Saindo do REPL e executando programa...") break instr = parse_line(line) if instr: program.append(instr) line_num += 1 except KeyboardInterrupt: print("\n[INFO] Interrompido pelo usuário (Ctrl+C).") sys.exit(0) run_program(program) def show_help(): print(""" RedDust Interpreter v3.1 Uso: reddust Executa um programa RedDust reddust Inicia modo interativo (REPL) reddust --debug Executa com debug (mostra memória) reddust --version Mostra versão reddust --help Exibe esta ajuda Formato do código: Cada linha deve conter 4 valores HEX separados por ponto-e-vírgula (;) Exemplo: 1;A;0;0 // INPUT: pede valor para mem[A] 2;A;0;0 // OUTPUT: mostra mem[A] 0;0;0;0 // HALT """) def main(): try: if "--help" in sys.argv: show_help() sys.exit(0) if "--version" in sys.argv: print("RedDust Interpreter v3.1 (HEX Mode)") sys.exit(0) debug = "--debug" in sys.argv if len(sys.argv) == 1 or (len(sys.argv) == 2 and debug): repl() return filename = sys.argv[1] if not filename.endswith(".redd"): print("[ERRO] Arquivo deve ter extensão .redd") sys.exit(1) try: with open(filename, 'r') as f: lines = f.readlines() except FileNotFoundError: print(f"[ERRO] Arquivo '{filename}' não encontrado.") sys.exit(1) program = [] for line in lines: instr = parse_line(line) if instr: program.append(instr) run_program(program, debug) except KeyboardInterrupt: print("\n[INFO] Execução interrompida pelo usuário (Ctrl+C).") sys.exit(0) if __name__ == "__main__": main()