refactor: improve the way we handle ignore loops
This commit is contained in:
37
dopey.py
37
dopey.py
@@ -1,6 +1,4 @@
|
|||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
from io import TextIOWrapper
|
|
||||||
from typing import Dict, List
|
from typing import Dict, List
|
||||||
|
|
||||||
class MismatchBracketException(Exception):
|
class MismatchBracketException(Exception):
|
||||||
@@ -32,11 +30,18 @@ class Operation:
|
|||||||
CLOSE_LOOP = "]"
|
CLOSE_LOOP = "]"
|
||||||
|
|
||||||
loop_stack = []
|
loop_stack = []
|
||||||
|
loop_ignore_stack = []
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def perform(cls, operation: str, program: List, switch: Dict) -> None:
|
def perform(cls, operation: str, program: List, switch: Dict) -> None:
|
||||||
|
# if operation does not exist, skip
|
||||||
if operation not in switch:
|
if operation not in switch:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# dont perform any non loop operations if inside an ignore loop
|
||||||
|
if len(cls.loop_ignore_stack) and operation != Operation.CLOSE_LOOP and operation != Operation.OPEN_LOOP:
|
||||||
|
return
|
||||||
|
|
||||||
switch[operation](program)
|
switch[operation](program)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -62,12 +67,10 @@ class Operation:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def output(cls, _) -> None:
|
def output(cls, _) -> None:
|
||||||
sys.stdout.write(chr(Memory.buffer[Memory.get_pointer()])) # TODO rollover if too big in ASCII
|
sys.stdout.write(chr(Memory.buffer[Memory.get_pointer()])) # TODO rollover if too big in ASCII
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def input(cls, _) -> None:
|
def input(cls, _) -> None:
|
||||||
# flush text to terminal before asking for input
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
if not len(Memory.input_buffer):
|
if not len(Memory.input_buffer):
|
||||||
input_ = sys.stdin.readline()
|
input_ = sys.stdin.readline()
|
||||||
Memory.input_buffer += list(input_)
|
Memory.input_buffer += list(input_)
|
||||||
@@ -77,26 +80,24 @@ class Operation:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def open_loop(cls, program: List) -> None:
|
def open_loop(cls, program: List) -> None:
|
||||||
if not Memory.buffer[Memory.get_pointer()]:
|
if not Memory.buffer[Memory.get_pointer()]:
|
||||||
# TODO redo this section to be more readable
|
# enter ignore loop if byte at pointer is zero
|
||||||
stack = [Operation.OPEN_LOOP]
|
cls.loop_ignore_stack.append(Operation.OPEN_LOOP)
|
||||||
while program[1] < len(program[0]):
|
|
||||||
operation = program[0][program[1]]
|
|
||||||
program[1] += 1
|
|
||||||
if operation == Operation.CLOSE_LOOP:
|
|
||||||
stack.pop()
|
|
||||||
if not len(stack):
|
|
||||||
return
|
|
||||||
elif operation == Operation.OPEN_LOOP:
|
|
||||||
stack.append(Operation.OPEN_LOOP)
|
|
||||||
raise MismatchBracketException
|
|
||||||
else:
|
else:
|
||||||
|
# save start loop position should we need to return
|
||||||
cls.loop_stack.append(program[1]-1)
|
cls.loop_stack.append(program[1]-1)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def close_loop(cls, program: List) -> None:
|
def close_loop(cls, program: List) -> None:
|
||||||
|
# check if in ignore loop
|
||||||
|
if len(cls.loop_ignore_stack):
|
||||||
|
cls.loop_ignore_stack.pop()
|
||||||
|
return
|
||||||
|
|
||||||
|
# check if there is a bracket mismatch
|
||||||
if not len(cls.loop_stack):
|
if not len(cls.loop_stack):
|
||||||
raise MismatchBracketException
|
raise MismatchBracketException
|
||||||
|
|
||||||
|
# check if we need to repeat
|
||||||
last_open_loop_pos = cls.loop_stack.pop()
|
last_open_loop_pos = cls.loop_stack.pop()
|
||||||
if Memory.buffer[Memory.get_pointer()]:
|
if Memory.buffer[Memory.get_pointer()]:
|
||||||
program[1] = last_open_loop_pos
|
program[1] = last_open_loop_pos
|
||||||
@@ -122,7 +123,7 @@ def main() -> None:
|
|||||||
file_location = sys.argv[1]
|
file_location = sys.argv[1]
|
||||||
|
|
||||||
file = open(file_location, "r")
|
file = open(file_location, "r")
|
||||||
program = [file.read(), 0] # TODO create files class
|
program = [file.read(), 0] # TODO create program class
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
switch = Operation.get_operations_in_switch()
|
switch = Operation.get_operations_in_switch()
|
||||||
|
|||||||
56
test.bf
56
test.bf
@@ -1,40 +1,20 @@
|
|||||||
--->--->>>>->->->>>>>-->>>>>>>>>>>>>>>>>>+>>++++++++++[
|
>>>->+>+++++>(++++++++++)[[>>>+<<<-]>+++++>+>>+[<<+>>>>>+<<<-]<-]>>>>[
|
||||||
<<++[
|
[>>>+>+<<<<-]+++>>+[<+>>>+>+<<<-]>>[>[[>>>+<<<-]<]<<++>+>>>>>>-]<-
|
||||||
--<+<<+<<+>>>>[
|
]+++>+>[[-]<+<[>+++++++++++++++++<-]<+]>>[
|
||||||
>[<->>+++>>[-]+++<<<+[<++>>+<--]]+>+++++[>>+++++++++<<-]
|
[+++++++++.-------->>>]+[-<<<]>>>[>>,----------[>]<]<<[
|
||||||
>>++++.[-]>>+[<<<<+>>+>>-]<<<<<<[>+<-]<<
|
<<<[
|
||||||
]++++++++++.[-]>++
|
>--[<->>+>-<<-]<[[>>>]+>-[+>>+>-]+[<<<]<-]>++>[<+>-]
|
||||||
]-->>[-->[-]>]<<[
|
>[[>>>]+[<<<]>>>-]+[->>>]<-[++>]>[------<]>+++[<<<]>
|
||||||
>>--[
|
|
||||||
-[
|
|
||||||
-[
|
|
||||||
-----[>+>+++++++<<+]-->>-.----->,[<->-]<[[<]+[->>]<-]<[<<,[-]]>>>>
|
|
||||||
]>
|
|
||||||
]<[
|
|
||||||
>-[+<+++]+<+++[+[---->]+<<<<<<[>>]<[-]]
|
|
||||||
>[<+[---->]++[<]<[>]>[[>]+>+++++++++<<-[<]]]>[>>>>]
|
|
||||||
]<[
|
|
||||||
-[[>+>+<<-]>[<+>-]++>+>>]<[<<++[-->>[-]]>[[-]>[<<+>>-]>]]
|
|
||||||
]<[
|
|
||||||
[[<<]-[>>]<+<-]>[-<+]<<[<<]-<[>[+>>]>[>]>[-]]
|
|
||||||
>[[+>>]<-->>[>]+>>>]
|
|
||||||
]<[
|
|
||||||
-[
|
|
||||||
--[+<<<<--[+>[-]>[<<+>+>-]<<[>>+<<-]]++[>]]
|
|
||||||
<<[>+>+<<-]>--[<+>-]++>>>
|
|
||||||
]<[<<<[-]+++>[-]>[<+>>>+<<-]+>>>]
|
|
||||||
]<[
|
|
||||||
+[[<]<<[<<]-<->>+>[>>]>[>]<-]+[-<+]<++[[>+<-]++<[<<->>+]<++]<
|
|
||||||
<<<<<< +> > >+> > >+[
|
|
||||||
<<< ->+>+>+[
|
|
||||||
<<<<<<< +>->+> > >->->+[
|
|
||||||
<<<<< ->+>+> >+>+[
|
|
||||||
<<<< ->->+>->+[
|
|
||||||
<<<<<<<<+>-> >+> > >->+>+[
|
|
||||||
<<<<< -> >+> >->+[
|
|
||||||
<<<< +>->+> >+]]]]]]]
|
|
||||||
+++[[>+<-]<+++]--->>[-[<->-]<++>>]++[[<->-]>>]>[>]
|
|
||||||
]<
|
|
||||||
]
|
|
||||||
]<
|
]<
|
||||||
|
]>[
|
||||||
|
-[+>>+>-]+>>+>>>+>[<<<]>->+>[
|
||||||
|
>[->+>+++>>++[>>>]+++<<<++<<<++[>>>]>>>]<<<[>[>>>]+>>>]
|
||||||
|
<<<<<<<[<<++<+[-<<<+]->++>>>++>>>++<<<<]<<<+[-<<<+]+>->>->>
|
||||||
|
]<<+<<+<<<+<<-[+<+<<-]+<+[
|
||||||
|
->+>[-<-<<[<<<]>[>>[>>>]<<+<[<<<]>-]]
|
||||||
|
<[<[<[<<<]>+>>[>>>]<<-]<[<<<]]>>>->>>[>>>]+>
|
||||||
|
]>+[-<<[-]<]-[
|
||||||
|
[>>>]<[<<[<<<]>>>>>+>[>>>]<-]>>>[>[>>>]<<<<+>[<<<]>>-]>
|
||||||
|
]<<<<<<[---<-----[-[-[<->>+++<+++++++[-]]]]<+<+]>
|
||||||
|
]>>
|
||||||
]
|
]
|
||||||
Reference in New Issue
Block a user