-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.py
executable file
·101 lines (71 loc) · 2.04 KB
/
1.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
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):
char = chars[position]
if char == '>':
ptr.right()
elif char == '<':
ptr.left()
elif char == '+':
ptr.increment()
elif char == '-':
ptr.decrement()
elif char == '.':
sys.stdout.write(chr(ptr.get() % 256))
sys.stdout.flush()
elif char == ',':
ptr.set(ord(raw_input()))
elif char == '[' and ptr.get() == 0:
position = jump_table[position]
elif char == ']' and ptr.get() != 0:
position = jump_table[position]
position += 1
def remove_comments(chars):
codes = '<>[]-+,.'
tmp = ""
for c in chars:
if c in codes:
tmp += c
return tmp
if __name__ == "__main__":
run(remove_comments(open(sys.argv[1]).read()))