mirror of
https://github.com/Alvin-Zilverstand/sloppatron.git
synced 2026-03-06 11:07:36 +01:00
bingbong
This commit is contained in:
39
languages/custom/bingbong/bingbong_interpreter.py
Normal file
39
languages/custom/bingbong/bingbong_interpreter.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import sys
|
||||
|
||||
def interpret_bingbong(source_code):
|
||||
"""
|
||||
Interprets a string of BingBong source code by converting it from a
|
||||
binary representation to a Python script and executing it.
|
||||
"""
|
||||
binary_string = source_code.replace('.', '0').replace(',', '1')
|
||||
|
||||
# Ensure the binary string has a valid length for UTF-8 conversion
|
||||
if len(binary_string) % 8 != 0:
|
||||
print("Error: The length of the binary string must be a multiple of 8.")
|
||||
return
|
||||
|
||||
try:
|
||||
byte_array = bytearray(int(binary_string[i:i+8], 2) for i in range(0, len(binary_string), 8))
|
||||
python_code = byte_array.decode('utf-8')
|
||||
|
||||
print("--- Executing Decoded Python Code ---")
|
||||
# WARNING: exec() is used here, which can be dangerous. Only run trusted BingBong code.
|
||||
exec(python_code)
|
||||
print("--- Execution Finished ---")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred during execution: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python bingbong_interpreter.py <source_file>")
|
||||
sys.exit(1)
|
||||
|
||||
source_file = sys.argv[1]
|
||||
try:
|
||||
with open(source_file, 'r') as f:
|
||||
source_code = f.read()
|
||||
interpret_bingbong(source_code)
|
||||
except FileNotFoundError:
|
||||
print(f"Error: File not found at '{source_file}'")
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user