Advertisement
bf17

BF compiler

Apr 1st, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. # python 3.5
  2. # read in a brainfuck file and generate a python program that executes the bf commands
  3. # source file: a.txt
  4. # output file: za.py
  5.  
  6. indent = "  "
  7. indent_level = 0
  8. ws = ""
  9. steps = 0
  10.  
  11. f_out = open('za.py', 'w')
  12. f_out.write("# Python 3.5\n\n"
  13.  
  14. "# First, count how many commands\n"
  15. "f = open('a.txt', 'r')\n"
  16. "prevch = ''\n"
  17. "nextch = ''\n"
  18. "command_count = 0\n"
  19. "command_set = '><+-,.[]'\n"
  20. "\n"
  21. "while True:\n"
  22. "   nextch = f.read(1)\n"
  23. "   if(not nextch): break\n"
  24. "\n"
  25. "   if(nextch in command_set):\n"
  26. "       command_count += 1\n"
  27. "f.close()\n"
  28. "# Build the executable================\n"
  29. "from array import array\n"
  30. "\n"
  31. "P = 0\n"
  32. "high_P = 0\n"
  33. "steps = 0\n"
  34. "indent = '  '\n"
  35. "indent_level = 0\n\n"
  36. "m_size = 32\n"
  37. "m = array('B')\n"
  38. "m = [ 0 for i in range(m_size)]\n"
  39. "\n"
  40. "def dump():\n"
  41. "   print()\n"
  42. "   print('|', end = '')\n"
  43. "   for i in range(high_P + 1):\n"
  44. "       if(i == P):\n"
  45. "           left = '('\n"
  46. "           right = ')'\n"
  47. "       else: left = right = ' '\n"
  48. "       print(left + '%03d' % i + ':' + '%03d' % m[i] + right, end = '|')\n"
  49. "\n"
  50. "def right():\n"
  51. "   global P\n"
  52. "   global high_P\n"
  53. "   global steps\n"
  54. "   steps += 1\n"
  55. "   P += 1\n"
  56. "   high_P = max(P, high_P)\n"
  57. "\n"
  58. "def left():\n"
  59. "   global P\n"
  60. "   global steps\n"
  61. "   steps += 1\n"
  62. "   P -= 1\n"
  63. "\n"
  64. "def plus():\n"
  65. "   global P\n"
  66. "   global steps\n"
  67. "   steps += 1\n"
  68. "   m[P] = (m[P] + 1) % 256\n"
  69. "\n"
  70. "def minus():\n"
  71. "   global P\n"
  72. "   global steps\n"
  73. "   steps += 1\n"
  74. "   m[P] = (m[P] - 1) % 256\n"
  75. "\n"
  76. "def key():\n"
  77. "   global P\n"
  78. "   global steps\n"
  79. "   steps += 1\n"
  80. "   j = input('char: ')\n"
  81. "   if(len(j) > 0):\n"
  82. "       m[P] = ord(j[0])\n"
  83. "   else:\n"
  84. "       m[P] = 0\n"
  85. "\n"
  86. "def emit():\n"
  87. "   global P\n"
  88. "   if((m[P] > 31 and m[P] < 127 or m[P] == 10)):\n"
  89. "       print(chr(m[P]), end = '')\n"
  90. "\n"
  91. "def pause():\n"
  92. "   dump()\n"
  93. "   print()\n"
  94. "   j = input('continue')\n"
  95. "\n"
  96.  
  97. "# The Program:\n\n"
  98. )
  99.  
  100. f_in = open('a.txt', 'r')
  101.  
  102. while True:
  103.     nextch = f_in.read(1)
  104.     if(not nextch): break
  105.    
  106.     if(nextch == ">"):
  107.         ws = (indent * indent_level) + "right()\n"
  108.         f_out.write(ws)
  109.     elif(nextch == "<"):
  110.         ws = (indent * indent_level) + "left()\n"
  111.         f_out.write(ws)
  112.     elif(nextch == "+"):
  113.         ws = (indent * indent_level) + "plus()\n"
  114.         f_out.write(ws)
  115.     elif(nextch == "-"):
  116.         ws = (indent * indent_level) + "minus()\n"
  117.         f_out.write(ws)
  118.     elif(nextch == ","):
  119.         ws = (indent * indent_level) + "key()\n"
  120.         f_out.write(ws)
  121.     elif(nextch == "."):
  122.         ws = (indent * indent_level) + "emit()\n"
  123.         f_out.write(ws)
  124.     elif(nextch == "["):
  125.         ws = (indent * indent_level) + "while(m[P] > 0):\n"
  126.         f_out.write(ws)
  127.         indent_level += 1
  128.         ws = (indent * indent_level) + "steps += 2\n"
  129.         f_out.write(ws)
  130.     elif(nextch == "]"):
  131.         indent_level -= 1
  132.     elif(nextch == "#"):
  133.         ws = (indent * indent_level) + "pause()\n"
  134.         f_out.write(ws)
  135.     elif(nextch == "!"):
  136.         ws = (indent * indent_level) + "dump()\n"
  137.         f_out.write(ws)
  138.     else: pass
  139.  
  140. f_out.write("print()\n")
  141. f_out.write("print(command_count, 'commands')\n")
  142. f_out.write("print(steps, 'steps')\n")
  143.  
  144. f_in.close()
  145. f_out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement