Esempio n. 1
0
 def __init__(self, filename):
     self.im = Image.open(filename)
     if self.im.mode != "RGB":
         self.im = self.im.convert("RGB")
     self.ip = IP(self.im.size)
     self.cell = Cell()
     self.output = ''
Esempio n. 2
0
 def __init__(self, script, in_fd=sys.stdin, out_fd=sys.stdout):
     self.out_fd = out_fd
     self.in_fd = in_fd
     self.script = script
     self.cell = Cell()
     self.stack = Stack()
     self.code = CodeFeeder(script, {'[': ']', ']': '['})
     self.opcodes = {
         ">": self.cell.right,
         "<": self.cell.left,
         "+": self.cell.inc,
         "-": self.cell.dec,
         ".": self.print_char,
         ",": self.read_char,
         "[": self.jz,
         "]": self.jnz,
         "\\": self.push,
         "/": self.pop,
         "!": self.pvt
     }
Esempio n. 3
0
class BrainFuckShitStack:

    def __init__(self, script, in_fd=sys.stdin, out_fd=sys.stdout):
        self.out_fd = out_fd
        self.in_fd = in_fd
        self.script = script
        self.cell = Cell()
        self.stack = Stack()
        self.code = CodeFeeder(script, {'[':']', ']':'['})
        self.opcodes = {
            ">": self.cell.right,
            "<": self.cell.left,
            "+": self.cell.inc,
            "-": self.cell.dec,
            ".": self.print_char,
            ",": self.read_char,
            "[": self.jz,
            "]": self.jnz,
            "\\": self.push,
            "/": self.pop,
            "!": self.pvt
        }


    def run(self):
        for op in self.code:
            self.opcodes.get(op, lambda: None)()

    def print_char(self):
        self.out_fd.write(self.cell.read())
        self.out_fd.flush()

    def read_char(self):
        self.cell.write(ord(self.in_fd.read(1)))

    def jz(self):
        if not self.cell:
            self.code.jump_ahead()
            
    def jnz(self):
        if self.cell:
            self.code.jump_back()

    def push(self):
        pushed = self.cell.read()
        self.stack.push(pushed)

    def pop(self):
        popped = ord(self.stack.pop())
        self.cell.write(popped)

    def pvt(self):
        old_stack = str(self.stack)
        old_code = str(self.code)
        self.stack = Stack(old_code)
        self.code.build_from(old_stack[::-1])
        self.code.loc_pointer -= 1 #inc after yield, need to fix
Esempio n. 4
0
class BrainFuckShitStack:
    def __init__(self, script, in_fd=sys.stdin, out_fd=sys.stdout):
        self.out_fd = out_fd
        self.in_fd = in_fd
        self.script = script
        self.cell = Cell()
        self.stack = Stack()
        self.code = CodeFeeder(script, {'[': ']', ']': '['})
        self.opcodes = {
            ">": self.cell.right,
            "<": self.cell.left,
            "+": self.cell.inc,
            "-": self.cell.dec,
            ".": self.print_char,
            ",": self.read_char,
            "[": self.jz,
            "]": self.jnz,
            "\\": self.push,
            "/": self.pop,
            "!": self.pvt
        }

    def run(self):
        for op in self.code:
            self.opcodes.get(op, lambda: None)()

    def print_char(self):
        self.out_fd.write(self.cell.read())
        self.out_fd.flush()

    def read_char(self):
        self.cell.write(ord(self.in_fd.read(1)))

    def jz(self):
        if not self.cell:
            self.code.jump_ahead()

    def jnz(self):
        if self.cell:
            self.code.jump_back()

    def push(self):
        pushed = self.cell.read()
        self.stack.push(pushed)

    def pop(self):
        popped = ord(self.stack.pop())
        self.cell.write(popped)

    def pvt(self):
        old_stack = str(self.stack)
        old_code = str(self.code)
        self.stack = Stack(old_code)
        self.code.build_from(old_stack[::-1])
        self.code.loc_pointer -= 1  #inc after yield, need to fix
Esempio n. 5
0
 def __init__(self, script, in_fd=sys.stdin, out_fd=sys.stdout):
     self.out_fd = out_fd
     self.in_fd = in_fd
     self.script = script
     self.cell = Cell()
     self.stack = Stack()
     self.code = CodeFeeder(script, {'[':']', ']':'['})
     self.opcodes = {
         ">": self.cell.right,
         "<": self.cell.left,
         "+": self.cell.inc,
         "-": self.cell.dec,
         ".": self.print_char,
         ",": self.read_char,
         "[": self.jz,
         "]": self.jnz,
         "\\": self.push,
         "/": self.pop,
         "!": self.pvt
     }
Esempio n. 6
0
 def setup(self, script=None):
     self.loc_pointer = 0
     self.cell = Cell()
     self.output = ''
     self.script = script or ''
     self.script_len = len(self.script)
