From c2712885d5f05d0c583e37212258794008f49f45 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko <36362599+Maks1mS@users.noreply.github.com> Date: Fri, 17 Sep 2021 21:10:49 +0300 Subject: [PATCH 1/4] migrate to export file of turing emulator --- turinglab/__main__.py | 6 +++--- turinglab/emulator.py | 2 +- turinglab/input.py | 28 +++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/turinglab/__main__.py b/turinglab/__main__.py index e93533a..7b6a88e 100644 --- a/turinglab/__main__.py +++ b/turinglab/__main__.py @@ -1,13 +1,13 @@ import sys -from turinglab.input import from_tur +from turinglab.input import from_file from turinglab.emulator import Emulator from turinglab.output import to_docx def main(): - tur, input_string, docx = sys.argv[1:] + program_file, input_string, docx = sys.argv[1:] - [program,_,_] = from_tur(tur) + program = from_file(program_file) tm = Emulator(program, input_string) diff --git a/turinglab/emulator.py b/turinglab/emulator.py index 6808c3f..aae7309 100644 --- a/turinglab/emulator.py +++ b/turinglab/emulator.py @@ -24,7 +24,7 @@ class Emulator(): symbol = self.tape[self.head] try: - symbol, direction, state = self.instructions[self.current_state][symbol] + symbol, direction, state = self.instructions[symbol][self.current_state] self.tape[self.head] = symbol self.current_state = state diff --git a/turinglab/input.py b/turinglab/input.py index 3c01bcb..755201d 100644 --- a/turinglab/input.py +++ b/turinglab/input.py @@ -46,4 +46,30 @@ def from_tur(filename): solution_dict[i][symbol] = action - return solution_dict, task, description \ No newline at end of file + return solution_dict, task, description + +def from_file(filename: str): + f = open(filename, "r") + + program = dict() + + for line in f: + symbol, *actions = line.replace("\n", "").split('\t') + + if symbol == " ": + symbol = "λ" + + program[symbol] = [] + + for action in actions: + new_symbol, direction, new_state = list(action) + + if new_symbol == "_": + new_symbol = 'λ' + new_state = int(new_state) - 1 + + program[symbol].append([new_symbol, direction, new_state]) + + f.close() + + return program \ No newline at end of file From d619623eb346d25ba0ec25309480fcc213984655 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko <36362599+Maks1mS@users.noreply.github.com> Date: Fri, 17 Sep 2021 21:56:12 +0300 Subject: [PATCH 2/4] migrate to argparse --- turinglab/__main__.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/turinglab/__main__.py b/turinglab/__main__.py index 7b6a88e..fb96ee2 100644 --- a/turinglab/__main__.py +++ b/turinglab/__main__.py @@ -1,15 +1,26 @@ import sys +from argparse import ArgumentParser from turinglab.input import from_file from turinglab.emulator import Emulator from turinglab.output import to_docx - + +def get_parser() -> ArgumentParser: + parser = ArgumentParser() + parser.add_argument("input_file", type=str, help="Path to file with program") + parser.add_argument("input_string", type=str, help="Input symbols") + parser.add_argument("output_file", type=str, help="Output file") + + return parser def main(): - program_file, input_string, docx = sys.argv[1:] + parser = get_parser() - program = from_file(program_file) + args = parser.parse_args() - tm = Emulator(program, input_string) + + program = from_file(args.input_file) + + tm = Emulator(program, args.input_string) data = [tm.info()] @@ -17,7 +28,7 @@ def main(): tm.step() data.append(tm.info()) - to_docx(docx, data) + to_docx(args.output_file, data) if __name__ == '__main__': main() \ No newline at end of file From 44eea85a282bda81b174bc5099efd0bfbd43525f Mon Sep 17 00:00:00 2001 From: Maxim Slipenko <36362599+Maks1mS@users.noreply.github.com> Date: Fri, 17 Sep 2021 21:56:19 +0300 Subject: [PATCH 3/4] add tests --- .vscode/settings.json | 7 ++++++- tests/test_emulator.py | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/test_emulator.py diff --git a/.vscode/settings.json b/.vscode/settings.json index f2d90cb..882c8c5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,8 @@ { - "python.linting.enabled": true + "python.linting.enabled": true, + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true, } \ No newline at end of file diff --git a/tests/test_emulator.py b/tests/test_emulator.py new file mode 100644 index 0000000..18010e4 --- /dev/null +++ b/tests/test_emulator.py @@ -0,0 +1,26 @@ +from turinglab.emulator import Emulator +from turinglab.output import to_docx + +def test_emulator(): + + blank_symbol = '_' + + instructions = { + '0': [['1', '>', 0], ['0', '<', 1]], + '1': [['0', '>', 0], ['1', '<', 1]], + blank_symbol: [[blank_symbol, '<', 1], [blank_symbol, '>', -1]] + } + + input_string = '010' + + tm = Emulator(instructions, input_string, blank_symbol) + + while not tm.stopped: + tm.step() + + head, _, tape = tm.info() + output_string = "".join(tape.values()).strip(blank_symbol) + + assert output_string == '101' + assert head == 0 + From 46e07d398834dbfecee0a56aee4a7b1a0078f279 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko <36362599+Maks1mS@users.noreply.github.com> Date: Fri, 17 Sep 2021 22:28:54 +0300 Subject: [PATCH 4/4] migrate to setuptools_scm --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ae0ef1f..1bae99c 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,8 @@ from setuptools import setup setup( name='turinglab', - version='0.1.0', + use_scm_version=True, + setup_requires=['setuptools_scm'], author='Maxim Slipenko', packages=['turinglab'], install_requires = [