Add a way to run the programs

This commit is contained in:
Juhani Krekelä 2018-07-12 19:43:33 +03:00
parent ca314ee310
commit 2ed9adcc93
2 changed files with 27 additions and 1 deletions

2
README
View File

@ -10,3 +10,5 @@ To produce a tape usable by the step command, input your tape in the form:
Which says there is a tape with 5 cells, second from right being 1 and all Which says there is a tape with 5 cells, second from right being 1 and all
others being 0, and the tape head is in the middle of this tape others being 0, and the tape head is in the middle of this tape
You can also run the compiled result by giving sf2xed.py the -r argument.

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import enum import enum
import sys
from collections import namedtuple from collections import namedtuple
class actions(enum.Enum): class actions(enum.Enum):
@ -264,7 +265,26 @@ def process_tape(tape, processed):
return ' '.join(processed_tape) + ' z' return ' '.join(processed_tape) + ' z'
def run_replacements(replacements, processed_tape):
while True:
made_replacement = False
for start, end in replacements:
if start in processed_tape:
processed_tape = processed_tape.replace(start, end)
made_replacement = True
break
if not made_replacement:
break
print(processed_tape)
def main(): def main():
if len(sys.argv) == 2 and sys.argv[1] == '-r':
run = True
else:
run = False
program = input('program: ') program = input('program: ')
start_noreachability, states_noreachability = turingify(parse_smallfuck(program)) start_noreachability, states_noreachability = turingify(parse_smallfuck(program))
@ -278,7 +298,11 @@ def main():
tape = input('tape: ') tape = input('tape: ')
if tape == '': break if tape == '': break
tape = tape.split(' ') tape = tape.split(' ')
print(process_tape(tape, processed)) processed_tape = process_tape(tape, processed)
print(processed_tape)
if run:
run_replacements(replacements, processed_tape)
if __name__ == '__main__': if __name__ == '__main__':
main() main()