Esempio n. 7
0
class BrainFuck:

    _token_inverse = {'[': ']', ']': '['}

    def __init__(self, script=None):
        self.setup(script)

    def setup(self, script=None):
        self.loc_pointer = 0
        self.cell = Cell()
        self.output = ''
        self.script = script or ''
        self.script_len = len(self.script)

    def run(self):
        for op in self.get_ops():
            if op == ">":
                self.cell.right()
            elif op == "<":
                self.cell.left()
            elif op == "+":
                self.cell.inc()
            elif op == "-":
                self.cell.dec()
            elif op == ".":
                self.print_char()
            elif op == ",":
                self.read_char()
            elif op == "[":
                self.jz()
            elif op == "]":
                self.jnz()

    def repl(self):
        while True:
            try:
                script = _input('f**k > ')
            except EOFError:
                break
            self.setup(script)
            self.run()
            print(repr(self.output))
            print('ord output:', [ord(c) for c in self.output])
            print("current cell", ord(self.cell))
            print("current cell # ", ord(self.cell.pointer))

    def get_ops(self):
        while self.loc_pointer < self.script_len:
            yield self.script[self.loc_pointer]
            self.loc_pointer += 1

    def print_char(self):
        self.output += self.cell.read()

    def read_char(self):
        self.cell.write(ord(stdin.read(1)))

    def find_right_matching(self, loc):
        token = self.script[loc]
        inverse = self.inverse[token]
        start = loc + 1
        end = start + self.script[start:].index(inverse)
        while token in self.script[start:end + 1]:
            next_token = self.script[loc:].index(token)
            start = self.find_right_matching(start + next_token) + 1
            end = start + self.script[start:].index(inverse)
        return end

    def find_left_matching(self, loc):
        token = self.script[loc]
        end = loc
        inverse = self._token_inverse[token]
        start = self.script[:end].rindex(inverse)
        while token in self.script[start:end]:
            next_token = self.script[:end].rindex(token)
            end = self.find_left_matching(next_token)
            start = self.script[:end].rindex(inverse)
        return start

    def jz(self):
        if not self.cell:
            self.loc_pointer = self.find_right_matching(self.loc_pointer)

    def jnz(self):
        if self.cell:
            self.loc_pointer = self.find_left_matching(self.loc_pointer) - 1
Esempio n. 8
0
 def setup(self, script=None):
     self.loc_pointer = 0
     self.cell = Cell()
     self.output = ''
     self.script = script or ''
     self.script_len = len(self.script)
Esempio n. 9
0
class BrainFuck:
    
    _token_inverse = {'[':']',
                      ']':'['}

    def __init__(self, script=None):
        self.setup(script)

    def setup(self, script=None):
        self.loc_pointer = 0
        self.cell = Cell()
        self.output = ''
        self.script = script or ''
        self.script_len = len(self.script)

    def run(self):
        for op in self.get_ops():
            if op == ">":
                self.cell.right()
            elif op == "<":
                self.cell.left()
            elif op == "+":
                self.cell.inc()
            elif op == "-":
                self.cell.dec()
            elif op == ".":
                self.print_char()
            elif op == ",":
                self.read_char()
            elif op == "[":
                self.jz()
            elif op == "]":
                self.jnz()

    def repl(self):
        while True:
            try:
                script = _input('f**k > ')
            except EOFError:
                break
            self.setup(script)
            self.run()
            print(repr(self.output))
            print('ord output:', [ ord(c) for c in self.output])
            print("current cell", ord(self.cell))
            print("current cell # ", ord(self.cell.pointer))

    def get_ops(self):
        while self.loc_pointer < self.script_len:
            yield self.script[self.loc_pointer]
            self.loc_pointer += 1
            
    def print_char(self):
        self.output += self.cell.read()

    def read_char(self):
        self.cell.write(ord(stdin.read(1)))

    def find_right_matching(self, loc):
        token = self.script[loc]
        inverse = self.inverse[token]
        start = loc+1
        end = start + self.script[start:].index(inverse)
        while token in self.script[start:end+1]:
            next_token = self.script[loc:].index(token)
            start = self.find_right_matching(start+next_token)+1
            end = start + self.script[start:].index(inverse)
        return end

    def find_left_matching(self, loc):
        token = self.script[loc]
        end = loc
        inverse = self._token_inverse[token]
        start = self.script[:end].rindex(inverse)
        while token in self.script[start:end]:
            next_token = self.script[:end].rindex(token)
            end = self.find_left_matching(next_token)
            start = self.script[:end].rindex(inverse)
        return start

    def jz(self):
        if not self.cell:
            self.loc_pointer = self.find_right_matching(self.loc_pointer)

    def jnz(self):
        if self.cell:
            self.loc_pointer = self.find_left_matching(self.loc_pointer) -1
Esempio n. 10
0
class ImageExecutor:

    def __init__(self, filename):
        self.im = Image.open(filename)
        if self.im.mode != "RGB":
            self.im = self.im.convert("RGB")
        self.ip = IP(self.im.size)
        self.cell = Cell()
        self.output = ''

    def run(self):
        for op in self.get_ops():
            if op == (255,0,0):
                self.cell.right()
            elif op == (128,0,0):
                self.cell.left()
            elif op == (0,255,0):
                self.cell.inc()
            elif op == (0,128,0):
                self.cell.dec()
            elif op == (0,0,255):
                self.print_char()
            elif op == (0,0,128):
                self.read_char()
            elif op == (255,255,0):
                self.jz()
            elif op == (128,128,0):
                self.jnz()
            elif op == (0,255,255):
                self.ip.right()
            elif op == (0,128,128):
                self.ip.left()

    def get_ops(self):
        for px in self.ip:
            yield self.im.getpixel(px)

    def print_char(self):
        self.output += self.cell.read()

    def read_char(self):
        cell.write(ord(stdin.read(1)))

    def jz(self):
        if not self.cell:
            for op in self.get_ops():
                if op == (128,128,0):
                    self.ip.next()
                    self.ip.reverse()
                    return
                elif op == (0,128,128):
                    self.ip.right()
                elif op == (0,255,255):
                    self.ip.left()

    def jnz(self):
        if self.cell:
            self.ip.reverse()
            for op in self.get_ops():
                if op == (255,255,0):
                    self.ip.reverse()
                    return
                elif op == (0,128,128):
                    self.ip.right()
                elif op == (0,255,255):
                    self.ip.left()