Created
May 18, 2015 00:19
-
-
Save Alex-Huleatt/7a62eb2a05d003229c8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match=0 #var for match brackets | |
i=0 #program counter | |
tape=[0]*30000 #"infinite" tape | |
t_cnt=15000 #tape counter | |
ops={'>':'p_cnt+=1', #map of chars to python code | |
'<':'t_cnt-=1', | |
'+':'tape[t_cnt]+=1', | |
'-':'tape[t_cnt]-=1', | |
'.':'print(chr(tape[t_cnt]),end="")', | |
',':'tape[t_cnt]=ord(input("\\n>>"))', | |
'[':'if(not tape[t_cnt]):match=1', | |
']':'if(tape[t_cnt]):m=-1'} #newlines for readability | |
drt={'[':1,']':-1} #wee map used to tell which direction to scan for matching brackets | |
def bf(prog): | |
global match,i,tape,t_cnt,ops,drt #Global variables because I am a bad person | |
while i<len(prog): | |
c = prog[i] | |
if match: #try to match brackets | |
match += drt[c] if c in drt else 0 | |
elif c in ops: | |
exec(ops[c],globals()) #Weird python 3 behavior with exec, had to make variables global. | |
i += -1 if match < 0 else 1 | |
bf('''++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.''') #Hello world program found on wikipedia |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment