Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Fri May 25 00:26:37 2018
- @author: Mauro
- """
- # BrainFuck interpreter
- # have a pointer that will travel an array
- class Pointer:
- def __init__(self):
- self.p = 0
- self.arr = [0 for i in range(10)]
- # >
- def increment(self):
- self.p += 1
- # <
- def decrement(self):
- self.p -= 1
- # +
- def increase(self):
- self.arr[self.p] += 1
- # -
- def decrease(self):
- self.arr[self.p] -= 1
- # .
- def putc(self, outstream):
- output = self.arr[self.p]
- print("OUTPUT:", output)
- outstream.append(output)
- # ,
- def getc(self):
- i = input("INPUT:")
- self.arr[self.p] = i[0]
- def get_value(self):
- return self.arr[self.p]
- class Instructions:
- def __init__(self, program):
- # the underlying pointer that moves on the array
- self.p = Pointer()
- # the string corresponding to the program
- self.prog = program
- # the instruction pointer
- self.ip = 0
- # parentheses stack
- self.pstack = []
- self.pcount = 0
- self.current_pcount = 0
- # the output stream
- self.out_stream = []
- # equivalent to char* c; *c++
- # returns null when the end of the string is reached
- def getc(self):
- if self.ip < len(self.prog):
- c = self.prog[self.ip]
- self.ip += 1
- return c
- else:
- return None
- # runs the program
- def interpret(self):
- c = self.getc()
- while c:
- print("-------", c, self.ip, "--------")
- if c == ">":
- self.p.increment()
- elif c == "<":
- self.p.decrement()
- elif c == "+":
- self.p.increase()
- elif c == "-":
- self.p.decrease()
- elif c == ".":
- self.p.putc(self.out_stream)
- elif c == ",":
- self.p.getc()
- elif c == "[":
- # puts the character index where the loop start
- known_starts = [start for start, end in self.pstack]
- if self.ip in known_starts:
- pass
- else:
- self.pstack.append([self.ip, -1])
- self.current_pcount = self.pcount
- self.pcount += 1
- # if the value of the pointer is 0 and the end instruction pos
- # is not known then eat characters till you find the matching
- # parentheses
- if self.p.get_value() == 0 and self.pstack[self.current_pcount][1] == -1:
- c = self.getc()
- # eat characters till the next MATCHING ]
- while c:
- if c == "[":
- self.pcount += 1
- elif c == "]":
- self.pcount -= 1
- # if the program finds the matching parenthes
- if self.current_pcount == self.pcount:
- break
- c = self.getc()
- # if the end is known go to end
- elif self.p.get_value() == 0:
- # end of loop
- self.ip = self.pstack[self.current_pcount][1]
- # pop the parentheses out of the stack (loop is skipped)
- self.pstack.pop(len(self.pstack)-1)
- self.pcount -= 1
- elif c == "]":
- self.pcount -= 1
- self.pstack[self.pcount][1] = self.ip
- # if the value is different then zero jump back to the
- # loop start
- if self.p.get_value != 0:
- self.ip = self.pstack[self.pcount][0] -1
- c = self.getc()
- # print the array
- print("".join([str(i) + "|" for i in self.p.arr]))
- # print the pointer position
- p_arrow = ""
- for i in range(len(self.p.arr)):
- # get the char length of the cell
- cellc = len(str(self.p.arr[i]))
- formatspec = "{:-^" + str(cellc) +"}|"
- if self.p.p == i:
- if cellc % 2 == 0:
- cell = formatspec.format("^^")
- else:
- cell = formatspec.format("^")
- else:
- cell = formatspec.format("")
- p_arrow += cell
- print(p_arrow)
- print(self.out_stream)
- print("".join([chr(c) for c in self.out_stream]))
- if __name__ == "__main__":
- # bfi = Instructions("++>+++++[<+>-]++++ ++++[<+++ +++ >-]<.")
- # bfi = Instructions("++++[>++++[-]<-]")
- bfi = Instructions("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.")
- bfi.interpret()
Advertisement
Add Comment
Please, Sign In to add comment