1.1 --- a/rsvp.py Wed Aug 27 00:45:14 2008 +0200
1.2 +++ b/rsvp.py Mon Sep 01 01:32:32 2008 +0200
1.3 @@ -90,6 +90,23 @@
1.4 self.result = None
1.5 self.exception = None
1.6
1.7 + def dump(self):
1.8 + print "PC", self.pc
1.9 + print "PC stack", self.pc_stack
1.10 + print "Frame stack", self.frame_stack
1.11 + print "Local stack pointers", self.local_sp_stack
1.12 + print "Invocation stack pointers", self.invocation_sp_stack
1.13 + print "Handler stack", self.handler_stack
1.14 + print
1.15 + print "Instruction", self.instruction
1.16 + print "Operand", self.operand
1.17 + print "Value", self.value
1.18 + print "Status", self.status
1.19 + print "Source", self.source
1.20 + print "Callable", self.callable
1.21 + print "Result", self.result
1.22 + print "Exception", self.exception
1.23 +
1.24 def load(self, address):
1.25
1.26 "Return the value at the given 'address'."
1.27 @@ -149,18 +166,14 @@
1.28 "Execute code in the memory at the current PC address."
1.29
1.30 self.instruction = self.load(self.pc)
1.31 - self.operand = self.instruction.get_operand()
1.32 -
1.33 - instruction_name = self.instruction.__class__.__name__
1.34 - if self.debug:
1.35 - print "%8d %s" % (self.pc, instruction_name)
1.36 -
1.37 - method = self.get_method(instruction_name)
1.38
1.39 # Process any inputs of the instruction.
1.40
1.41 self.process_inputs()
1.42 - next_pc = method()
1.43 +
1.44 + # Perform the instruction itself.
1.45 +
1.46 + next_pc = self.perform(self.instruction)
1.47
1.48 # Update the program counter.
1.49
1.50 @@ -169,15 +182,26 @@
1.51 else:
1.52 self.pc = next_pc
1.53
1.54 - def get_method(self, instruction_name):
1.55 + def get_method(self, instruction):
1.56 +
1.57 + "Return the handler method for the given 'instruction'."
1.58
1.59 - "Return the handler method for the given 'instruction_name'."
1.60 -
1.61 + instruction_name = instruction.__class__.__name__
1.62 + if self.debug:
1.63 + print "%8d %s" % (self.pc, instruction_name)
1.64 method = getattr(self, instruction_name, None)
1.65 if method is None:
1.66 raise IllegalInstruction, (self.pc, instruction_name)
1.67 return method
1.68
1.69 + def perform(self, instruction):
1.70 +
1.71 + "Perform the 'instruction', returning the next PC value or None."
1.72 +
1.73 + self.operand = instruction.get_operand()
1.74 + method = self.get_method(instruction)
1.75 + return method()
1.76 +
1.77 def process_inputs(self):
1.78
1.79 """
1.80 @@ -188,8 +212,7 @@
1.81
1.82 for input in (self.instruction.input, self.instruction.source):
1.83 if input is not None:
1.84 - method = self.get_method(input)
1.85 - method()
1.86 + self.perform(input)
1.87
1.88 def jump(self, addr, next):
1.89
1.90 @@ -294,6 +317,9 @@
1.91 frame = self.invocation_sp_stack.pop()
1.92 self.frame_stack = self.frame_stack[:frame] # reset stack before call
1.93
1.94 + def RecoverFrame(self):
1.95 + self.local_sp_stack.pop()
1.96 +
1.97 def StoreFrame(self):
1.98 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame
1.99 self.frame_stack[frame + self.operand] = self.value
1.100 @@ -372,6 +398,14 @@
1.101 frame = self.local_sp_stack[-1]
1.102 frame.extend([None] * self.operand)
1.103
1.104 + def AdjustFrame(self):
1.105 + if self.operand > 0:
1.106 + frame.append([None] * self.operand)
1.107 + elif self.operand == -1:
1.108 + self.invocation_sp_stack[-1] -= 1
1.109 + else:
1.110 + raise Exception, "AdjustFrame %r" % self.operand
1.111 +
1.112 def Return(self):
1.113 self.pc = self.pull_pc()
1.114