Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MohammadHosseinGhorbani/f54da36462998077e8f6d47eafb21405 to your computer and use it in GitHub Desktop.
Save MohammadHosseinGhorbani/f54da36462998077e8f6d47eafb21405 to your computer and use it in GitHub Desktop.
Dirtiest Brainfuck Interpreter I've Ever Seen (But It Works)
memory = [0]
pointer = 0
loop = False
loop_start = 0
loop_point = 0
bf_code = '<BRAINFUCK CODE>'
def check_char(char):
global memory, pointer
match char:
case '+':
memory[pointer] += 1
case '-':
memory[pointer] -= 1
case '>':
pointer += 1
if len(memory) < pointer+1:
memory.append(0)
case '<':
if pointer > 0:
pointer -= 1
case '.':
print(chr(memory[pointer]), end='')
for char_index in range(len(bf_code)):
char = bf_code[char_index]
if char in '+-><.':
check_char(char)
elif char == '[':
loop = True
loop_start = char_index+1
loop_point = pointer
elif char == ']':
loop_end = char_index
while loop:
for loop_char in bf_code[loop_start:loop_end]:
check_char(loop_char)
if not memory[loop_point]:
loop = False
else:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment