Add the 0-255 restrict, and fix the jump-back commands (I hope.)
Chris Pressey
8 years ago
26 | 26 |
Don't expect this interpreter to work on the example programs supplied with
|
27 | 27 |
the specification.
|
28 | 28 |
|
29 | |
There are two bugs/shortcomings that should really be fixed someday:
|
30 | |
|
31 | |
* Integers on the stack can be any value supported by Python, rather
|
32 | |
than limited to the range 0-255.
|
33 | |
* The instructions that skip back, probably skip back the wrong amount.
|
34 | |
|
35 | |
Other than those two things, this interpreter seems to have the same behaviour
|
36 | |
as Catatonic Porpoise's interpreter. Could use some test cases, though.
|
|
29 |
This interpreter seems to have the same behaviour as
|
|
30 |
[Catatonic Porpoise's Beatnik interpreter][]. This repository could stand
|
|
31 |
to contain some test cases, though.
|
37 | 32 |
|
38 | 33 |
`beatnik.py` also takes a `--debug` flag, which dumps some internal state
|
39 | 34 |
to standard error as the program is run.
|
|
112 | 107 |
|
113 | 108 |
[Beatnik]: http://esolangs.org/wiki/Beatnik
|
114 | 109 |
[wimpmode]: http://esolangs.org/wiki/Wimpmode
|
|
110 |
[Catatonic Porpoise's Beatnik interpreter]: http://esoteric.voxelperfect.net/files/beatnik/impl/BEATNIK.c
|
62 | 62 |
sys.stderr.write("\n")
|
63 | 63 |
|
64 | 64 |
|
|
65 |
class Stack(object):
|
|
66 |
def __init__(self):
|
|
67 |
self._stack = []
|
|
68 |
|
|
69 |
def push(self, value):
|
|
70 |
self._stack.append(value % 256)
|
|
71 |
|
|
72 |
def append(self, value):
|
|
73 |
"""for backwards compatibility"""
|
|
74 |
self.push(value)
|
|
75 |
|
|
76 |
def pop(self):
|
|
77 |
return self._stack.pop()
|
|
78 |
|
|
79 |
|
65 | 80 |
def main(args):
|
66 | 81 |
global DEBUG
|
67 | 82 |
tokenize = False
|
|
98 | 113 |
|
99 | 114 |
words.append('FOXY') # stop if you get to the end
|
100 | 115 |
|
101 | |
stack = []
|
|
116 |
stack = Stack()
|
102 | 117 |
pc = 0
|
103 | 118 |
done = False
|
104 | 119 |
|
|
150 | 165 |
pc += dist
|
151 | 166 |
elif value == 15:
|
152 | 167 |
a = stack.pop()
|
153 | |
pc += 1
|
154 | |
dist = scrabble(words[pc])
|
|
168 |
dist = scrabble(words[pc + 1])
|
155 | 169 |
if a == 0:
|
156 | 170 |
pc -= dist
|
157 | 171 |
elif value == 16:
|
158 | 172 |
a = stack.pop()
|
159 | |
pc += 1
|
160 | |
dist = scrabble(words[pc])
|
|
173 |
dist = scrabble(words[pc + 1])
|
161 | 174 |
if a != 0:
|
162 | 175 |
pc -= dist
|
163 | 176 |
elif value == 17:
|