Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- nest=0
- chars="<>+-,.["
- def genProgram():
- global chars
- global nest
- output=""
- while randint(0,6) > 0:
- nextc=randint(0,6)
- output+=chars[nextc]
- if nextc == 6:
- nest+=1
- if nest < 20:
- output+=genProgram()
- nest-=1
- output+="]"
- return output
- def interpret(bf):
- tape=0
- memory=[0]
- point=0
- while tape<len(bf):
- c=bf[tape]
- if c=="<":
- if point==0:
- memory=[0]+memory
- else:
- point-=1
- if c==">":
- point+=1
- if point==len(memory):
- memory+=[0]
- if c=="+":
- memory[point]+=1
- if memory[point]==256:
- memory[point]=0
- if c=="-":
- memory[point]-=1
- if memory[point]==-1:
- memory[point]=255
- if c==".":
- print(chr(memory[point]), end='')
- if c==",":
- get=input()
- if len(get)==0:
- memory[point]=0
- else:
- memory[point]=ord(get[0])
- if c=="[":
- if memory[point]==0:
- openedCount=0
- while openedCount>=0 or bf[tape]!="]":
- tape+=1
- if bf[tape]=="[":
- openedCount+=1
- if bf[tape]=="]":
- openedCount-=1
- elif c=="]":
- if memory[point]!=0:
- openedCount=0
- while openedCount>=0 or bf[tape]!="[":
- tape-=1
- if bf[tape]=="]":
- openedCount+=1
- if bf[tape]=="[":
- openedCount-=1
- tape+=1
- foo=genProgram()
- interpret(foo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement