# Copyright (c) 2009 Tim Freeman # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # (This is the standard MIT License, copied from # http://www.opensource.org/licenses/mit-license.php on 24 Apr 2007.) #desc Convenience functions to compile and run a Turing machine. from turing_tape import turing_tape, binarytape from animate import do_animation import compile import machine def animator(state_name_array): def show_name(stateno): return state_name_array[stateno] def animate(tape_length, tape_bits, headpos, sn, state, bpsn, short=False): do_animation(tape_length, tape_bits, headpos, sn, state, bpsn, state_to_name = show_name, short = short) return animate def compile_and_run(absyn, inputsize=None, inputtape=None, steps=None, inputstring=None, wholetape=None, animate=False): assert steps is not None if inputstring is not None: assert wholetape is None wholetape = binarytape(inputstring) if inputsize is not None: assert inputtape is not None assert wholetape is None wholetape = turing_tape(length=inputsize, bits=inputtape) compiled = compile.turing_compile(absyn) if animate: animate = animator(compiled.statemap) else: animate = machine.silent_do_animation return machine.turing(compiled.program, wholetape, steps, animate=animate)