-
Notifications
You must be signed in to change notification settings - Fork 73
/
using-brainfuck.py
78 lines (59 loc) · 2.03 KB
/
using-brainfuck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""print `Hello, World!` using brainfuck lang in python
Refs:
- https://fatiherikli.github.io/brainfuck-visualizer/
- https://en.wikipedia.org/wiki/Brainfuck
- https://copy.sh/brainfuck/text.html
"""
def bf_eval(code):
"""Bruh"""
output = ""
mem = [0]
mem_ptr = 0
pc = 0 # program counter
brackets_loop_stack = []
while pc < len(code):
if code[pc] == ">":
mem_ptr += 1
if pc >= len(mem):
mem.append(0)
elif code[pc] == "<":
mem_ptr -= 1
elif code[pc] == "+":
mem[mem_ptr] += 1
if mem[mem_ptr] > 255:
mem[mem_ptr] = 0
elif code[pc] == "-":
mem[mem_ptr] -= 1
if mem[mem_ptr] < 0:
mem[mem_ptr] = 255
elif code[pc] == '.':
output += chr(mem[mem_ptr])
elif code[pc] == ',':
pass # idc
elif code[pc] == '[':
if mem[mem_ptr] != 0:
brackets_loop_stack.append(pc)
else:
# run to the end of this bracket
opened_bracket_count = 0
while code[pc] != "]" or opened_bracket_count != 0:
if pc == len(code):
exit()
if code[pc] == "[":
opened_bracket_count += 1
if code[pc] == "]":
opened_bracket_count -= 1
pc += 1
elif code[pc] == ']':
if len(brackets_loop_stack) == 0:
exit()
if mem[mem_ptr] == 0:
_ = brackets_loop_stack.pop()
else:
pc = brackets_loop_stack[-1]
pc += 1
return output
brainfuck_code = "-[------->+<]>-.-[->+++++<]>++.+++++++..+++.[->+++++<]>+.------------.---[->+++<]>.-[--->+<]>---.+++.------.--------.-[--->+<]>."
brainfuck_helloworld = bf_eval(brainfuck_code)
# assert "Hello, World" == brainfuck_helloworld
print(brainfuck_helloworld)