-
Notifications
You must be signed in to change notification settings - Fork 4
/
pook.py
145 lines (124 loc) · 3.7 KB
/
pook.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
#!/usr/bin/env python
#
# an Ook! interpreter written in python
#
# you can wrap the memory pointer to the end of the the memory cells
# but you cannot do the same trick to get the first cell, since going
# further out would just initiate a new memory cell.
#
# 2001 (C) Øyvind Grønnesby <[email protected]>
#
# 2003-02-06: Thanks to John Farrell for spotting a bug!
import sys, string, types
def massage(text):
ret = []
tok = []
for line in text:
if line[0] != ";" and line != "\n" and line != "":
for token in line.split(" "):
if token != "":
ret.append(token.strip())
return ret
def sane(code):
if len(code) % 2 == 0:
return 1
else:
return 0
class OokInterpreter:
memory = [0]
memptr = 0
file = None
code = None
len = 0
codei = 0
def __langinit(self):
self.lang = {'Ook. Ook?' : self.mvptrup,
'Ook? Ook.' : self.mvptrdn,
'Ook. Ook.' : self.incptr,
'Ook! Ook!' : self.decptr,
'Ook. Ook!' : self.readc,
'Ook! Ook.' : self.prntc,
'Ook! Ook?' : self.startp,
'Ook? Ook!' : self.endp}
def mem(self):
return self.memory[self.memptr]
def __init__(self, file):
self.__langinit()
self.file = open(file)
self.code = massage(self.file.readlines())
self.file.close()
if not sane(self.code):
print self.code
raise "OokSyntaxError", "Code not sane."
else:
self.cmds()
def run(self):
self.codei = 0
self.len = len(self.code)
while self.codei < self.len:
self.lang[self.code[self.codei]]()
self.codei += 1
def cmds(self):
i = 0
l = len(self.code)
new = []
while i < l:
new.append(string.join((self.code[i], self.code[i+1]), " "))
i += 2
self.code = new
def startp(self):
ook = 0
i = self.codei
if self.memory[self.memptr] != 0:
return None
while 1:
i += 1
if self.code[i] == 'Ook! Ook?':
ook += 1
if self.code[i] == 'Ook? Ook!':
if ook == 0:
self.codei = i
break
else:
ook -= 1
if i >= self.len:
raise 'OokSyntaxError', 'Unmatched "Ook! Ook?".'
def endp(self):
ook = 0
i = self.codei
if self.memory[self.memptr] == 0:
return None
if i == 0:
raise 'OokSyntaxError', 'Unmatched "Ook? Ook!".'
while 1:
i -= 1
if self.code[i] == 'Ook? Ook!':
ook += 1
if self.code[i] == 'Ook! Ook?':
if ook == 0:
self.codei = i
break
else:
ook -= 1
if i <= 0:
raise 'OokSyntaxError', 'Unmatched "Ook? Ook!".'
def incptr(self):
self.memory[self.memptr] += 1
def decptr(self):
self.memory[self.memptr] -= 1
def mvptrup(self):
self.memptr += 1
if len(self.memory) <= self.memptr:
self.memory.append(0)
def mvptrdn(self):
if self.memptr == 0:
self.memptr = len(self.memory) - 1
else:
self.memptr -= 1
def readc(self):
self.memory[self.memptr] = ord(sys.stdin.read(1))
def prntc(self):
sys.stdout.write(chr(self.mem()))
if __name__ == '__main__':
o = OokInterpreter(sys.argv[1])
o.run()