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/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 = [ 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 + diff --git a/turinglab/__main__.py b/turinglab/__main__.py index e93533a..fb96ee2 100644 --- a/turinglab/__main__.py +++ b/turinglab/__main__.py @@ -1,15 +1,26 @@ import sys -from turinglab.input import from_tur +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(): - tur, input_string, docx = sys.argv[1:] + parser = get_parser() - [program,_,_] = from_tur(tur) + 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 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