Advertisement
enoua5

bf2ffm.py

Jun 19th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. import sys
  2. #WARNING - very lazy coding ahead!
  3. bff=open(input("File containing BF code: "),'r')
  4. bfr=bff.read()
  5. bff.close()
  6. bf=""
  7. #get rid of anything extra
  8. for i in bfr:
  9.     if i in "<>+-,.[]":
  10.         bf+=i
  11. #match the []'s
  12. matches=[-1]*len(bf)
  13. c=0
  14. while c<len(bf):
  15.     if bf[c]=="[":
  16.         m=c
  17.         d=0
  18.         try:
  19.             while not(bf[m]=="]" and d-1==0):
  20.                 if bf[m]=="]":
  21.                     d-=1
  22.                 if bf[m]=="[":
  23.                     d+=1
  24.                 m+=1
  25.             matches[m]=c
  26.             matches[c]=m
  27.         except IndexError:
  28.             print("ERROR: bracket mismatch!")
  29.             sys.exit(2)
  30.     c+=1
  31. #now we can piece it together
  32. ffm=""
  33. i=0
  34. while i<len(bf):
  35.     if bf[i]=="<":
  36.         ffm+=str(i)+";lft;0;"+str(i+1)+":"+str(i+1)+"\n"
  37.     if bf[i]==">":
  38.         ffm+=str(i)+";rgt;0;"+str(i+1)+":"+str(i+1)+"\n"
  39.     if bf[i]=="+":
  40.         ffm+=str(i)+";inc;0;"+str(i+1)+":"+str(i+1)+"\n"
  41.     if bf[i]=="-":
  42.         ffm+=str(i)+";dec;0;"+str(i+1)+":"+str(i+1)+"\n"
  43.     if bf[i]==",":
  44.         ffm+=str(i)+";inp;0;"+str(i+1)+":"+str(i+1)+"\n"
  45.     if bf[i]==".":
  46.         ffm+=str(i)+";out;0;"+str(i+1)+":"+str(i+1)+"\n"
  47.     if bf[i]=="[":
  48.         ffm+=str(i)+";nop;1;"+str(matches[i])+":"+str(i+1)+"\n"
  49.     if bf[i]=="]":
  50.         ffm+=str(i)+";nop;1;"+str(i+1)+":"+str(matches[i])+"\n"
  51.     i+=1
  52. ffm+=str(i)+";hlt;0;"+str(i)+":"+str(i)
  53. print(ffm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement