| dd1058f by root at 2008-06-19 |
1 |
__author__='Karthik K'
|
| 5a0a023 by karthik at 2008-06-10 |
2 |
|
|
3 |
'''
|
|
4 |
I am not sure about the global current_ptr. It may crash. I made it global for the use in print_memory_map
|
|
5 |
If you hit any issues mail to karthik3186@gmail.com
|
|
6 |
'''
|
|
7 |
import sys
|
|
8 |
|
|
9 |
SIZE=10000 #Size of the memory that will be allocated for BrainF**K
|
|
10 |
LOWER_BOUND=0
|
|
11 |
UPPER_BOUND=SIZE-1
|
|
12 |
memory=[0]*SIZE #Actual memory been given to BrainF**K
|
|
13 |
current_ptr=0
|
|
14 |
|
|
15 |
def extract_between_brackets(source, start):
|
|
16 |
'''
|
|
17 |
Given a source and a starting square bracket, it gives the string between this square bracket
|
|
18 |
and its corresponding close bracket
|
|
19 |
'''
|
|
20 |
openB=1
|
|
21 |
ret=''
|
|
22 |
cur=start+1
|
|
23 |
while (openB!=0):
|
|
24 |
if cur >= len(source):
|
|
25 |
break
|
|
26 |
if source[cur]=='[':
|
|
27 |
openB+=1
|
|
28 |
elif source[cur]==']':
|
|
29 |
openB-=1
|
|
30 |
else:
|
|
31 |
pass
|
|
32 |
ret+=source[cur]
|
|
33 |
cur+=1
|
|
34 |
if openB!=0:
|
|
35 |
return 'ERROR'
|
|
36 |
return ret[0:len(ret)-1], cur
|
|
37 |
|
|
38 |
def check_and_exit_if_needed():
|
|
39 |
if current_ptr < LOWER_BOUND or current_ptr > UPPER_BOUND:
|
|
40 |
print "SEGMENTATION FAULT"
|
|
41 |
sys.exit(1)
|
|
42 |
|
|
43 |
def verify_program(source):
|
|
44 |
'''
|
|
45 |
Given a BrainF**K source, this function verifies whether it is syntactically correct
|
|
46 |
'''
|
|
47 |
if extract_between_brackets('['+source+']', 0) != 'ERROR':
|
|
48 |
return True
|
|
49 |
return False
|
|
50 |
|
|
51 |
def execute(source):
|
|
52 |
'''
|
|
53 |
Given a BrainF**K source, this function executes it.
|
|
54 |
'''
|
|
55 |
global current_ptr
|
|
56 |
check_and_exit_if_needed()
|
|
57 |
if (not verify_program(source)):
|
|
58 |
print "SYNTAX ERROR"
|
|
59 |
return
|
|
60 |
i=0
|
|
61 |
while i<len(source):
|
|
62 |
current_character=source[i]
|
|
63 |
if current_character == '>':
|
|
64 |
current_ptr += 1
|
|
65 |
check_and_exit_if_needed()
|
|
66 |
elif current_character == '<':
|
|
67 |
current_ptr -= 1
|
|
68 |
check_and_exit_if_needed()
|
|
69 |
elif current_character == '+':
|
|
70 |
memory[current_ptr]+=1
|
|
71 |
elif current_character == '-':
|
|
72 |
memory[current_ptr]-=1
|
|
73 |
elif current_character == '.':
|
|
74 |
print chr(memory[current_ptr])
|
|
75 |
elif current_character == ',':
|
|
76 |
pass
|
|
77 |
elif current_character == '[':
|
|
78 |
# Now this is where things get interesting. You have to search for the matching right '['
|
|
79 |
# and execute the source within these brackets recursively :-)
|
|
80 |
source_within_brackets, end = extract_between_brackets(source, i)
|
|
81 |
if source_within_brackets == 'ERROR':
|
|
82 |
print 'Syntax Error'
|
|
83 |
sys.exit(1)
|
|
84 |
while memory[current_ptr]:
|
|
85 |
execute(source_within_brackets)
|
|
86 |
i=end-1
|
|
87 |
i+=1
|
|
88 |
def print_memory_map():
|
|
89 |
global current_ptr
|
|
90 |
for i in range(0, current_ptr):
|
|
91 |
print '[',memory[i],']',
|
|
92 |
print '[',memory[current_ptr],']'
|
|
93 |
if __name__ == '__main__':
|
|
94 |
#print 'Hello World'
|
|
95 |
execute('>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.')
|
|
96 |
#print_memory_map()
|
| dd1058f by root at 2008-06-19 |
97 |
|