Пример #1
0
def interpret(source):
	brackets = getBrackets(source)
	inv_brackets = {v:k for k, v in brackets.items()}
	
	t = Tape()
	
	ind = 0	
	while ind < len(source):
		if source[ind] == '>':
			t.right()
			ind += 1			
		elif source[ind] == '<':
			t.left()
			ind += 1
		elif source[ind] == '+':
			t.inc()
			ind += 1
		elif source[ind] == '-':
			t.dec()
			ind += 1
		elif source[ind] == '.':
			sys.stdout.write(t.getChar())
			ind += 1
		elif source[ind] == ',':
			t.putChar()
			ind += 1
		elif source[ind] == '[':
			if t.getNum() == 0:
				ind = brackets[ind]
			ind += 1
		elif source[ind] == ']':
			ind = inv_brackets[ind]
Пример #2
0
def mainloop(program,bracket_map):
    tape = Tape()
    pc   = 0
    while pc < len(program):
        code = program[pc]

        if code == ">":
            tape.advance()

        elif code == "<":
            tape.devance()

        elif code == "+":
            tape.inc()

        elif code == "-":
            tape.dec()

        elif code == ".":
            sys.stdout.write(chr(tape.get()))

        elif code == ",":
            tape.set(ord(sys.stdin.read(1)))

        elif code == "[" and tape.get() == 0:
            pc = bracket_map[pc]

        elif code == "]" and tape.get() != 0:
            pc = bracket_map[pc]

        pc += 1
def mainloop(program, bracket_map):
    pc = 0
    tape = Tape()

    while pc < len(program):
        jitdriver.jit_merge_point(pc=pc, tape=tape, program=program,bracket_map=bracket_map)

        code = program[pc]

        if code == ">":
            tape.advance()

        elif code == "<":
            tape.devance()

        elif code == "+":
            tape.inc()

        elif code == "-":
            tape.dec()

        elif code == ".":
            # print
            os.write(1, chr(tape.get()))

        elif code == ",":
            # read from stdin
            tape.set(ord(os.read(0, 1)[0]))

        elif code == "[" and tape.get() == 0:
            # Skip forward to the matching ]
            pc = bracket_map[pc]

        elif code == "]" and tape.get() != 0:
            # Skip back to the matching [
            pc = bracket_map[pc]

        pc += 1