7
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										7
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							@@ -1,3 +1,8 @@
 | 
			
		||||
{
 | 
			
		||||
    "python.linting.enabled": true
 | 
			
		||||
    "python.linting.enabled": true,
 | 
			
		||||
    "python.testing.pytestArgs": [
 | 
			
		||||
        "tests"
 | 
			
		||||
    ],
 | 
			
		||||
    "python.testing.unittestEnabled": false,
 | 
			
		||||
    "python.testing.pytestEnabled": true,
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								setup.py
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								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 = [
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								tests/test_emulator.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								tests/test_emulator.py
									
									
									
									
									
										Normal 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
 | 
			
		||||
 | 
			
		||||
@@ -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()
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
		Reference in New Issue
	
	Block a user