-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.py
executable file
·45 lines (43 loc) · 1.38 KB
/
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
import sys
data = open('brainfucksrc', 'r').read()
out = ''
dataPointer = 0
instructionPointer = 0
cells = [0]*30000
while instructionPointer < len(data):
if data[instructionPointer] == '+':
cells[dataPointer] += 1
elif data[instructionPointer] == '-':
cells[dataPointer] -= 1
elif data[instructionPointer] == '>':
dataPointer += 1
elif data[instructionPointer] == '<':
dataPointer -= 1
elif data[instructionPointer] == '.':
out += chr(cells[dataPointer])
if cells[dataPointer] == 0:
print(out)
out = ''
elif data[instructionPointer] == ',':
cells[dataPointer] = ord(raw_input('input character: '))
elif data[instructionPointer] == '[':
if cells[dataPointer] == 0:
nest = 1
while nest > 0:
instructionPointer += 1
if data[instructionPointer] == '[':
nest += 1
elif data[instructionPointer] == ']':
nest -= 1
elif data[instructionPointer] == ']':
if cells[dataPointer] != 0:
nest = 1
while nest > 0:
instructionPointer -= 1
if data[instructionPointer] == ']':
nest += 1
elif data[instructionPointer] == '[':
nest -= 1
instructionPointer += 1
if out != '':
print(out)