-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfinterpreter.py
More file actions
84 lines (80 loc) · 2.61 KB
/
bfinterpreter.py
File metadata and controls
84 lines (80 loc) · 2.61 KB
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
79
80
81
82
83
84
"""brainfuck to text"""
bfinput = "+++[->+++<]>[->++++++++<]>.\
>++++[->+++++<]>[->+++++<]>+.\
>+++[->+++++<]>[->++++++++<]>+.\
>++[->+++++<]>+[->++++<]>.\
>++++[->++++++++<]>.\
>+++[->++++++<]>[->++++++<]>.\
>++++++[->++++++<]>+[->+++<]>.\
>++++++[->++++++<]>+[->+++<]>.\
>++[->++++++<]>+[->++++<]>+[->++<]>+.\
>++++[->++++++++<]>.\
>++[->++++++++<]>+[->+++++++<]>.\
>++[->++++++<]>+[->++++++++<]>.\
>++[->++++++<]>[->++++++++<]>+.\
>++++[->+++++++<]>+[->++++<]>.\
>++++[->++++++++<]>.\
>+++[->+++<]>[->++++++++<]>+.\
>++++[->++++++++<]>.\
>++++[->+++++<]>[->+++++<]>.\
>+++[->+++++<]>[->+++++++<]>.\
>++++[->+++++<]>[->+++++<]>.\
>++[->+++++<]>+[->+++<]>.\
>"
class bfbits:
def __init__ (self):
self.mempoint = 0
self.instructpoint = 0
self.byteline = [0]*500
self.loopspot = []
self.loopdepth = 0
self.on = True
self.offdepth = 0
self.outputstring =''
def moveright(self):
if(self.on):
self.mempoint += 1
def moveleft(self):
if(self.on):
self.mempoint -= 1
def increment(self):
if(self.on):
self.byteline[self.mempoint] += 1
def decrement(self):
if(self.on):
self.byteline[self.mempoint] -= 1
def takeinput(self):
if(self.on):
inpt = raw_input()
self.byteline[self.mempoint] = inpt
def giveoutput(self):
if(self.on):
print(chr(self.byteline[self.mempoint]))
self.outputstring += chr(self.byteline[self.mempoint])
def beginloop(self):
if(self.byteline[self.mempoint]!=0 and self.on):
self.loopspot.append(self.instructpoint)
self.on = True
self.loopdepth += 1
elif(self.btyeline[selt.mempoint] == 0 and self.on):
self.on = False
self.offdepth = self.loopdepth
def endloop(self):
if(self.byteline[self.mempoint]!=0 and self.on):
self.instructpoint =self.loopspot[self.loopdepth-1]
elif(self.byteline[self.mempoint]==0 and self.on):
self.loopspot.pop()
self.loopdepth -= 1
if(self.loopdepth == self.offdepth):
self.on = True
def contread(self):
self.instructpoint += 1
reader = bfbits()
commands = {'+' : reader.increment, '-' : reader.decrement,
'>' : reader.moveright, '<' : reader.moveleft,
'.' : reader.giveoutput, ',' : reader.takeinput,
'[' : reader.beginloop, ']' : reader.endloop, }
while(reader.instructpoint<len(bfinput)):
commands[bfinput[reader.instructpoint]]()
reader.contread()
print(reader.outputstring)