-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbf.py
executable file
·167 lines (117 loc) · 3.35 KB
/
bf.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Python Brainfuck interpreter.
Jitted using http://morepypy.blogspot.fr/2011/04/tutorial-part-2-adding-jit.html
"""
import sys
import os
try:
from rpython.rlib.jit import JitDriver, purefunction
except ImportError:
class JitDriver(object):
def __init__(self, **kw):
pass
def jit_merge_point(self, **kw):
pass
def can_enter_jit(self, **kw):
pass
def purefunction(f):
return f
jitdriver = JitDriver(greens=['position', 'chars', 'jump_table'],
reds=['ptr'])
@purefunction
def get_jump(jump_table, pos):
return jump_table[pos]
def create_jump_table(chars):
jump_table = {}
left_positions = []
position = 0
for char in chars:
if char == '[':
left_positions.append(position)
elif char == ']':
left = left_positions.pop()
right = position
jump_table[left] = right
jump_table[right] = left
position += 1
return jump_table
class Array(object):
def __init__(self):
self._cells = [0] * 10000 # preallocation
self._index = 0
def get(self):
return self._cells[self._index]
def set(self, n):
self._cells[self._index] = n
def increment(self):
self._cells[self._index] += 1
def decrement(self):
if self._cells[self._index] > 0:
self._cells[self._index] -= 1
def right(self):
self._index += 1
if self._index >= len(self._cells):
self._cells.append(0)
def left(self):
if self._index > 0:
self._index -= 1
def run(chars):
"""Actual BrainFuck Interpreter."""
jump_table = create_jump_table(chars)
ptr = Array()
position = 0
while position < len(chars):
jitdriver.jit_merge_point(position=position,
ptr=ptr,
chars=chars,
jump_table=jump_table)
char = chars[position]
if char == '>':
ptr.right()
elif char == '<':
ptr.left()
elif char == '+':
ptr.increment()
elif char == '-':
ptr.decrement()
elif char == '.':
os.write(1, chr(ptr.get() % 256))
elif char == ',':
ptr.set(ord(os.read(0, 1)[0]))
elif char == '[' and ptr.get() == 0:
position = get_jump(jump_table, position)
elif char == ']' and ptr.get() != 0:
position = get_jump(jump_table, position)
position += 1
def remove_comments(chars):
codes = '<>[]-+,.'
tmp = ""
for c in chars:
if c in codes:
tmp += c
return tmp
def entry_point(argv):
try:
filename = argv[1]
except IndexError:
print "Usage: %s program.bf" % argv[0]
return 1
fp = os.open(filename, os.O_RDONLY, 0777)
chars = ""
while True:
read = os.read(fp, 4096)
if len(read) == 0:
break
chars += read
os.close(fp)
run(remove_comments(chars))
return 0
def target(*args):
return entry_point, None
def jitpolicy(driver):
from rpython.jit.codewriter.policy import JitPolicy
return JitPolicy()
if __name__ == "__main__":
entry_point(sys.argv)