micropython

Change of rsvp.py

254:13558f703a70
rsvp.py
     1.1 --- a/rsvp.py	Sun Jul 12 22:09:41 2009 +0200
     1.2 +++ b/rsvp.py	Mon Jul 13 01:02:42 2009 +0200
     1.3 @@ -127,9 +127,6 @@
     1.4          cls = self._get_class("__builtins__", "TypeError")
     1.5          self.type_error = cls.location
     1.6          self.type_error_instance = cls.instance_template_location
     1.7 -        cls = self._get_class("__builtins__", "IndexError")
     1.8 -        self.index_error = cls.location
     1.9 -        self.index_error_instance = cls.instance_template_location
    1.10  
    1.11          # Debugging attributes.
    1.12  
    1.13 @@ -781,6 +778,9 @@
    1.14          cls = self.machine._get_class("__builtins__", "list")
    1.15          self.list_class = cls.location
    1.16          self.list_instance = cls.instance_template_location
    1.17 +        cls = self.machine._get_class("__builtins__", "IndexError")
    1.18 +        self.index_error = cls.location
    1.19 +        self.index_error_instance = cls.instance_template_location
    1.20          cls = self.machine._get_class("__builtins__", "tuple")
    1.21          self.tuple_class = cls.location
    1.22  
    1.23 @@ -1001,6 +1001,7 @@
    1.24          # NOTE: Assume single location for data and header.
    1.25  
    1.26          item_pos = self.machine.load(item + 1)
    1.27 +
    1.28          if item_pos >= 0 and item_pos < nelements:
    1.29              pass
    1.30          elif item_pos < 0 and item_pos >= -nelements:
    1.31 @@ -1088,6 +1089,66 @@
    1.32  
    1.33              self.machine.save(obj + 1, (None, new_fragment))
    1.34  
    1.35 +    def builtins_tuple_len(self):
    1.36 +        frame = self.local_sp_stack[-1]
    1.37 +
    1.38 +        # Get the tuple address.
    1.39 +
    1.40 +        obj_context, obj = self.frame_stack[frame]
    1.41 +
    1.42 +        # Get the header.
    1.43 +        # NOTE: Assume single location for header.
    1.44 +
    1.45 +        header = self.machine.load(obj)
    1.46 +        nelements = header.size - 1
    1.47 +
    1.48 +        # Make a new object.
    1.49 +
    1.50 +        addr = self.machine._MakeObject(2, self.int_instance)
    1.51 +
    1.52 +        # Store the result.
    1.53 +        # NOTE: The data is considered ready to use.
    1.54 +
    1.55 +        self.machine.save(addr + 1, nelements)
    1.56 +
    1.57 +        # Return the new object.
    1.58 +        # Introduce object as context for the new object.
    1.59 +
    1.60 +        self.machine.result = addr, addr
    1.61 +
    1.62 +    def builtins_tuple_getitem(self):
    1.63 +        frame = self.local_sp_stack[-1]
    1.64 +
    1.65 +        # Get the operand address.
    1.66 +
    1.67 +        item_context, item = self.frame_stack[frame + 1]
    1.68 +
    1.69 +        # Get the tuple address.
    1.70 +
    1.71 +        obj_context, obj = self.frame_stack[frame]
    1.72 +
    1.73 +        # Get the header.
    1.74 +        # NOTE: Assume single location for header.
    1.75 +
    1.76 +        header = self.machine.load(obj)
    1.77 +        nelements = header.size - 1
    1.78 +
    1.79 +        # NOTE: Assume single location for data and header.
    1.80 +
    1.81 +        item_pos = self.machine.load(item + 1)
    1.82 +
    1.83 +        if item_pos >= 0 and item_pos < nelements:
    1.84 +            pass
    1.85 +        elif item_pos < 0 and item_pos >= -nelements:
    1.86 +            item_pos = nelements + item_pos
    1.87 +        else:
    1.88 +            self.machine.exception = self.machine._MakeObject(2, self.index_error_instance)
    1.89 +            return self.machine.RaiseException()
    1.90 +
    1.91 +        # NOTE: Assume single location for header.
    1.92 +
    1.93 +        self.machine.result = self.machine.load(obj + 1 + item_pos)
    1.94 +
    1.95      def builtins_object_init(self):
    1.96          pass
    1.97  
    1.98 @@ -1111,6 +1172,11 @@
    1.99          "__builtins__.list.__getitem__" : builtins_list_getitem,
   1.100          "__builtins__.list.__len__" : builtins_list_len,
   1.101          "__builtins__.list.append" : builtins_list_append,
   1.102 +        "__builtins__.tuple.__len__" : builtins_tuple_len,
   1.103 +        "__builtins__.tuple.__getitem__" : builtins_tuple_getitem,
   1.104 +
   1.105 +        # Native initialisers:
   1.106 +
   1.107          "__builtins__.object.__init__" : builtins_object_init,        # NOTE: A no-operation.
   1.108          "__builtins__.BaseException.__init__" : builtins_object_init, # NOTE: To be made distinct, potentially in the builtins module.
   1.109