示例#1
0
    def test_fac(self):
        fac = compile_program('''
            [
                [(1,2), [(0,1,R), (A,A,S), (B,0,R)]],
                [(2,2), [(0,0,R), (A,A,S), (B,0,R)]],
                [(2,3), [(B,B,L), (A,A,S), (B,B,L)]],
                [(3,3), [(0,0,S), (B,0,R), (0,0,L)]],
                [(3,4), [(0,0,L), (B,B,S), (B,B,R)]],
                [(3,6), [(1,1,R), (B,B,L), (B,B,R)]],
                [(4,4), [(0,0,S), (B,0,R), (0,0,R)]],
                [(4,3), [(0,0,L), (B,B,S), (B,B,L)]],
                [(4,5), [(1,1,R), (B,B,L), (B,B,L)]],
                [(5,5), [(0,0,R), (0,B,L), (A,0,L)]],
                [(5,5), [(B,B,S), (0,B,L), (A,0,L)]],
                [(5,4), [(B,B,L), (A,A,S), (B,B,R)]],
                [(5,4), [(B,B,L), (A,A,S), (B,B,R)]],
                [(6,6), [(B,B,S), (0,B,L), (A,0,R)]],
                [(6,3), [(B,B,L), (B,B,S), (B,B,R)]],
            ]
        ''')

        m = ma.Machine(tapes=('0000', None, None),
                       functions=fac)

        for i in m:
            print(i)
示例#2
0
 def test_compile_program(self):
     assert compile_program('''
         [
             [(1,1), [(1,B,R)]],
             [(1,2), [(B,B,R)]]
         ]
     ''') == [[(1, 1), [(1, None, 'r')]], [(1, 2), [(None, None, 'r')]]]
示例#3
0
    def test_run_compiled_program(self):
        prog = compile_program('''
            [
                [(1,1), [(0,B,R)]],
                [(1,2), [(B,B,R)]]
            ]
        ''')
        m = ma.Machine(tapes=('00', ),
                       functions=prog)

        i = iter(m)
        assert str(next(i)[1][0]) == '00'
        assert str(next(i)[1][0]) == '0'
        assert str(next(i)[1][0]) == ''
        assert str(next(i)[1][0]) == ''

        self.assertRaises(StopIteration, lambda: next(i))
示例#4
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('program')
    parser.add_argument('-i', '--input', dest='inputs', action='append')
    parser.add_argument('-o', '--output', dest='format')
    parser.add_argument('-s', '--step', action='store_true')
    parser.add_argument('-l', '--lag', type=float, dest='lag')
    parser.add_argument('-c', '--count', dest='count')

    args = parser.parse_args()

    file_path = os.path.abspath(os.path.expanduser(args.program))
    if not os.path.exists(file_path):
        # try current dir
        parser.exit(message='No such file %s\n' % file_path)

    with open(file_path, 'r', encoding='utf-8') as f:
        program = compile_program(f.read())

    kw = {'tapes': args.inputs}
    if isinstance(program, dict):
        kw.update(program)
    else:
        kw.update({'functions': program})

    ma = Machine(**kw)

    run_fac = RUNNER_FACTORIES.get(args.format)
    if not run_fac:
        run_fac = create_terminal_runner

    runargs = {
        'step_by_step': args.step,
        'lag': args.lag
    }

    if args.count:
        runargs['tape_eval'] = create_counter(args.count)

    run_fac(ma,
            **runargs)()
示例#5
0
    def test_multiply(self):
        prog = compile_program('''
            [
                [(1,2), [(0,1,R), (B,B,S)]],
                [(2,2), [(0,0,R), (B,B,S)]],
                [(2,3), [('x','x',R), (B,B,S)]],
                [(3,3), [(0,0,R), (B,0,R)]],
                [(3,4), [(B,B,L), (B,B,S)]],
                [(4,4), [(0,0,L), (B,B,S)]],
                [(4,5), [('x','x',L), (B,B,S)]],
                [(5,5), [(0,0,L), (B,B,S)]],
                [(5,1), [(1,1,R), (B,B,S)]],
                [(1,6), [('x','x',R), (B,B,S)]],
            ]
        ''')

        m = ma.Machine(tapes=('00x00', None),
                       functions=prog)
        i = iter(m)

        def n():
            _, tapes = next(i)
            t1, t2 = tapes
            return str(t1), str(t2)

        t1, t2 = n()
        assert str(t1) == '00x00'
        assert str(t2) == ''
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == ''
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == ''
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == ''
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == '0'
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '10x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '11x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '11x00'
        assert str(t2) == '00'
        t1, t2 = n()
        assert str(t1) == '11x00'
        assert str(t2) == '000'
        t1, t2 = n()
        assert str(t1) == '11x00'
        assert str(t2) == '0000'