title: Smallf*ck (simple brainfuck dialect) interpreter endless loop
body: <p>I'm trying to implement a Smallf*ck interpreter. </p>
  
  <p>Smallfuck is an even more laconic dialect of Brainfuck, which operates on bits instead of bytes, has a limited size of memory tape and has no I/O commands. Thus, it is left with only 5 commands:</p>
  
  <pre><code>* : flip the current bit;
  &gt; : increment the data pointer (move it to the next cell to the right);
  &lt; : decrement the data pointer (move it to the next cell to the left);
  [ : “begin loop”:
      if the current bit is 1, increment the instruction pointer
      (move it to the next command to the right), otherwise,
      move it to the next command to the right of the matching ] command;
  ] : “end loop”:
      if the current bit is 0, increment the instruction pointer,
      otherwise move it to the next command to the right of the matching [ command.
      Can also be interpreted as unconditional jump to the matching [ command,
      since [ performs an extra check itself.
  </code></pre>
  
  <p>Example input: <code>"*&gt;*&gt;*&gt;*&gt;*&gt;*&gt;*&gt;*", "00101100"</code> should return <code>"11010011"</code></p>
  
  <p>My implementation so far:</p>
  
  <pre class="lang-py prettyprint-override"><code>def interpreter(code, tape):
      ptr, cmd_pos = 0, 0
      tape = [int(num) for num in tape]
      while ptr &lt; len(tape) and cmd_pos &lt; len(code):
          if code[cmd_pos] == "&gt;":
              ptr += 1
          elif code[cmd_pos] == "&lt;":
              ptr -= 1
          elif code[cmd_pos] == "*":
              tape[ptr] = 1 if tape[ptr] == 0 else 0
          elif code[cmd_pos] == "[":
              if tape[ptr] == 0:
                  found = False
                  while not found:
                      cmd_pos += 1
                      if code[cmd_pos] == "]":
                          found = True
          elif code[cmd_pos] == "]":
              found = False
              while not found:
                  cmd_pos -= 1
                  if code[cmd_pos] == "[":
                      found = True
          cmd_pos += 1
      return ''.join([str(num) for num in tape])
  </code></pre>
  
  <p>There is also a codewars question, which is the reason I'm doing this: <a href="https://www.codewars.com/kata/58678d29dbca9a68d80000d7/train/python" rel="nofollow noreferrer">https://www.codewars.com/kata/58678d29dbca9a68d80000d7/train/python</a></p>
  
  <p>I don't know what's wrong with my code... The basic stuff works, but loops don't.
  At some test cases on codewars I create an endless loop and I don't know why.</p>
  
  <p>Help would be really appreciated, maybe someone has even a lot of fun implementing this:</p>