Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- #WARNING - very lazy coding ahead!
- bff=open(input("File containing BF code: "),'r')
- bfr=bff.read()
- bff.close()
- bf=""
- #get rid of anything extra
- for i in bfr:
- if i in "<>+-,.[]":
- bf+=i
- #match the []'s
- matches=[-1]*len(bf)
- c=0
- while c<len(bf):
- if bf[c]=="[":
- m=c
- d=0
- try:
- while not(bf[m]=="]" and d-1==0):
- if bf[m]=="]":
- d-=1
- if bf[m]=="[":
- d+=1
- m+=1
- matches[m]=c
- matches[c]=m
- except IndexError:
- print("ERROR: bracket mismatch!")
- sys.exit(2)
- c+=1
- #now we can piece it together
- ffm=""
- i=0
- while i<len(bf):
- if bf[i]=="<":
- ffm+=str(i)+";lft;0;"+str(i+1)+":"+str(i+1)+"\n"
- if bf[i]==">":
- ffm+=str(i)+";rgt;0;"+str(i+1)+":"+str(i+1)+"\n"
- if bf[i]=="+":
- ffm+=str(i)+";inc;0;"+str(i+1)+":"+str(i+1)+"\n"
- if bf[i]=="-":
- ffm+=str(i)+";dec;0;"+str(i+1)+":"+str(i+1)+"\n"
- if bf[i]==",":
- ffm+=str(i)+";inp;0;"+str(i+1)+":"+str(i+1)+"\n"
- if bf[i]==".":
- ffm+=str(i)+";out;0;"+str(i+1)+":"+str(i+1)+"\n"
- if bf[i]=="[":
- ffm+=str(i)+";nop;1;"+str(matches[i])+":"+str(i+1)+"\n"
- if bf[i]=="]":
- ffm+=str(i)+";nop;1;"+str(i+1)+":"+str(matches[i])+"\n"
- i+=1
- ffm+=str(i)+";hlt;0;"+str(i)+":"+str(i)
- print(ffm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement