Advertisement
Guest User

Cork Interpreter

a guest
Sep 1st, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. #This runs a Cork file.
  2. #To use it, open your command line in the proper directory and run 'python [thisfile] [corkfileplaintext]'.
  3. #Tell me about your results and good luck!
  4. from sys import stdin, stdout, argv, exit
  5. def lify(instr):
  6.     z,result,ns=0,[],'0123456789'
  7.     while z<len(instr):
  8.         if instr[z] in '><+-.a,i':
  9.             result.append(instr[z])
  10.         if instr[z]=='!':
  11.             tmpr=''
  12.             z+=1
  13.             if not instr[z] in ns:
  14.                 exit('Parser found ! and expected a number; instead got {0} at instruction {1}'.format(instr[z],z))
  15.             while instr[z] in ns:
  16.                 tmpr+=instr[z]
  17.                 z+=1
  18.             result.append(int(tmpr))
  19.             z-=1
  20.         z+=1
  21.     return result
  22.  
  23. def run(instr):
  24.     code,ptr,cptr,result = lify(instr),128,0,''
  25.     tape = [0]*257
  26.     while ptr<len(tape) and ptr>=0 and cptr<len(code):
  27.         if code[cptr]=='>':
  28.             ptr+=1
  29.         if code[cptr]=='<':
  30.             ptr-=1
  31.         if code[cptr]=='+':
  32.             tape[ptr]+=1
  33.         if code[cptr]=='-':
  34.             tape[ptr]-=1
  35.         if code[cptr]=='.':
  36.             result+=(str(tape[ptr])+' ')
  37.         if code[cptr]=='a':
  38.             result+=chr(tape[ptr])
  39.         if code[cptr]==',':
  40.             stdout.write('#-->')
  41.         tape[ptr]=int(stdin.readline())
  42.         if code[cptr]=='i':
  43.             stdout.write('A-->')
  44.         tape[ptr]=int(stdin.readline())
  45.         if type(code[cptr])==int and tape[ptr]!=0:
  46.             cptr=code[cptr]
  47.     else:
  48.         cptr+=1
  49.     print result
  50. if __name__ == '__main__':
  51.     s = open(argv[1], 'r')
  52.     cfile = s.read()
  53.     cfile = filter(lambda x:x in '><+-ai.,!0123456789',cfile)
  54.     run(cfile)
  55.     s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement