예제 #1
0
def main():
    """Main method opens the input file, creates a turing machine with the program file,
    and then runs the turing machine on the lines in the input file"""
    infile = open(infilename)

    # Creates a turing machine with the instructions from the given file
    turing_machine = TuringMachine(programfilename)
    turing_machine.print_header()

    # for each line in the input file it will run the turing machine
    for line in infile:
        # Remove whitespace from line
        line = line.strip()

        # Convert line to a list
        tape = list(line)

        # Run the turing machine on the tape
        turing_machine.run(tape)
        turing_machine.print_footer()

    infile.close()
예제 #2
0
def main():
    options = parse_args()

    if options.explain:
        with open(options.explain) as f:
            program = json.load(f)
        print("==== \033[1mExpected tape input format:\033[0m ==== ")
        print(program.get('tape-format', '<not provided>'))
        print()
        print("==== \033[1mExpected tape output format:\033[0m ====")
        print(program.get('output-format', '<not provided>'))
        return

    program = Program.from_file(options.program)
    tape = [i for i in options.tape]

    if options.accepts:
        print(
            TuringMachine.accepts(program,
                                  tape,
                                  error_on_eot=not options.infinite,
                                  verbose=not options.quiet))
        return

    machine = TuringMachine(program,
                            tape,
                            err_on_eot=not options.infinite,
                            verbose=not options.quiet)
    if options.ensuretransitions:
        machine.ensure_transitions()
    print("Machine start!")
    print("Tape output (without blanks)")
    machine.print_tape_trimmed()
    if not options.quiet:
        print("\nBeginning simulation...")
    machine.run()
    print('\nFinished!')
    print("Tape output (without blanks)")
    machine.print_tape_trimmed()