From 2ed9adcc93966f1aebc926cf78b6ca808fc5d5ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Thu, 12 Jul 2018 19:43:33 +0300 Subject: [PATCH] Add a way to run the programs --- README | 2 ++ sf2xed.py | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README b/README index ea5a51f..72ab2f1 100644 --- a/README +++ b/README @@ -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 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. diff --git a/sf2xed.py b/sf2xed.py index 51f7e53..4e554b8 100644 --- a/sf2xed.py +++ b/sf2xed.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import enum +import sys from collections import namedtuple class actions(enum.Enum): @@ -264,7 +265,26 @@ def process_tape(tape, processed): 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(): + if len(sys.argv) == 2 and sys.argv[1] == '-r': + run = True + else: + run = False + program = input('program: ') start_noreachability, states_noreachability = turingify(parse_smallfuck(program)) @@ -278,7 +298,11 @@ def main(): tape = input('tape: ') if tape == '': break 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__': main()