-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbf.py
executable file
·167 lines (131 loc) · 3.71 KB
/
bf.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python
import sys
import itertools
def group( s ):
gg = [(len(list(group)),name) for name, group in itertools.groupby(s)]
out = []
for i in gg:
if( i[1] in ',.[]'):
'''expand back out'''
out+= i[0] *[( 1 ,i[1] )]
continue
out+=[i]
return out
def aprint( a , b ):
if a != b:
print a
assert( a == b )
aprint( group( ">><<[[]]" ) , [(2,'>'),(2,'<'),(1,'['),(1,'['),(1,']'),(1,']')] )
def stripNonBf( s ):
ret = ""
for c in s:
if c in '[]<>-+,.':
ret += c
return ret
#runs extended brainfuck that is compiled by this program from a file every run
class Ebf :
dp=0
ip=0
loopStack=[]
instructions=[]
tape = [0]
die=False
def __init__( self , f ):
full = f.read()
full = stripNonBf( full )
full = group( full )
self.instructions = full
def add( self , count ):
dp = self.dp
tape = self.tape
count = count % 255
tape[dp] = ( tape[dp] + count ) % 255
self.ip += 1
def sub( self, count ):
count = count % 255
self.tape[ self.dp] = ( self.tape[ self.dp] - count )
if self.tape[ self.dp] < 0 :
self.tape[ self.dp] = -self.tape[ self.dp]
self.ip += 1
def dec( self, count ):
self.dp -= count
if self.dp < 0:
self.die = True
self.ip += 1
def inc( self, count ):
self.dp += count
if self.dp >= len( self.tape ):
self.tape += [0]*count
self.ip += 1
def out( self , noUse ):
del noUse
c = self.tape[ self.dp ]
try :
c = chr ( c )
except Exception as e:
pass
else:
sys.stdout.write( c )
sys.stdout.flush()
self.ip += 1
def _in( self , noUse ):
del noUse
c = sys.stdin.read( 1 )
if len( c ) == 0 :
self.die = True
return
self.tape[self.dp] = ord(c)
self.ip += 1
def _open( self , noUse ):
del noUse
if self.tape[self.dp] != 0:
self.loopStack.append( self.ip )
self.ip += 1
else:
level = 1
while True:
try:
self.ip += 1
if self.instructions[self.ip][1] == ']' :
level -= 1
elif self.instructions[self.ip] [1]== '[':
level += 1
if level == 0 :
self.ip += 1
return
except IndexError as e:
print "out of bounds instruction. ip=",self.ip
print e
die = True;
return
def close( self , noUse ):
del noUse
self.ip = self.loopStack[-1]
self.loopStack.pop()
ftable = {
'+' : add,
'-' : sub,
'<' : dec,
'>' : inc,
'.' : out,
',' : _in,
'[' : _open,
']' : close
}
def dump( self ):
print "ins:",self.instructions[self.ip],"ip:",self.ip,"loopStack:",self.loopStack,"tape[dp]:",self.tape[ self.dp] ,"dp",self.dp
def run(self):
while not self.die:
ins = self.instructions[ self.ip ]
if ins[1] == '.' :
pass
self.ftable [ ins[1] ] (self,ins[0] )
if self.ip >= len( self.instructions ):
self.die = True
def main():
sys.stdin.softspace = False
e = Ebf( file( "./hanoi.b", "rb" ) )
e.run()
#print e.instructions
if __name__ == "__main__":
main()