December SPECIAL! For a limited time only. Get 20% discount on a LIFETIME PRO account!Want more features on Pastebin? Sign Up, it's FREE!
tweet
Guest

Untitled

By: a guest on Sep 12th, 2015  |  syntax: Python  |  size: 0.82 KB  |  views: 70  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print  |  QR code  |  clone
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def bf_interpreter(code):
  2.         tape = [0 for x in range(30000)]
  3.         ptr = 0
  4.         x = 0
  5.  
  6.         while x < len(code):
  7.                 if code[x] == ">": ptr += 1
  8.                 elif code[x] == "<": ptr -= 1
  9.                 elif code[x] == "+": tape[ptr] += 1
  10.                 elif code[x] == "-": tape[ptr] -= 1
  11.                 elif code[x] == ".": print tape[ptr],
  12.                 elif code[x] == ",": tape[ptr] = int(raw_input(""))
  13.                 elif code[x] == "[":
  14.                         if tape[ptr] == 0:
  15.                                 skip = 0
  16.                                 for y in range(x+1, len(code)):
  17.                                         if code[y] == "[":
  18.                                                 skip += 1
  19.                                         if code[y] == "]":
  20.                                                 if skip != 0:
  21.                                                         skip -= 1
  22.                                                 else:
  23.                                                         x = y
  24.                                                         break
  25.                 elif code[x] == "]":
  26.                         if tape[ptr] != 0:
  27.                                 skip = 0
  28.                                 for y in range(x-1)[::-1]:
  29.                                         if code[y] == "]":
  30.                                                 skip += 1
  31.                                         if code[y] == "[":
  32.                                                 if skip != 0:
  33.                                                         skip -= 1
  34.                                                 else:
  35.                                                         x = y
  36.                                                         break
  37.                 x += 1
clone this paste RAW Paste Data
Top