1.1 --- a/rsvp.py Sun May 18 19:59:26 2008 +0200
1.2 +++ b/rsvp.py Mon May 19 00:22:00 2008 +0200
1.3 @@ -151,23 +151,29 @@
1.4 except IndexError:
1.5 raise EmptyMetadataStack
1.6
1.7 - def execute(self):
1.8 + def run(self):
1.9
1.10 "Execute code in the memory, starting from the current PC address."
1.11
1.12 try:
1.13 while 1:
1.14 - instruction = self.load(self.pc)
1.15 - if self.debug:
1.16 - print "%8d %s" % (self.pc, instruction)
1.17 - method = getattr(self, instruction, None)
1.18 - if method is None:
1.19 - raise IllegalInstruction, self.pc
1.20 - else:
1.21 - method()
1.22 + self.execute()
1.23 except EmptyPCStack:
1.24 pass
1.25
1.26 + def execute(self):
1.27 +
1.28 + "Execute code in the memory at the current PC address."
1.29 +
1.30 + instruction = self.load(self.pc).__class__.__name__
1.31 + if self.debug:
1.32 + print "%8d %s" % (self.pc, instruction)
1.33 + method = getattr(self, instruction, None)
1.34 + if method is None:
1.35 + raise IllegalInstruction, self.pc
1.36 + else:
1.37 + method()
1.38 +
1.39 def jump(self, addr, next):
1.40
1.41 """
1.42 @@ -198,9 +204,9 @@
1.43 self.pc += 1
1.44
1.45 def JumpWithFrame(self):
1.46 - addr = self.load(self.pc + 1)
1.47 + addr = self.pull()
1.48 self.frame_sp += 1 # adopt the added frame
1.49 - self.jump(addr, self.pc + 2)
1.50 + self.jump(addr, self.pc + 1) # return to the instruction after this one
1.51
1.52 def LoadName(self):
1.53
1.54 @@ -210,10 +216,10 @@
1.55 frame, push the retrieved value onto the stack.
1.56 """
1.57
1.58 - n = self.load(self.pc + 1)
1.59 + n = self.load(self.pc).get_operand()
1.60 frame = self.frame_stack[self.frame_sp]
1.61 self.push(self.value_stack[frame + n])
1.62 - self.pc += 2
1.63 + self.pc += 1
1.64
1.65 def StoreName(self):
1.66
1.67 @@ -223,10 +229,10 @@
1.68 position n in the current stack frame.
1.69 """
1.70
1.71 - n = self.load(self.pc + 1)
1.72 + n = self.load(self.pc).get_operand()
1.73 frame = self.frame_stack[self.frame_sp]
1.74 self.value_stack[frame + n] = self.pull()
1.75 - self.pc += 2
1.76 + self.pc += 1
1.77
1.78 LoadTemp = LoadName
1.79 StoreTemp = StoreName
1.80 @@ -242,9 +248,10 @@
1.81 NOTE: This assumes that constants are encoded with context.
1.82 """
1.83
1.84 - addr = self.load(self.pc + 1)
1.85 - self.push(addr)
1.86 - self.pc += 2
1.87 + addr = self.load(self.pc).get_operand()
1.88 + value = (None, addr)
1.89 + self.push(value)
1.90 + self.pc += 1
1.91
1.92 def LoadAttr(self):
1.93
1.94 @@ -255,10 +262,11 @@
1.95 value on the top of the stack with the retrieved value.
1.96 """
1.97
1.98 - n = self.load(self.pc + 1)
1.99 - ref = self.pull()
1.100 - self.push(self.load(ref + n))
1.101 - self.pc += 2
1.102 + n = self.load(self.pc).get_operand()
1.103 + context, ref = self.pull()
1.104 + value = self.load(ref + n)
1.105 + self.push(value)
1.106 + self.pc += 1
1.107
1.108 def StoreAttr(self):
1.109
1.110 @@ -269,39 +277,38 @@
1.111 also removing the next value on the stack.
1.112 """
1.113
1.114 - n = self.load(self.pc + 1)
1.115 + n = self.load(self.pc).get_operand()
1.116 value = self.pull()
1.117 - self.save(self.pull() + n, value)
1.118 - self.pc += 2
1.119 + context, ref = self.pull()
1.120 + self.save(ref + n, value)
1.121 + self.pc += 1
1.122
1.123 def LoadAddress(self):
1.124
1.125 """
1.126 - LoadAddress addr, #n
1.127 - Load from position n in reference at addr: get the contents of position
1.128 - n in the memory referenced by addr, adding the retrieved value to the
1.129 - top of the stack.
1.130 + LoadAddress addr
1.131 + Load from addr: get the contents of the location in memory referenced by
1.132 + addr, adding the retrieved value to the top of the stack.
1.133 """
1.134
1.135 - red = self.load(self.pc + 1)
1.136 - n = self.load(self.pc + 2)
1.137 - self.push(self.load(ref + n))
1.138 - self.pc += 3
1.139 + addr = self.load(self.pc).get_operand()
1.140 + value = self.load(addr)
1.141 + self.push(value)
1.142 + self.pc += 1
1.143
1.144 def StoreAddress(self):
1.145
1.146 """
1.147 - StoreAddress addr, #n
1.148 - Save to position n in reference at addr: pull a value from the stack and
1.149 - save it to position n in the memory referenced by addr, also removing
1.150 - the value on the top of the stack.
1.151 + StoreAddress addr
1.152 + Save to addr: pull a value from the stack and save it to the location in
1.153 + memory referenced by addr, also removing the value on the top of the
1.154 + stack.
1.155 """
1.156
1.157 - ref = self.load(self.pc + 1)
1.158 - n = self.load(self.pc + 2)
1.159 + addr = self.load(self.pc).get_operand()
1.160 value = self.pull()
1.161 - self.save(ref + n, value)
1.162 - self.pc += 3
1.163 + self.save(addr, value)
1.164 + self.pc += 1
1.165
1.166 def Return(self):
1.167
1.168 @@ -321,12 +328,12 @@
1.169 represents a true value, jump to address addr.
1.170 """
1.171
1.172 - addr = self.load(self.pc + 1)
1.173 + addr = self.load(self.pc).get_operand()
1.174 value = self.pull()
1.175 if value:
1.176 self.pc = addr
1.177 else:
1.178 - self.pc += 2
1.179 + self.pc += 1
1.180
1.181 def JumpIfFalse(self):
1.182
1.183 @@ -336,12 +343,12 @@
1.184 represents a false value, jump to address addr.
1.185 """
1.186
1.187 - addr = self.load(self.pc + 1)
1.188 + addr = self.load(self.pc).get_operand()
1.189 value = self.pull()
1.190 if not value:
1.191 self.pc = addr
1.192 else:
1.193 - self.pc += 2
1.194 + self.pc += 1
1.195
1.196 def StoreFrame(self):
1.197
1.198 @@ -352,11 +359,11 @@
1.199 given position.
1.200 """
1.201
1.202 - pos = self.load(self.pc + 1)
1.203 + pos = self.load(self.pc).get_operand()
1.204 value = self.pull()
1.205 frame = self.frame_stack[-1] # different from the current frame after MakeFrame
1.206 self.value_stack[frame + pos] = value
1.207 - self.pc += 2
1.208 + self.pc += 1
1.209
1.210 # Library functions.
1.211