コード例 #1
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
コード例 #2
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
コード例 #3
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
     }
コード例 #4
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
     }
コード例 #5
0
 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
コード例 #6
0
 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