interpeter.py (2581B)
1 import sys 2 import os 3 import subprocess 4 import re 5 6 # Set the absolute path to your GCC compiler based on your system logs 7 COMPILER_PATH = r"G:\bsc\MinGW64\bin\gcc.exe" 8 9 def run_universal_compiler(): 10 if len(sys.argv) < 2: 11 print("Usage: python emojic.py <file.c>") 12 sys.exit(1) 13 14 source_path = sys.argv[1] 15 if not os.path.exists(source_path): 16 print(f"File not found: {source_path}") 17 sys.exit(1) 18 19 with open(source_path, 'r', encoding='utf-8') as f: 20 content = f.read() 21 22 mapping = {} 23 # Extract emoji mappings from #define statements 24 define_pattern = re.compile(r'#define\s+(\S+)\s+(.+)') 25 26 lines = content.splitlines() 27 code_lines = [] 28 29 for line in lines: 30 match = define_pattern.match(line.strip()) 31 if match: 32 emoji_key = match.group(1) 33 c_value = match.group(2).strip() 34 mapping[emoji_key] = c_value 35 else: 36 code_lines.append(line) 37 38 translated_code = "\n".join(code_lines) 39 40 # Sort keys by length descending to replace multi-emoji tokens correctly 41 sorted_keys = sorted(mapping.keys(), key=len, reverse=True) 42 43 for key in sorted_keys: 44 translated_code = translated_code.replace(key, mapping[key]) 45 46 # Ensure standard header is present if logic needs it 47 header = "#include <stdio.h>\n" 48 if "#include" not in translated_code: 49 translated_code = header + translated_code 50 51 temp_file = "output_target.c" 52 with open(temp_file, "w", encoding="utf-8") as f: 53 f.write(translated_code) 54 55 exe_name = source_path.rsplit('.', 1)[0] + ".exe" 56 compile_cmd = [COMPILER_PATH, temp_file, "-o", exe_name] 57 58 try: 59 # Use shell=True for better command resolution on Windows 60 result = subprocess.run(compile_cmd, capture_output=True, text=True, shell=(os.name == 'nt')) 61 62 if result.returncode == 0: 63 print(f"Compilation Successful: {exe_name}") 64 run_cmd = [f".\\{exe_name}"] if os.name == 'nt' else [f"./{exe_name}"] 65 subprocess.run(run_cmd, shell=(os.name == 'nt')) 66 else: 67 print("Compilation Error:") 68 print(result.stderr) 69 70 except FileNotFoundError: 71 print(f"Error: Compiler '{COMPILER_PATH}' not found.") 72 print("Please ensure GCC is installed and added to your PATH,") 73 print("or edit COMPILER_PATH in this script to point to your gcc.exe.") 74 75 if os.path.exists(temp_file): 76 os.remove(temp_file) 77 78 if __name__ == "__main__": 79 run_universal_compiler()