Starts a simple REPL for this machine.

    Args:
        optimize: Controls whether to run inputted code through the
        optimizer.

        persist: If True, the machine is not deleted after each line.

def repl(optimize=True, persist=True):
    """Starts a simple REPL for this machine.

    Args:
        optimize: Controls whether to run inputted code through the
        optimizer.

        persist: If True, the machine is not deleted after each line.
    """
    print("Extra commands for the REPL:")
    print(".code    - print code")
    print(".raw     - print raw code")
    print(".quit    - exit immediately")
    print(".reset   - reset machine (IP and stacks)")
    print(".restart - create a clean, new machine")
    print(".clear   - same as .restart")
    print(".stack   - print data stack")
    print("")

    machine = Machine([])

    def match(s, *args):
        return any(map(lambda arg: s.strip()==arg, args))

    while True:
        try:
            source = raw_input("> ").strip()

            if source[0] == "." and len(source) > 1:
                if match(source, ".quit"):
                    return
                elif match(source, ".code"):
                    print_code(machine)
                elif match(source, ".raw"):
                    print(machine.code)
                elif match(source, ".reset"):
                    machine.reset()
                elif match(source, ".restart", ".clear"):
                    machine = Machine([])
                elif match(source, ".stack"):
                    print(machine.stack)
                else:
                    raise ParseError("Unknown command: %s" % source)
                continue

            code = compile(parse(source), silent=False, optimize=optimize)

            if not persist:
                machine.reset()

            machine.code += code
            machine.run()
        except EOFError:
            return
        except KeyboardInterrupt:
            return
        except ParseError as e:
            print("Parse error: %s" % e)
        except MachineError as e:
            print("Machine error: %s" % e)
        except CompileError as e:
            print("Compile error: %s" % e)