Add support (and examples) for Jolverine Super Wimp Mode.
catseye
11 years ago
6 | 6 | import sys |
7 | 7 | |
8 | 8 | class Program(object): |
9 | def __init__(self, program, debug=False): | |
9 | def __init__(self, program, debug=False, super_wimp_mode=False): | |
10 | 10 | self.wheel = [ |
11 | 11 | 'left', |
12 | 12 | 'right', |
22 | 22 | self.playfield = {} |
23 | 23 | self.insertpos = 'bottom' |
24 | 24 | self._debug = debug |
25 | self._super_wimp_mode = super_wimp_mode | |
25 | 26 | y = 0 |
26 | 27 | self.maxx = 0 |
27 | 28 | for line in file: |
112 | 113 | self.insertpos = 'bottom' |
113 | 114 | |
114 | 115 | def run(self): |
115 | while True: | |
116 | instr = self.playfield.get((self.x, self.y), ' ') | |
117 | if instr == '*': | |
118 | self.execute() | |
119 | self.cycle() | |
120 | self.x += self.dx | |
121 | self.y += self.dy | |
122 | self.dump() | |
123 | if (self.x < 0 or self.y < 0 or | |
124 | self.x > self.maxx or self.y > self.maxy): | |
125 | break | |
116 | if not self._super_wimp_mode: | |
117 | while True: | |
118 | instr = self.playfield.get((self.x, self.y), ' ') | |
119 | if instr == '*': | |
120 | self.execute() | |
121 | self.cycle() | |
122 | self.x += self.dx | |
123 | self.y += self.dy | |
124 | self.dump() | |
125 | if (self.x < 0 or self.y < 0 or | |
126 | self.x > self.maxx or self.y > self.maxy): | |
127 | break | |
128 | else: | |
129 | table = { | |
130 | '<': 'left', | |
131 | '>': 'right', | |
132 | '+': 'rot', | |
133 | 'x': 'adddx', | |
134 | 'y': 'adddy', | |
135 | 'i': 'input', | |
136 | 'o': 'output', | |
137 | } | |
138 | while True: | |
139 | instr = self.playfield.get((self.x, self.y), ' ') | |
140 | name = table.get(instr, None) | |
141 | if name is not None: | |
142 | self.debug("* WIMPZECUTING %s @ (%d,%d)" % (name, self.x, self.y)) | |
143 | command = getattr(self, name) | |
144 | command() | |
145 | self.x += self.dx | |
146 | self.y += self.dy | |
147 | self.dump() | |
148 | if (self.x < 0 or self.y < 0 or | |
149 | self.x > self.maxx or self.y > self.maxy): | |
150 | break | |
126 | 151 | |
127 | 152 | ### debugging ### |
128 | 153 | |
132 | 157 | print "------------" |
133 | 158 | print "x=%d,y=%d,dx=%d,dy=%d" % (self.x, self.y, self.dx, self.dy) |
134 | 159 | print "head: %d cell: %d" % (self.head, self.tape.get(self.head, 0)) |
135 | print "Next reinsert: %s" % self.insertpos | |
136 | print "Wheel:" | |
137 | print "------------" | |
138 | i = 0 | |
139 | for x in self.wheel: | |
140 | arrow = " " | |
141 | if i == self.index: | |
142 | arrow = "-->" | |
143 | print "%s %d. %s" % (arrow, i, x) | |
144 | i += 1 | |
160 | if not self._super_wimp_mode: | |
161 | print "Next reinsert: %s" % self.insertpos | |
162 | print "Wheel:" | |
163 | print "------------" | |
164 | i = 0 | |
165 | for x in self.wheel: | |
166 | arrow = " " | |
167 | if i == self.index: | |
168 | arrow = "-->" | |
169 | print "%s %d. %s" % (arrow, i, x) | |
170 | i += 1 | |
145 | 171 | print "------------" |
146 | 172 | |
147 | 173 | |
151 | 177 | |
152 | 178 | if __name__ == '__main__': |
153 | 179 | debug = False |
154 | fnai = 1 | |
155 | if sys.argv[1] == '-d': | |
180 | super_wimp_mode = False | |
181 | if sys.argv[1] in ('-d', '--debug'): | |
156 | 182 | debug = True |
157 | fnai = 2 | |
158 | with open(sys.argv[fnai]) as file: | |
159 | p = Program(file, debug=debug) | |
183 | sys.argv.pop(1) | |
184 | if sys.argv[1] == '--super-wimp-mode': | |
185 | super_wimp_mode = True | |
186 | sys.argv.pop(1) | |
187 | with open(sys.argv[1]) as file: | |
188 | p = Program(file, debug=debug, super_wimp_mode=super_wimp_mode) | |
160 | 189 | p.dump() |
161 | 190 | p.run() |