Guest User

Untitled

a guest
Oct 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. class Nibble:
  2. import sys
  3. chunks = []
  4. heap = []
  5. index = 0
  6. stackIndex = 0
  7. stack = []
  8.  
  9. ptrRight = "0000"
  10. ptrLeft = "0001"
  11.  
  12. ptrAdd = "0010"
  13. ptrSub = "0011"
  14.  
  15. printState = "0100"
  16. readState = "0101"
  17.  
  18. loopStart = "0110"
  19. loopEnd = "0111"
  20.  
  21. bytes = {
  22. ">": { "byte": ptrRight,
  23. "F": (lambda stack: setattr(stack, "index", stack.index+1))},
  24. "<": { "byte": ptrLeft,
  25. "F": (lambda stack: setattr(stack, "index", stack.index-1))},
  26. "+": { "byte": ptrAdd,
  27. "F": (lambda stack: stack.updateIndex(1))},
  28. "-": { "byte": ptrSub,
  29. "F": (lambda stack: stack.updateIndex(-1))},
  30. ".": { "byte": printState,
  31. "F": (lambda stack: Nibble.sys.stdout.write(chr(stack.heap[stack.index])))},
  32. ",": { "byte": readState,
  33. "F": (lambda stack: stack.index)}, # Eh, deal with this later.
  34. "[": { "byte": loopStart,
  35. "F": (lambda stack: stack.ExecuteLoop())},
  36. "]": { "byte": loopEnd,
  37. "F": (lambda stack: stack.ExecuteLoop())}
  38. }
  39.  
  40. def ExecuteLoop(self):
  41. if (self.chunks[self.stackIndex] == self.loopStart):
  42. self.stack.append(self.stackIndex)
  43. elif(self.chunks[self.stackIndex] == self.loopEnd):
  44. if ( self.heap[self.index] < 1 ):
  45. self.stack.pop()
  46. else:
  47. self.stackIndex = self.stack[-1]
  48.  
  49.  
  50.  
  51. def updateIndex(self, val, set=False):
  52. while self.index > len(self.heap) - 1:
  53. self.heap.append(0)
  54. if set:
  55. self.heap[self.index] = val
  56. else:
  57. self.heap[self.index] += val
  58.  
  59. @staticmethod
  60. def convert2Bytes(bf):
  61. for k in Nibble.bytes.iterkeys():
  62. bf = bf.replace(k, Nibble.bytes[k]['byte'])
  63. return bf
  64.  
  65. @staticmethod
  66. def chunkstring(string):
  67. length = len(Nibble.ptrRight)
  68. return [string[0+i:length+i] for i in range(0, len(string), length)]
  69.  
  70. @staticmethod
  71. def execute(biteme):
  72. stack = Nibble()
  73. stack.chunks = Nibble.chunkstring(biteme)
  74.  
  75. while stack.stackIndex < len(stack.chunks):
  76. chunk = stack.chunks[stack.stackIndex]
  77. f = [Nibble.bytes[k]["F"] for k in Nibble.bytes if Nibble.bytes[k]["byte"] == chunk][0]
  78. f(stack)
  79. stack.stackIndex += 1
  80. print("")
  81.  
  82. # Sample Program to output something.
  83. #0010001000100000000000000010001000100010001000100010011000010010001000100110000100100010001000000011011100000011011100010001001000100000000000000010001000100010001000100010001001100001001000100010001001100001001000100010000000110111000000110111000100010010000000000010001000100010001000100010001000100010011000010010000000000010000100110111000100110000000000000010001000100010000100010001000100010110001100000000001000010001011100010011000000000110001100000000001000000010000000100001000100010001011100000100000001000000010000000100
Add Comment
Please, Sign In to add comment