-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfinterpreter.py
75 lines (66 loc) · 2.04 KB
/
bfinterpreter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import bytecodes
import os
class Interpreter(object):
def __init__(self, bytecode):
#super(Interpreter, self).__init__()
self.bytecode = bytecode
def decode(self, bytecode):
return ord(bytecode)
def read4(self, code, pc):
highval = ord(code[pc+3])
if highval >= 128:
highval -= 256
return (ord(code[pc]) |
(ord(code[pc+1]) << 8) |
(ord(code[pc+2]) << 16) |
(highval << 24))
def run(self):
pc = 0
memory = [0 for x in xrange(300)]
dp = 0
while(pc < len(self.bytecode)):
inst = self.decode(self.bytecode[pc])
pc += 1
if inst == bytecodes.BF_INCR_DP:
dp += 1
elif inst == bytecodes.BF_DCR_DP:
dp -= 1
elif inst == bytecodes.BF_INCR_D:
memory[dp] += 1
elif inst == bytecodes.BF_DCR_D:
memory[dp] -= 1
elif inst == bytecodes.BF_ECHO:
if memory[dp] < 256:
print chr(memory[dp])
else:
print memory[dp]
elif inst == bytecodes.BF_READ:
inp = read()
memory[dp] = ord(inp)
elif inst == bytecodes.BF_JUMP_IF_ZERO:
if memory[dp] == 0:
arg = self.read4(self.bytecode, 4)
pc = arg
else:
# skip argument
pc += 4
elif inst == bytecodes.BF_JUMP_UNLESS_ZERO:
if memory[dp] != 0:
arg = self.read4(self.bytecode, pc)
pc = arg
else:
pc += 4
#print memory
def read():
result = None
while True:
s = os.read(0, 1)
if result is None:
result = s
if s == "\n":
break
if s == '':
if len(result) > 1:
break
raise SystemExit
return result[0]