-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrainfuckInterpreter.py
78 lines (69 loc) · 2.55 KB
/
BrainfuckInterpreter.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
# All valid tokens of the Brainfuck language
# > Increment data pointer
# < Decrement data pointer
# + Add 1 to value in the current cell
# - Subtract 1 from the value in the current cell
# . Output the ASCII character of the current cell's value
# , Input a character and store the ASCII value in the current cell
# [ Beginning of a loop that runs until the value of the data current cell is 0
# ] End of the looping structure
# Also you can use r to reset the memory and data pointer and mi where i is an int between 0-9 to show the memory values
class BrainFuckInterpreter:
def __init__(self):
self.memory = [0 for i in range(0, 30000)]
self.ptr = 0
self.pos = 0
self.text = ''
def eval_token(self):
token = self.text[self.pos]
if token == '>':
self.ptr += 1
self.pos += 1
elif token == '<':
self.ptr -= 1
self.pos += 1
elif token == '+':
self.memory[self.ptr] += 1
self.pos += 1
elif token == '-':
self.memory[self.ptr] -= 1
self.pos += 1
elif token == '.':
print(chr(self.memory[self.ptr]), end='')
self.pos += 1
elif token == ',':
self.memory[self.ptr] = ord(input())
self.pos += 1
elif token == '[':
self.pos += 1
start_pos = self.pos
end_pos = 0
while self.memory[self.ptr] != 0 or self.text[self.pos] != ']':
if self.text[self.pos] == ']':
end_pos = self.pos + 1
self.pos = start_pos
self.eval_token()
self.pos = end_pos
elif token == 'r':
self.memory = [0 for i in range(0, 30000)]
self.ptr = 0
self.pos += 1
elif token == 'm':
if self.text[self.pos+1] in [str(i) for i in range(0, 10)]:
[print(self.memory[i]) for i in range(0, int(self.text[self.pos+1]))]
self.pos += 1
self.pos += 1
else:
raise Exception("Unidentified token: '{}'".format(token))
print([self.memory[i] for i in range(0, 5)])
def eval_text(self, text):
self.text = text
self.pos = 0
while self.pos != len(self.text):
self.eval_token()
bf = BrainFuckInterpreter()
while True:
code = input("\nbf>>> ")
bf.eval_text(code)
# Sample Code for Hello World!
# ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.