Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Nibble:
- import sys
- chunks = []
- heap = []
- index = 0
- stackIndex = 0
- stack = []
- ptrRight = "0000"
- ptrLeft = "0001"
- ptrAdd = "0010"
- ptrSub = "0011"
- printState = "0100"
- readState = "0101"
- loopStart = "0110"
- loopEnd = "0111"
- bytes = {
- ">": { "byte": ptrRight,
- "F": (lambda stack: setattr(stack, "index", stack.index+1))},
- "<": { "byte": ptrLeft,
- "F": (lambda stack: setattr(stack, "index", stack.index-1))},
- "+": { "byte": ptrAdd,
- "F": (lambda stack: stack.updateIndex(1))},
- "-": { "byte": ptrSub,
- "F": (lambda stack: stack.updateIndex(-1))},
- ".": { "byte": printState,
- "F": (lambda stack: Nibble.sys.stdout.write(chr(stack.heap[stack.index])))},
- ",": { "byte": readState,
- "F": (lambda stack: stack.index)}, # Eh, deal with this later.
- "[": { "byte": loopStart,
- "F": (lambda stack: stack.ExecuteLoop())},
- "]": { "byte": loopEnd,
- "F": (lambda stack: stack.ExecuteLoop())}
- }
- def ExecuteLoop(self):
- if (self.chunks[self.stackIndex] == self.loopStart):
- self.stack.append(self.stackIndex)
- elif(self.chunks[self.stackIndex] == self.loopEnd):
- if ( self.heap[self.index] < 1 ):
- self.stack.pop()
- else:
- self.stackIndex = self.stack[-1]
- def updateIndex(self, val, set=False):
- while self.index > len(self.heap) - 1:
- self.heap.append(0)
- if set:
- self.heap[self.index] = val
- else:
- self.heap[self.index] += val
- @staticmethod
- def convert2Bytes(bf):
- for k in Nibble.bytes.iterkeys():
- bf = bf.replace(k, Nibble.bytes[k]['byte'])
- return bf
- @staticmethod
- def chunkstring(string):
- length = len(Nibble.ptrRight)
- return [string[0+i:length+i] for i in range(0, len(string), length)]
- @staticmethod
- def execute(biteme):
- stack = Nibble()
- stack.chunks = Nibble.chunkstring(biteme)
- while stack.stackIndex < len(stack.chunks):
- chunk = stack.chunks[stack.stackIndex]
- f = [Nibble.bytes[k]["F"] for k in Nibble.bytes if Nibble.bytes[k]["byte"] == chunk][0]
- f(stack)
- stack.stackIndex += 1
- print("")
- # Sample Program to output something.
- #0010001000100000000000000010001000100010001000100010011000010010001000100110000100100010001000000011011100000011011100010001001000100000000000000010001000100010001000100010001001100001001000100010001001100001001000100010000000110111000000110111000100010010000000000010001000100010001000100010001000100010011000010010000000000010000100110111000100110000000000000010001000100010000100010001000100010110001100000000001000010001011100010011000000000110001100000000001000000010000000100001000100010001011100000100000001000000010000000100
Add Comment
Please, Sign In to add comment