bf17 icon

BF compiler

bf17 | PRO | 04/01/17 05:28:52 PM UTC | 0 ⭐ | 377 👁️ | Never ⏰ | []
Python |

3.03 KB

|

None

|

0 👍

/

0 👎

# python 3.5
# read in a brainfuck file and generate a python program that executes the bf commands
# source file: a.txt
# output file: za.py
 
indent = "  "
indent_level = 0
ws = ""
steps = 0
 
f_out = open('za.py', 'w')
f_out.write("# Python 3.5\n\n"
 
"# First, count how many commands\n"
"f = open('a.txt', 'r')\n"
"prevch = ''\n"
"nextch = ''\n"
"command_count = 0\n"
"command_set = '><+-,.[]'\n"
"\n"
"while True:\n"
"   nextch = f.read(1)\n"
"   if(not nextch): break\n"
"\n"
"   if(nextch in command_set):\n"
"       command_count += 1\n"
"f.close()\n"
"# Build the executable================\n"
"from array import array\n"
"\n"
"P = 0\n"
"high_P = 0\n"
"steps = 0\n"
"indent = '  '\n"
"indent_level = 0\n\n"
"m_size = 32\n"
"m = array('B')\n"
"m = [ 0 for i in range(m_size)]\n"
"\n"
"def dump():\n"
"   print()\n"
"   print('|', end = '')\n"
"   for i in range(high_P + 1):\n"
"       if(i == P):\n"
"           left = '('\n"
"           right = ')'\n"
"       else: left = right = ' '\n"
"       print(left + '%03d' % i + ':' + '%03d' % m[i] + right, end = '|')\n"
"\n"
"def right():\n"
"   global P\n"
"   global high_P\n"
"   global steps\n"
"   steps += 1\n"
"   P += 1\n"
"   high_P = max(P, high_P)\n"
"\n"
"def left():\n"
"   global P\n"
"   global steps\n"
"   steps += 1\n"
"   P -= 1\n"
"\n"
"def plus():\n"
"   global P\n"
"   global steps\n"
"   steps += 1\n"
"   m[P] = (m[P] + 1) % 256\n"
"\n"
"def minus():\n"
"   global P\n"
"   global steps\n"
"   steps += 1\n"
"   m[P] = (m[P] - 1) % 256\n"
"\n"
"def key():\n"
"   global P\n"
"   global steps\n"
"   steps += 1\n"
"   j = input('char: ')\n"
"   if(len(j) > 0):\n"
"       m[P] = ord(j[0])\n"
"   else:\n"
"       m[P] = 0\n"
"\n"
"def emit():\n"
"   global P\n"
"   if((m[P] > 31 and m[P] < 127 or m[P] == 10)):\n"
"       print(chr(m[P]), end = '')\n"
"\n"
"def pause():\n"
"   dump()\n"
"   print()\n"
"   j = input('continue')\n"
"\n"
 
"# The Program:\n\n"
)
 
f_in = open('a.txt', 'r')
 
while True:
    nextch = f_in.read(1)
    if(not nextch): break
    
    if(nextch == ">"):
        ws = (indent * indent_level) + "right()\n"
        f_out.write(ws)
    elif(nextch == "<"):
        ws = (indent * indent_level) + "left()\n"
        f_out.write(ws)
    elif(nextch == "+"):
        ws = (indent * indent_level) + "plus()\n"
        f_out.write(ws)
    elif(nextch == "-"):
        ws = (indent * indent_level) + "minus()\n"
        f_out.write(ws)
    elif(nextch == ","):
        ws = (indent * indent_level) + "key()\n"
        f_out.write(ws)
    elif(nextch == "."):
        ws = (indent * indent_level) + "emit()\n"
        f_out.write(ws)
    elif(nextch == "["):
        ws = (indent * indent_level) + "while(m[P] > 0):\n"
        f_out.write(ws)
        indent_level += 1
        ws = (indent * indent_level) + "steps += 2\n"
        f_out.write(ws)
    elif(nextch == "]"):
        indent_level -= 1
    elif(nextch == "#"):
        ws = (indent * indent_level) + "pause()\n"
        f_out.write(ws)
    elif(nextch == "!"):
        ws = (indent * indent_level) + "dump()\n"
        f_out.write(ws)
    else: pass
 
f_out.write("print()\n")
f_out.write("print(command_count, 'commands')\n")
f_out.write("print(steps, 'steps')\n")
 
f_in.close()
f_out.close()

Comments