Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This runs a Cork file.
- #To use it, open your command line in the proper directory and run 'python [thisfile] [corkfileplaintext]'.
- #Tell me about your results and good luck!
- from sys import stdin, stdout, argv, exit
- def lify(instr):
- z,result,ns=0,[],'0123456789'
- while z<len(instr):
- if instr[z] in '><+-.a,i':
- result.append(instr[z])
- if instr[z]=='!':
- tmpr=''
- z+=1
- if not instr[z] in ns:
- exit('Parser found ! and expected a number; instead got {0} at instruction {1}'.format(instr[z],z))
- while instr[z] in ns:
- tmpr+=instr[z]
- z+=1
- result.append(int(tmpr))
- z-=1
- z+=1
- return result
- def run(instr):
- code,ptr,cptr,result = lify(instr),128,0,''
- tape = [0]*257
- while ptr<len(tape) and ptr>=0 and cptr<len(code):
- if code[cptr]=='>':
- ptr+=1
- if code[cptr]=='<':
- ptr-=1
- if code[cptr]=='+':
- tape[ptr]+=1
- if code[cptr]=='-':
- tape[ptr]-=1
- if code[cptr]=='.':
- result+=(str(tape[ptr])+' ')
- if code[cptr]=='a':
- result+=chr(tape[ptr])
- if code[cptr]==',':
- stdout.write('#-->')
- tape[ptr]=int(stdin.readline())
- if code[cptr]=='i':
- stdout.write('A-->')
- tape[ptr]=int(stdin.readline())
- if type(code[cptr])==int and tape[ptr]!=0:
- cptr=code[cptr]
- else:
- cptr+=1
- print result
- if __name__ == '__main__':
- s = open(argv[1], 'r')
- cfile = s.read()
- cfile = filter(lambda x:x in '><+-ai.,!0123456789',cfile)
- run(cfile)
- s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement