2022-04-03 16:45:35 +00:00
|
|
|
"""
|
|
|
|
@author: Mateusz779
|
|
|
|
"""
|
2022-04-03 15:37:02 +00:00
|
|
|
pointer = 0
|
|
|
|
cell = []
|
2022-04-03 15:41:33 +00:00
|
|
|
code = ""
|
|
|
|
|
2022-04-03 15:37:02 +00:00
|
|
|
|
|
|
|
def main():
|
2022-04-03 15:44:56 +00:00
|
|
|
"""
|
|
|
|
Main function
|
|
|
|
"""
|
|
|
|
|
2022-04-03 15:37:02 +00:00
|
|
|
global code, cell, pointer
|
|
|
|
code = input("Enter the code: ")
|
|
|
|
print("\n\n")
|
|
|
|
if code != "":
|
|
|
|
for i in range(len(code)):
|
2022-04-03 15:41:33 +00:00
|
|
|
interpreter(code[i], i)
|
2022-04-03 15:37:02 +00:00
|
|
|
print("\n\n")
|
|
|
|
|
|
|
|
def interpreter(znak, i):
|
2022-04-03 15:44:56 +00:00
|
|
|
"""
|
|
|
|
Brainfuck interpreter
|
2022-04-10 13:10:29 +00:00
|
|
|
|
2022-04-03 15:44:56 +00:00
|
|
|
> increment the data pointer (to point to the next cell to the right).
|
|
|
|
< decrement the data pointer (to point to the next cell to the left).
|
|
|
|
+ increment (increase by one) the byte at the data pointer.
|
|
|
|
- decrement (decrease by one) the byte at the data pointer.
|
|
|
|
. output the byte at the data pointer.
|
|
|
|
, accept one byte of input, storing its value in the byte at the data pointer.
|
|
|
|
[ if the byte at the data pointer is zero, then instead of
|
|
|
|
moving the instruction pointer forward to the next command,
|
|
|
|
jump it forward to the command after the matching ]
|
|
|
|
] if the byte at the data pointer is nonzero, then instead of moving
|
|
|
|
the instruction pointer forward to the next command,
|
|
|
|
jump it back to the command after the matching [.
|
|
|
|
"""
|
|
|
|
|
2022-04-03 15:37:02 +00:00
|
|
|
global code, cell, pointer
|
|
|
|
match znak:
|
|
|
|
case ">":
|
|
|
|
pointer += 1
|
2022-04-03 16:39:30 +00:00
|
|
|
if(pointer > len(cell) - 1):
|
|
|
|
cell.append(0)
|
2022-04-03 15:37:02 +00:00
|
|
|
case "<":
|
|
|
|
pointer -= 1
|
2022-04-03 15:41:33 +00:00
|
|
|
if pointer < 0:
|
2022-04-03 15:37:02 +00:00
|
|
|
pointer = 0
|
2022-04-03 16:39:30 +00:00
|
|
|
cell.insert(0, 0)
|
2022-04-03 15:37:02 +00:00
|
|
|
case "+":
|
|
|
|
try:
|
|
|
|
cell[pointer] += 1
|
|
|
|
except:
|
|
|
|
cell.append(0)
|
|
|
|
cell[pointer] += 1
|
|
|
|
case "-":
|
|
|
|
try:
|
|
|
|
cell[pointer] -= 1
|
2022-04-03 16:39:30 +00:00
|
|
|
if cell[pointer] < 0:
|
|
|
|
cell[pointer] = 0
|
2022-04-03 15:37:02 +00:00
|
|
|
except:
|
|
|
|
cell.append(0)
|
|
|
|
case ".":
|
2022-04-03 16:39:30 +00:00
|
|
|
try:
|
|
|
|
if cell[pointer] == 10 or cell[pointer] == 13:
|
|
|
|
print("\n")
|
|
|
|
else:
|
|
|
|
print(chr(cell[pointer]), end="")
|
|
|
|
except:
|
|
|
|
cell.append(0)
|
2022-04-03 15:37:02 +00:00
|
|
|
print(chr(cell[pointer]), end="")
|
|
|
|
case ",":
|
2022-04-03 16:39:30 +00:00
|
|
|
try:
|
|
|
|
cell[pointer] = ord(input("Enter a character: "))
|
|
|
|
except:
|
|
|
|
cell.append(0)
|
|
|
|
cell[pointer] = ord(input("Enter a character: "))
|
2022-04-03 15:37:02 +00:00
|
|
|
case "[":
|
2022-04-03 16:39:30 +00:00
|
|
|
try:
|
|
|
|
loop_begin = i
|
|
|
|
if cell[pointer] != 0:
|
|
|
|
loop_end = code.find("]", i)
|
|
|
|
for a in range(0, cell[pointer]-1):
|
|
|
|
for j in range(1, loop_end-loop_begin):
|
|
|
|
interpreter(code[loop_begin + j], loop_begin + j)
|
|
|
|
except:
|
|
|
|
cell.append(0)
|
2022-04-03 15:41:33 +00:00
|
|
|
|
2022-04-03 15:37:02 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|