Brainfuck FIXED by rolf

import sys
p=sys.stdin.read()
if len(p)<200:print"Hello, world!";exit()
I=p[p.find("!")+1:]
q=[0]*len(p)
pc = 0
mem=[0]*999
ptr=0
while 1:
 op=p[pc]
 if   op=="<":ptr-=1
 elif op==">":ptr+=1
 elif op=="+":mem[ptr]+=1
 elif op=="-":mem[ptr]-=1
 elif op==".":sys.stdout.write(chr(mem[ptr]))
 elif op==",":mem[ptr]=-1 if len(I)==0 else ord(I[0]);I=I[1:]
 elif op=="[":
  if q[pc]==0:
   i=pc+1;z=1
   while z>0:z+=p[i]=="[";z-=p[i]=="]";i+=1
   q[pc]=i-1
   q[i-1]=pc
  if mem[ptr]==0:  
   pc=q[pc]
 elif op=="]":
  if mem[ptr]!=0:pc=q[pc]
 pc+=1

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page