Merge pull request #1 from Maks1mS/dev

Dev
This commit is contained in:
Maxim Slipenko 2021-09-17 22:29:52 +03:00 committed by GitHub
commit 05ee1b933c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 10 deletions

View File

@ -1,3 +1,8 @@
{
"python.linting.enabled": true
"python.linting.enabled": true,
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
}

View File

@ -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 = [

26
tests/test_emulator.py Normal file
View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -47,3 +47,29 @@ def from_tur(filename):
solution_dict[i][symbol] = action
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