[Tutor] BF Program hangs
Kaustubh Pratap chand
Warrior at programmer.net
Sat Jun 18 22:02:34 CEST 2011
Hello i made this program which interprets brainf***
But i don't understand why it doesn't ends....and also it doesn't print anything
import sys
cell=[0] * 30000
code_pointer=0
cell_pointer=0
close_brace_pos=0
open_brace_pos=0
bf=raw_input("Input bf program:")
while (code_pointer<len(bf)):
c=bf[code_pointer]
if c=='.':
sys.stdout.write(cell[cell_pointer])
code_pointer+=1
elif c==',':
cell[cell_pointer]=sys.stdin.read(1);
code_pointer+=1
elif c=='>':
cell_pointer+=1
code_pointer+=1
elif c=='<':
cell_pointer-=1
code_pointer+=1
elif c=='+':
cell[cell_pointer]+=1
code_pointer+=1
elif c=='-':
cell[cell_pointer]-=1
code_pointer+=1
elif c=='[':
if cell[cell_pointer]>0:
code_pointer+=1
else:
code_pointer=close_brace_pos+1
elif c==']':
close_brace_pos=code_pointer
code_pointer=open_brace_pos
else:
code_pointer+=1
For those of you who don't understand brainfuck its a very simple 8 instruction language
http://en.wikipedia.org/wiki/Brainfuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110618/7d432be8/attachment.html>
More information about the Tutor
mailing list