Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: brainfuck_interpreter_manager.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - PasteBin Version Without Any Brainfuck Code (To Bypass PasteBin/NSA Idiotic Censors)
- - This script provides a Brainfuck interpreter, enabling users to execute Brainfuck code.
- - It utilizes a Python wrapper for the Brainfuck code execution.
- - It includes functions to encode text into Brainfuck code and decode Brainfuck code back into text.
- - It features a menu-driven interface for user interaction ease.
- - The ASCII art header adds a visually appealing introduction to the Brainfuck Interpreter Manager.
- Requirements:
- - Python 3.x
- Functions:
- - brainfuck_interpreter(code):
- Executes Brainfuck code and generates the corresponding output.
- - encode_to_brainfuck(text):
- Converts text into Brainfuck code for execution.
- - decode_from_brainfuck(code):
- Converts Brainfuck code into its original text.
- - header():
- Prints a visually appealing ASCII art header.
- Usage:
- Run the script and follow the menu prompts to encode text into Brainfuck or decode Brainfuck code into text.
- Additional Notes:
- - This script assumes the user input is valid Brainfuck code during decoding.
- """
- def brainfuck_interpreter(code):
- """
- Interprets Brainfuck code.
- Args:
- code (str): The Brainfuck code to interpret.
- Returns:
- None
- Raises:
- None
- """
- # Convert the input code string into a list containing only valid Brainfuck commands
- code = [c for c in code if c in ['>', '<', '+', '-', '.', ',', '[', ']']]
- # Initialize variables: tape (memory), pointer, code pointer, and loop stack
- tape = [0] * 30000
- ptr = 0
- code_ptr = 0
- loop_stack = []
- # Execute the Brainfuck code
- while code_ptr < len(code):
- command = code[code_ptr] # Get the current command
- # Execute different commands based on the Brainfuck syntax
- if command == '>':
- ptr += 1 # Move the pointer to the right
- elif command == '<':
- ptr -= 1 # Move the pointer to the left
- elif command == '+':
- tape[ptr] = (tape[ptr] + 1) % 256 # Increment the value at the pointer
- elif command == '-':
- tape[ptr] = (tape[ptr] - 1) % 256 # Decrement the value at the pointer
- elif command == '.':
- print(chr(tape[ptr]), end='') # Output the ASCII character at the pointer
- elif command == ',':
- tape[ptr] = ord(input('Input: ')[0]) # Input a character and store its ASCII value
- elif command == '[':
- if tape[ptr] == 0: # If the value at the pointer is zero, skip to the matching ']'
- open_brackets = 1
- while open_brackets != 0:
- code_ptr += 1
- if code[code_ptr] == '[':
- open_brackets += 1
- elif code[code_ptr] == ']':
- open_brackets -= 1
- else:
- loop_stack.append(code_ptr) # Otherwise, push the current code pointer to the loop stack
- elif command == ']':
- if tape[ptr] != 0: # If the value at the pointer is not zero, jump back to the matching '['
- code_ptr = loop_stack[-1]
- else:
- loop_stack.pop() # Otherwise, pop the last code pointer from the loop stack
- code_ptr += 1 # Move to the next command in the code
- def encode_to_brainfuck(text):
- """
- Encodes text into Brainfuck code.
- Args:
- text (str): The text to encode.
- Returns:
- str: The Brainfuck code.
- Raises:
- None
- """
- # Initialize an empty string to store the Brainfuck code
- brainfuck_code = ""
- # Iterate through each character in the input text
- for char in text:
- # Get the ASCII value of the character
- ascii_value = ord(char)
- # Append '+' repeated ascii_value times followed by '.>' to the Brainfuck code string
- brainfuck_code += "+" * ascii_value + ".>"
- # Remove the last '>' from the Brainfuck code and return the encoded string
- return brainfuck_code[:-1]
- def decode_from_brainfuck(code):
- """
- Decodes Brainfuck code into text.
- Args:
- code (str): The Brainfuck code to decode.
- Returns:
- str: The decoded text.
- Raises:
- None
- """
- # Initialize variables: tape (memory), pointer, decoded text, code pointer, and loop stack
- tape = [0] * 30000
- ptr = 0
- decoded_text = ""
- code_ptr = 0
- loop_stack = []
- # Execute the Brainfuck code
- while code_ptr < len(code):
- command = code[code_ptr] # Get the current command
- # Execute different commands based on the Brainfuck syntax
- if command == '>':
- ptr += 1 # Move the pointer to the right
- elif command == '<':
- ptr -= 1 # Move the pointer to the left
- elif command == '+':
- tape[ptr] = (tape[ptr] + 1) % 256 # Increment the value at the pointer
- elif command == '-':
- tape[ptr] = (tape[ptr] - 1) % 256 # Decrement the value at the pointer
- elif command == '.':
- decoded_text += chr(tape[ptr]) # Append the ASCII character at the pointer to the decoded text
- elif command == ',':
- raise ValueError("Input command ',' is not supported in this decoding function.")
- elif command == '[':
- if tape[ptr] == 0: # If the value at the pointer is zero, skip to the matching ']'
- open_brackets = 1
- while open_brackets != 0:
- code_ptr += 1
- if code[code_ptr] == '[':
- open_brackets += 1
- elif code[code_ptr] == ']':
- open_brackets -= 1
- else:
- loop_stack.append(code_ptr) # Otherwise, push the current code pointer to the loop stack
- elif command == ']':
- if tape[ptr] != 0: # If the value at the pointer is not zero, jump back to the matching '['
- code_ptr = loop_stack[-1]
- else:
- loop_stack.pop() # Otherwise, pop the last code pointer from the loop stack
- code_ptr += 1 # Move to the next command in the code
- # Return the decoded text
- return decoded_text
- def header():
- """
- Prints the ASCII art header for the Brainfuck interpreter manager.
- This function displays a stylized text banner for the Brainfuck interpreter manager,
- providing a visually appealing introduction when the program is run.
- """
- print(r"""
- ____ ____ ____ ____ ____ _____ __ __ __ __ _
- | \ | \ / || || \ | || | | / ]| |/ ]
- | o )| D )| o | | | | _ || __|| | | / / | ' /
- | || / | | | | | | || |_ | | |/ / | \
- | O || \ | _ | | | | | || _] | : / \_ | \
- | || . \| | | | | | | || | | \ || . |
- |_____||__|\_||__|__||____||__|__||__| \__,_|\____||__|\_|
- ___ ___ ____ ____ ____ ____ ___ ____
- | | | / || \ / | / | / _]| \
- | _ _ || o || _ || o || __| / [_ | D )
- | \_/ || || | || || | || _]| /
- | | || _ || | || _ || |_ || [_ | \
- | | || | || | || | || || || . \
- |___|___||__|__||__|__||__|__||___,_||_____||__|\_|
- """)
- def main_loop():
- """
- Main loop for the Brainfuck interpreter menu.
- """
- while True:
- print("\n\t\t\t:: BRAINFUCK INTERPRETER MENU ::\n")
- print("1: Encode")
- print("2: Decode")
- print("0: Exit")
- choice = input("\nSelect an option (or press '0' to exit): ")
- if choice == "1":
- # Encode text
- text = input("\nEnter text to encode: ")
- brainfuck_code = encode_to_brainfuck(text)
- print("\nEncoded Brainfuck code:", brainfuck_code)
- elif choice == "2":
- # Decode Brainfuck code
- code = input("Enter Brainfuck code to decode: ")
- decoded_text = decode_from_brainfuck(code)
- print("\nDecoded text:", decoded_text)
- elif choice == "0":
- # Print Header & Exit Program
- header()
- print("_" * 100)
- print("\n\t\t\t Exiting Program... Goodbye!")
- print("_" * 100)
- break
- else:
- # Invalid choice
- print("\nInvalid choice! Please select again.\n")
- def main():
- """
- Main function to run the Brainfuck interpreter.
- """
- # Print the header ascii text
- header()
- input("Hit [ANY] Key To Continue.\n")
- # Print additional Brainfuck code and its execution result
- print("_" * 100)
- print(
- """
- :: Brainfuck Commands ::
- > Increment the data pointer (move to the next cell to the right).
- < Decrement the data pointer (move to the next cell to the left).
- + Increment the byte at the data pointer.
- - Decrement the byte at the data pointer.
- . Output the byte at the data pointer as ASCII character.
- , Input a character and store it in the byte at the data pointer.
- [ If the byte at the data pointer is zero jump forward to the command after the matching ].
- ] If the byte at the data pointer is nonzero jump back to the command after the matching [.
- """)
- print() # Newline for ending
- print("_" * 100)
- input("Hit [ANY] Key To Continue.\n")
- # Print Header Once More
- print("\n" * 2)
- header()
- # Run the main loop
- main_loop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement