Example #1
0
    def test_string_loading(self):
        from tbas.corpus import load_string

        target_str = "Spammish Repetition"
        m = Machine(program=load_string(target_str))
        m.run()
        self.assertEqual(target_str, str(m))
Example #2
0
 def test_loop(self):
     m = Machine(program='+++++')
     self.assertEqual(5, m.run())
     self.assertEqual(5, m.mem_at(0))
     m.reset_program()
     m.load_program('[-]')
     self.assertEqual(11, m.run())
     self.assertEqual(0, m.mcell)
Example #3
0
 def test_digits(self):
     m = Machine()
     from string import digits
     program = '+'*14 + '=' + '-'*14 + '>'.join('+'*i for i in range(len(digits)))
     program += '<'*(program.count('>'))
     program += '>'.join('?' for _ in range(len(digits)))
     m.load_program(program)
     m.run()
     for index, val in enumerate(digits):
         self.assertEqual(ord(val), m.mem_at(index))
Example #4
0
 def test_tbas(self):
     m = Machine()
     from tbas.badge_io import tbas_chars
     program = '+'*15 + '=' + '-'*15 + '>'.join('+'*i for i in range(len(tbas_chars)))
     program += '<'*(program.count('>'))
     program += '>'.join('?' for _ in range(len(tbas_chars)))
     m.load_program(program)
     m.run()
     for index, val in enumerate(tbas_chars):
         self.assertEqual(ord(val), m.mem_at(index))
Example #5
0
 def test_ascii_uppercase(self):
     m = Machine()
     from string import ascii_uppercase
     program = '+'*13 + '=' + '-'*13 + '>'.join('+'*i for i in range(len(ascii_uppercase)))
     program += '<'*(program.count('>'))
     program += '>'.join('?' for _ in range(len(ascii_uppercase)))
     m.load_program(program)
     m.run()
     for index, val in enumerate(ascii_uppercase):
         self.assertEqual(ord(val), m.mem_at(index))
Example #6
0
def interpret_program(program_string: str, machine: Machine = None, t=65536):
    if machine is None:
        machine = Machine()
    timeout = t
    machine.load_program(program_string)

    should_continue = True

    while should_continue and timeout > 0:
        try:
            should_continue = machine.step_once()
        except Exception as e:
            machine.debug_printout()
            raise e
        timeout -= 1
    print()
    if timeout == 0:
        machine.debug_printout()
        assert False, "Program used too many cycles"
Example #7
0
    def __init__(self,
                 machine: Machine = Machine(program="+++[?-]"),
                 running=False):
        self.machine = machine
        self.running = running

        self.root = Tk()
        self.toolbar = ToolBar(self.root, self.run, self.continue_exec,
                               self.step, self.reset)
        self.program = Program(self.root, machine.program)
        self.buffer = Mem(self.root,
                          title="Buffer",
                          size=machine.buffer.max_length)
        self.memory = Mem(self.root, title="Memory", size=machine.num_cells)
        self.status = StatusBar(self.root)

        self.buffer.update_callback = self.update_buf
        self.memory.update_callback = self.update_mem

        self.update()
Example #8
0
 def test_quine(self):
     m = Machine(program='++++++=?+=>?')
     m.run()
     m = Machine(program='++++++=?++++=>++>+[?<=>?<<=>>]<<----=?+=>>>?')
     m.run()
     self.assertTrue(True)
Example #9
0
 def test_buffer_fifo(self):
     m = Machine(program='+'*8 + '=?-?-?-?-?-?-?-?' + '+'*9 + '=->?>?>?>?>?>?>?>?')
     m.run()
     for i in range(9, 0, -1):
         self.assertEqual(i, m.mem_at(9 - i))
Example #10
0
 def test_buffer_filo(self):
     m = Machine(program='+'*8 + '=?-?-?-?-?-?-?-?' + '+'*8 + '=>?>?>?>?>?>?>?>?')
     m.run()
     self.assertEqual(9, m.mem_at(0))
     for i in range(1, 9):
         self.assertEqual(i, m.mem_at(i))
Example #11
0
 def test_buffer_program(self):
     m = Machine()
     program_string = '++++++=?'
     m.load_program(program_string)
     m.run()
     self.assertEqual(program_string, str(m.buffer))
Example #12
0
 def test_nested_loop(self):
     m = Machine(program='+++++[>+++[>+<-]<-]')
     m.run()
     self.assertEqual(15, m.mem_at(2))
Example #13
0
    def test_multiply(self):
        from tbas.corpus import multiply_numbers

        m = Machine(program=multiply_numbers(3, 5))
        m.run()
        self.assertEqual(15, m.mcell)

        m.clean_init()
        m.load_program(multiply_numbers(5, 8, 2))
        m.run()
        self.assertEqual(42, m.mcell)

        m.clean_init()
        m.load_program(multiply_numbers(10, 7, -1))
        m.run()
        self.assertEqual(69, m.mcell)  # nice
Example #14
0
 def test_not(self):
     m = Machine(program='++++++++=?++++++++++++++=?')
     m.run()
     self.assertEqual(0, m.mcell)
Example #15
0
 def test_mul(self):
     m = Machine(program='++++++++=?++++++++++=?')
     m.run()
     self.assertEqual(18*8, m.mcell)
Example #16
0
 def test_div(self):
     m = Machine(program='++++++++=?+++++++++++=+++++?')
     m.run()
     self.assertEqual(24//8, m.mcell)
Example #17
0
 def test_eptr(self):
     m = Machine(program='+'*25 + '=>>>?')
     m.run()
     self.assertEqual(m.ip, m.mcell)
Example #18
0
 def test_mptr(self):
     m = Machine(program='+'*24 + '=>>>?')
     m.run()
     self.assertEqual(m.data_pointer, m.mcell)
Example #19
0
 def test_xor(self):
     m = Machine(program='++++++++=?+++++++++++++++=?')
     m.run()
     self.assertEqual(23 ^ 8, m.mcell)
Example #20
0
 def test_mptr_inc_dec(self):
     m = Machine(program='+++>+++>+++>++++')
     m.run()
     self.assertEqual(3, m.mem_at(0))
     self.assertEqual(3, m.mem_at(1))
     self.assertEqual(3, m.mem_at(2))
     self.assertEqual(4, m.mcell)
     m.reset_program()
     m.load_program('>>>-<--<---<----')
     m.run()
     self.assertEqual(0, m.mcell)
     self.assertEqual(0, m.mem_at(1))
     self.assertEqual(1, m.mem_at(2))
     self.assertEqual(3, m.mem_at(3))
Example #21
0
 def test_sub(self):
     m = Machine(program='++++++++=?+++++++++=?')
     m.run()
     self.assertEqual(17-8, m.mcell)
Example #22
0
 def test_reljump_left(self):
     m = Machine(program='>+<' + '+'*26 + '=' + '-'*26 + '+'*10 + '>[-<?]<')
     m.run()
     self.assertEqual(15, m.mcell)
Example #23
0
 def test_and(self):
     m = Machine(program='++++++++=?++++++++++++=?')
     m.run()
     self.assertEqual(20 & 8, m.mcell)
Example #24
0
 def test_reljump_right(self):
     m = Machine(program='+'*27 + '=' + '-'*20 + '?' + '+'*10)
     m.run()
     self.assertEqual(10, m.mcell)
Example #25
0
def main():
    UI(machine=Machine(program=input("> ")))._start()