comparison pytouhou/game/eclrunner.py @ 48:8353c33d53d4

Support a few more instructions
author Thibaut Girka <thib@sitedethib.com>
date Mon, 22 Aug 2011 15:45:42 +0200
parents 1f1793e7ec8e
children cbe1cb50f2fd
comparison
equal deleted inserted replaced
47:1f1793e7ec8e 48:8353c33d53d4
10 self.instruction_pointer = instruction_pointer 10 self.instruction_pointer = instruction_pointer
11 11
12 self.stack = [] 12 self.stack = []
13 13
14 self.implementation = {4: self.set_variable, 14 self.implementation = {4: self.set_variable,
15 5: self.set_variable,
15 2: self.relative_jump, 16 2: self.relative_jump,
16 3: self.relative_jump_ex, 17 3: self.relative_jump_ex,
18 20: self.add,
19 21: self.substract,
17 35: self.call, 20 35: self.call,
18 36: self.ret, 21 36: self.ret,
19 109: self.memory_write} 22 109: self.memory_write}
20 if implementation: 23 if implementation:
21 self.implementation.update(implementation) 24 self.implementation.update(implementation)
25
26
27 def _get_value(self, value): #TODO: -10013 and beyond!
28 assert not -10025 <= value <= -10013
29 if -10012 <= value <= -10001:
30 return self.variables[int(-10001-value)]
31 else:
32 return value
33
34
35 def add(self, variable_id, a, b):
36 #TODO: proper variable handling
37 #TODO: int vs float thing
38 self.variables[-10001-variable_id] = self._get_value(a) + self._get_value(b)
39
40
41 def substract(self, variable_id, a, b):
42 #TODO: proper variable handling
43 #TODO: int vs float thing
44 self.variables[-10001-variable_id] = self._get_value(a) - self._get_value(b)
45
22 46
23 47
24 def memory_write(self, value, index): 48 def memory_write(self, value, index):
25 #TODO 49 #TODO
26 #XXX: this is a hack to display bosses although we don't handle MSG :) 50 #XXX: this is a hack to display bosses although we don't handle MSG :)
43 67
44 def ret(self): 68 def ret(self):
45 self.sub, self.frame, self.instruction_pointer, self.variables = self.stack.pop() 69 self.sub, self.frame, self.instruction_pointer, self.variables = self.stack.pop()
46 70
47 71
48
49 def set_variable(self, variable_id, value): 72 def set_variable(self, variable_id, value):
50 #TODO: -10013 and beyond! 73 #TODO: -10013 and beyond!
51 self.variables[-10000-variable_id] = value 74 self.variables[-10001-variable_id] = self._get_value(value)
52 75
53 76
54 def relative_jump(self, frame, instruction_pointer): 77 def relative_jump(self, frame, instruction_pointer):
55 self.frame, self.instruction_pointer = frame, instruction_pointer 78 self.frame, self.instruction_pointer = frame, instruction_pointer
56 79
57 80
58 def relative_jump_ex(self, frame, instruction_pointer, variable_id): 81 def relative_jump_ex(self, frame, instruction_pointer, variable_id):
59 if self.variables[-10000-variable_id]: 82 if self.variables[-10001-variable_id]:
60 self.variables[-10000-variable_id] -= 1 83 self.variables[-10001-variable_id] -= 1
61 self.frame, self.instruction_pointer = frame, instruction_pointer 84 self.frame, self.instruction_pointer = frame, instruction_pointer
62 85
63 86
64 def update(self): 87 def update(self):
65 frame = self.frame 88 frame = self.frame