def execute(f): i = Image.open(f) m = i.load() ll = [] for j in range(i.size[1]): l = [] for k in range(i.size[0]): v = m[k, j] l.append((65536*v[0] + 256*v[1] + v[2]) % 11) ll.append(l) x = 0 y = 0 delta = [0, 1] list_ = [] while x in range(len(ll[0])) and y in range(len(ll)): if ll[y][x] == 8: delta = [delta[1], -delta[0]] elif ll[y][x] == 9: delta = [-delta[1], delta[0]] elif ll[y][x] == 10: pass else: list_.append(ll[y][x]) y += delta[0] x += delta[1] bf = '' for i in list_: bf += '>' if i == 0 else '<' if i == 1 else '+' if i == 2 else '-' if i == 3 else '.' if i == 4 else \ ',' if i == 5 else '[' if i == 6 else ']' if i == 7 else '' from brainfuck import execute execute(bf)
def execute(s): new = '' for i in s: o = ord(i) % 8 new += '+' if o == 0 else '-' if o == 1 else '<' if o == 2 else '>' if o == 3 else '.' if o == 4 else\ ',' if o == 5 else '[' if o == 6 else ']' from brainfuck import execute execute(new)
def execute(string): string = ''.join([i for i in string[1:] if i in ['0', '1']]) string = [string[i:i+3] for i in range(0, len(string), 3)] new = [] for i in string: new.append('+' if i == '000' else '-' if i == '001' else '>' if i == '010' else '<' if i == '011' else '.' if i == '100' else ',' if i == '101' else '[' if i == '110' else ']' if i == '111' else '') new = ''.join(new) from brainfuck import execute execute(new)
def replacefuck(string): debug = False program = [i for i in list(string[string.index("PROG:")+5:]) if i != '\n'] assert all(i not in program for i in ['+', '-', '>', '<', '[', ']', ',', '.']) for i in string.split('\n'): name = i[:i.index(":")] if name == "PROG": break bf = i[i.index(":")+1:] for i in bf: assert i in ['[', ']', ',', '.', '+', '-', '>', '<'] or i in name name2 = '' vars = '' intoVars = False for i in name: if i not in bf: assert not intoVars name2 += i else: intoVars = True vars += i while name2 in ''.join(program): if vars == '': program = list(''.join(program).replace(name2, bf)) break index = ''.join(program).index(name2) del program[index:index+len(name2)] thing = program[index:] del program[index:] newvars = [] vardict = {} for i in vars: newvars.append(int(thing[0], 16)) del thing[0] for i in range(len(vars)): vardict.update({vars[i]: newvars[i]}) for j in vars: while j in bf: dex = bf.index(j) char = bf[dex-1] bf = bf[:dex-1] + bf[dex:] bf = bf.replace(j, char * vardict[j], 1) thing = list(bf) + thing program += thing if debug or False: print("Final Brainfuck program: %s" % ''.join(program)) print("Program is now executing.") import time, os time.sleep(1) os.system(['clear', 'cls'][os.name == 'nt']) from brainfuck import execute execute(''.join(program))
def brainfuck_parse(bot, update, args): code = "".join(args) try: result = brainfuck.execute(code, [0] * 500) except: result = "Tu brainfuck es penoso, ha dado error." bot.sendMessage(update.message.chat_id, text=result)
def test_equivalence(self, program): """Sometimes this gets into infinite loops. Not sure what to do about that - valid (and even sane) programs might loop forever - e.g. daemons. Hypothesis supports timing out a test, but needs an example to finish. I.e., it won't kill a running infinite-looping example. """ state = random.getstate() inputter = '(lambda: [chr(random.randrange(256))])' outputter = 'io.StringIO("")' python = bf2py.main(program, inputter, outputter) exec(python) executed_python = locals()['output_stream'].read() random.setstate(state) executed_bf = brainfuck.execute(program, eval(inputter), eval(outputter)).read() self.assertEqual(executed_python, executed_bf) random.setstate(state)
from brainfuck import execute execute(r'>+[>,]<[<]>>[.>]') r''' This utility only reads in a file and prints it out Read: >+[>,] Rewind: <[<] Write: >>[.>] '''
from brainfuck import execute execute(r'''++ >[-]<[>+<[-]]>[<+>-]< #''') r''' ++ > [-] < [ > + < [-] ] > [ < + > - ] < # '''
from brainfuck import execute execute(r'''++ >[-]>[-]<<[>+>+<<-]>>[<<+>>-] #''') r''' ++ > [-] > [-] << [ > + > + << - ] >> [ << + >> - ] # '''
def test_execute_empty(self): program = "" execute(program)
from brainfuck import execute execute(r'''++>+++ >[-]<[>+<-]<[>+<-]>>[<<+>>-]< #''') r''' ++ > +++ > [-] < [ > + < - ] < [ > + < - ] >> [ << + >> - ] < #
op_cmp_min = '++', '+' op_lt = '+', '[-]' op_gt = '++', '>+<[>-<[-]]>[<+>-]<' al = 0, 1, 2 bl = 1, 1, 1 a_geq_b, b_geq_a = op_geq def mk_comptest(a, b, a_geq_b, b_geq_a): return '+' * a + '>' + '+' * b + '#>[-]>[-]>[-]>[-]+ [#<<<<[>>+>+<<<-]>>>[<<<+>>>-]+<[>-<[-]]>[>[-]<<<' + a_geq_b + '>>[-]]# <<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]+<[>-<[-]]>[>[-]<<<' + b_geq_a + '>>[-]]# >[<+<+>>-]<[>+<-]<[<<<->->>-] >>]<<<#' ai = iter(al) bi = iter(bl) try: print(a_geq_b, b_geq_a) print while True: a = ai.next() b = bi.next() try: execute(mk_comptest(a, b, a_geq_b, b_geq_a)) except: from traceback import print_exc print_exc() raise print except StopIteration: pass
from brainfuck import execute execute(r',[.[-],]') r''' , [ . [-], ] '''
from brainfuck import execute execute( r'++++++++++[>+++++++>++++++++++>+++>+>++++<<<<<-]>++.>+.+++++++..+++.>>>++++.<<++.<++++++++.--------.+++.------.--------.>+.>.' ) r''' +++++ +++++ initialize counter (cell #0) to 10 [ use loop to set the next four cells to 70/100/30/10/40 > +++++ ++ add 7 to cell #1 > +++++ +++++ add 10 to cell #2 > +++ add 3 to cell #3 > + add 1 to cell #4 > ++++ add 4 to cell #5 <<<<< - decrement counter (cell #0) ] > ++ . print 'H' > + . print 'e' +++++ ++ . print 'l' . print 'l' +++ . print 'o' >>> ++++ . print '\x2c' << ++ . print ' ' < +++++ +++ . print 'w' ----- --- . print 'o' +++ . print 'r' ----- - . print 'l' ----- --- . print 'd' > + . print '!' > . print '\n'
def main(): try: execute(input("bf> ")) except Exception as e: print(f"Error: {e}")
execute(r''' +++++ ++ > +++ === === === === === === === === === n;r d q rcp dcp rge cp0 cp1 cp2 =0= =1= =2= =3= =4= =5= =6= =7= =8= > [-] > [-] > [-] > [-] + > [-] > [-] > [-] TO 5 <<< WHILE [ DEBUG # CLR [-] TO 0 <<<<< DUP 3 4 [>>>+>+<<<<-]>>>>[<<<<+>>>>-] TO 1 <<< DUP 4 5 [>>>+>+<<<<-]>>>>[<<<<+>>>>-] GEQ >>>+ [<<<<[>>+>+<<<-]>>>[<<<+>>>-]+<[>-<[-]]>[>[-]<<<+>>[-]] <<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]+<[>-<[-]]>[>[-]<[-]] >[<+<+>>-]<[>+<-]<[<<<->->>-] >>]<<< BUP 6 7 [>+>+<<[-]]>>[<<+>>-] TO 6 < IF [[-] TO 3 <<< CLR [-] TO 2 < INC 1 + TO 1 < DUP 3 4 [>>+>+<<<-]>>>[<<<+>>>-] TO 3 < SUB 0 [<<<->>>-] TO 6 >>> END ] TO 5 < END ] TO 2 <<< # ''')
index = ''.join(program).index(name2) del program[index:index+len(name2)] thing = program[index:] del program[index:] newvars = [] vardict = {} for i in vars: newvars.append(int(thing[0], 16)) del thing[0] for i in range(len(vars)): vardict.update({vars[i]: newvars[i]}) for j in vars: while j in bf: dex = bf.index(j) char = bf[dex-1] bf = bf[:dex-1] + bf[dex:] bf = bf.replace(j, char * vardict[j], 1) thing = list(bf) + thing program += thing if debug or False: print("Final Brainfuck program: %s" % ''.join(program)) print("Program is now executing.") import time, os time.sleep(1) os.system(['clear', 'cls'][os.name == 'nt']) from brainfuck import execute execute(''.join(program)) if __name__ == '__main__': execute(open(input("Please input a replacefuck source file's filename.")).read())
from brainfuck import execute execute(r'''++>+++ >[-]>[-]<<<[>[>+>+<<-]>[<+>-]<<-]>>>[<<<+>>>-]<<< #''') r''' CLRALL 2 3 WHILE 0 ADD 1 2 3 ADD 2 1 DEC 0 END ''' r''' ++ > +++ > [-] > [-] << < [ > [ > + > + << -
from brainfuck import execute execute(r''' Store a value in array memory ============================= 0 1 2 3 4 5 6 7 0AB#?#?#?#?#?#?#?#? DIM A(8) >>++++++<+++ B=6:A=3:A(B)=A >[>>[-]<<-[>>+<<-]+>>]>[-]<<<[<<]>[>[>>]>+<<<[<<]>-]>-< < Retrieve a value from array memory ================================== 0 1 2 3 4 5 6 7 0AB#?#?#?#?#?#?#?#? DIM A(8) 'A(6)=3 >>++++++<[-] B=6:A=0:A=A(B) >[>>[-]<<-[>>+<<-]+>>]>[<<<[<<]>+>[>>]>-]<<<[<<]> copy A to somewhere >[>>[-]<<-[>>+<<-]+>>]>[-]<<<[<<]>[>[>>]>+<<<[<<]>-]>-< #''') r''' >> +++++ + < [-]
from brainfuck import execute execute(r''' [ CLR = [-] ADD = [<+>-]< DUP = >[-]>[-]<< [>+>+<<-]>>[<<+>>-] SWAP = >[-]< [>+<-]<[>+<-]>>[<<+>>-]< MUL = >[-]>[-]<< <[>[>+>+<<-] >[<+>-] <<-] >>>[<<<+>>>-]<<< IF = [ (your program here) [-]] ] ''')
from brainfuck import execute execute(r'''++++>++ #>[-]>[-]>[-]>[-]+ [#<<<<[>>+>+<<<-]>>>[<<<+>>>-]+<[>-<[-]]>[>[-]<<<A+A>>[-]]# <<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]+<[>-<[-]]>[>[-]<<<B++B>>[-]]# >[<+<+>>-]<[>+<-]<[<<<->->>-] >>]<<< #''') r''' ++++ > ++ # > [-] > [-] > [-] > [-] + [ # <<<< [ >> + > + <<< - ] >>> [ <<< + >>> - ] +
def test_execute_not_empty(self): program = "++++<<<<>>>>----." execute(program)