-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0.py
executable file
·76 lines (54 loc) · 1.54 KB
/
0.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
#!/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
def run(chars):
"""Actual BrainFuck Interpreter."""
jump_table = create_jump_table(chars)
cells = [0] * 10000
index = 0
position = 0
while position < len(chars):
char = chars[position]
if char == '>':
index += 1
elif char == '<':
if index > 0:
index -= 1
elif char == '+':
cells[index] += 1
elif char == '-':
if cells[index] > 0:
cells[index] -= 1
elif char == '.':
sys.stdout.write(chr(cells[index] % 256))
sys.stdout.flush()
elif char == ',':
cells[index] = ord(raw_input())
elif char == '[' and cells[index] == 0:
position = jump_table[position]
elif char == ']' and cells[index] != 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()))