1.1 --- a/rsvp.py Fri Mar 20 00:32:43 2009 +0100
1.2 +++ b/rsvp.py Sun Apr 05 03:05:59 2009 +0200
1.3 @@ -78,16 +78,21 @@
1.4
1.5 "A really simple virtual processor."
1.6
1.7 - def __init__(self, memory, objlist, paramlist, pc=None, debug=0):
1.8 + def __init__(self, memory, objlist, paramlist, true_constant, false_constant, pc=None, debug=0):
1.9
1.10 """
1.11 Initialise the processor with a 'memory' (a list of values containing
1.12 - instructions and data) and the optional program counter 'pc'.
1.13 + instructions and data), the object and parameter lists 'objlist' and
1.14 + 'paramlist', the addresses 'true_constant' and 'false_constant', and the
1.15 + optional program counter 'pc'.
1.16 """
1.17
1.18 self.memory = memory
1.19 self.objlist = objlist.as_raw()
1.20 self.paramlist = paramlist.as_raw()
1.21 + self.true_constant = true_constant
1.22 + self.false_constant = false_constant
1.23 +
1.24 self.pc = pc or 0
1.25 self.debug = debug
1.26
1.27 @@ -120,6 +125,7 @@
1.28 # Native class constants.
1.29
1.30 cls = objlist.access("__builtins__", "int")
1.31 + self.int_class_location = cls and cls.get_value() and cls.get_value().location
1.32 self.int_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location
1.33
1.34 # Debugging attributes.
1.35 @@ -307,8 +313,11 @@
1.36 # of proper locations.
1.37
1.38 if isinstance(addr, str):
1.39 - self.native_functions[addr](self)
1.40 - return next
1.41 + handler = self.native_functions[addr](self)
1.42 + if handler is None:
1.43 + return next
1.44 + else:
1.45 + return handler
1.46 else:
1.47 self.push_pc(self.pc + 1)
1.48 return addr
1.49 @@ -639,7 +648,7 @@
1.50
1.51 # Test operand suitability.
1.52
1.53 - if not self._CheckInstance(left, self.int_instance_location) and self._CheckInstance(right, self.int_instance_location):
1.54 + if not self._CheckInstance(left, self.int_class_location) and self._CheckInstance(right, self.int_class_location):
1.55 self.exception = self.type_error
1.56 return self.RaiseException()
1.57
1.58 @@ -651,6 +660,10 @@
1.59 # Make a new object.
1.60
1.61 addr = self._MakeObject(2, self.int_instance_location)
1.62 +
1.63 + # Store the result.
1.64 + # NOTE: The data is considered ready to use.
1.65 +
1.66 self.save(addr + 1, self.load(left_data) + self.load(right_data))
1.67
1.68 # Return the new object.
1.69 @@ -658,10 +671,36 @@
1.70
1.71 self.result = addr, addr
1.72
1.73 + def builtins_int_bool(self):
1.74 + frame = self.local_sp_stack[-1]
1.75 +
1.76 + # Get operands addresses.
1.77 +
1.78 + left_context, left = self.frame_stack[frame]
1.79 +
1.80 + # Test operand suitability.
1.81 +
1.82 + if not self._CheckInstance(left, self.int_class_location):
1.83 + self.exception = self.type_error
1.84 + return self.RaiseException()
1.85 +
1.86 + # NOTE: Assume single location for data.
1.87 +
1.88 + left_data = left + 1
1.89 +
1.90 + # Test the data.
1.91 + # NOTE: The data is considered ready to use.
1.92 +
1.93 + if self.load(left_data) != 0:
1.94 + self.result = self.true_constant, self.true_constant
1.95 + else:
1.96 + self.result = self.false_constant, self.false_constant
1.97 +
1.98 native_functions = {
1.99 "__builtins__.object.__init__" : builtins_object_init,
1.100 "__builtins__.int.__init__" : builtins_int_init,
1.101 "__builtins__.int.__add__" : builtins_int_add,
1.102 + "__builtins__.int.__bool__" : builtins_int_bool,
1.103 }
1.104
1.105 # vim: tabstop=4 expandtab shiftwidth=4