1.1 --- a/micropython/rsvp.py Sat Apr 30 23:36:09 2011 +0200
1.2 +++ b/micropython/rsvp.py Sun May 01 00:37:51 2011 +0200
1.3 @@ -68,7 +68,7 @@
1.4
1.5 "A wrapper for blocks."
1.6
1.7 - def set_location(self, location, with_builtins):
1.8 + def set_location(self, location, objtable, with_builtins):
1.9 item = self.item
1.10 item.location = location
1.11 return location + len(item.code)
1.12 @@ -80,12 +80,12 @@
1.13
1.14 "A wrapper for classes."
1.15
1.16 - def set_location(self, location, with_builtins):
1.17 + def set_location(self, location, objtable, with_builtins):
1.18 self.item.instance_template_location = location
1.19
1.20 # Include the instance template and __class__ attribute in the size.
1.21
1.22 - return RSVPObject.set_location(self, location + 1, with_builtins)
1.23 + return RSVPObject.set_location(self, location + 1, objtable, with_builtins)
1.24
1.25 def finalise_location(self, with_builtins):
1.26 self._finalise_location(with_builtins)
1.27 @@ -140,44 +140,82 @@
1.28
1.29 "A wrapper for constants."
1.30
1.31 - def set_location(self, location, with_builtins):
1.32 - location = RSVPObject.set_location(self, location, with_builtins)
1.33 - return location + len(self.raw_data())
1.34 + def set_location(self, location, objtable, with_builtins):
1.35 + item = self.item
1.36 + value = item.get_value()
1.37 + type_name = self.type_name(item.value_type_name(), value, objtable)
1.38 +
1.39 + location = RSVPObject.set_location(self, location, objtable, with_builtins)
1.40 + return location + self.raw_size(type_name, value)
1.41
1.42 def as_raw(self, objtable, paramtable, with_builtins):
1.43 item = self.item
1.44 - classcode = objtable.as_list().get_code(item.value_type_name())
1.45 - attrcode = objtable.get_index(item.value_type_name())
1.46 + value = item.get_value()
1.47 + type_name = self.type_name(item.value_type_name(), value, objtable)
1.48 + raw_data = self.raw_data(type_name, value, objtable)
1.49 + size = self.raw_size(type_name, value) + 1
1.50 +
1.51 + # Generate the footprint of the constant.
1.52 +
1.53 + classcode = objtable.as_list().get_code(type_name)
1.54 + attrcode = objtable.get_index(type_name)
1.55 +
1.56 return [
1.57 DataObject(
1.58 classcode,
1.59 attrcode, # is instance
1.60 None,
1.61 - item.value_type_name(),
1.62 - 2 # size (header plus data)
1.63 + type_name,
1.64 + size # header plus data
1.65 )
1.66
1.67 # NOTE: The RSVP library needs changing if more attributes are added
1.68 - # NOTE: here.
1.69 + # NOTE: here (such as a __class__ attribute for all instances).
1.70 +
1.71 + ] + raw_data
1.72 +
1.73 + def type_name(self, type_name, value, objtable):
1.74
1.75 - ] + self.raw_data()
1.76 + # Change the type of string constants if they might be used as attribute
1.77 + # accessors.
1.78 +
1.79 + accessor_cls = "__builtins__._accessor"
1.80 +
1.81 + if type_name == "__builtins__.str" and value in objtable.attribute_names() and \
1.82 + accessor_cls in objtable.object_names():
1.83
1.84 - def raw_data(self):
1.85 - item = self.item
1.86 - value = item.get_value()
1.87 - # NOTE: Start simple and use single entries for most types.
1.88 - if item.value_type_name() in ("__builtins__.tuple", "__builtins__.list"):
1.89 + return accessor_cls
1.90 + else:
1.91 + return type_name
1.92 +
1.93 + def raw_data(self, type_name, value, objtable):
1.94 +
1.95 + # NOTE: Start simple and use single entries for most types, even though
1.96 + # NOTE: a concrete representation may use multiple memory words to
1.97 + # NOTE: represent the data.
1.98 +
1.99 + if type_name in ("__builtins__.tuple", "__builtins__.list"):
1.100 return [len(value)] + list(value)
1.101 + elif type_name == "__builtins__._accessor":
1.102 + return [value, objtable.get_index(value)]
1.103 else:
1.104 return [value]
1.105
1.106 + def raw_size(self, type_name, value):
1.107 + if type_name in ("__builtins__.tuple", "__builtins__.list"):
1.108 + return 1 + len(value)
1.109 + elif type_name == "__builtins__._accessor":
1.110 + return 2
1.111 + else:
1.112 + return 1
1.113 +
1.114 class RSVPFunction(RSVPObject):
1.115
1.116 "A wrapper for functions."
1.117
1.118 - def set_location(self, location, with_builtins):
1.119 + def set_location(self, location, objtable, with_builtins):
1.120 item = self.item
1.121 - location = RSVPObject.set_location(self, location, with_builtins)
1.122 + location = RSVPObject.set_location(self, location, objtable, with_builtins)
1.123
1.124 # Set the code location only where the code has been
1.125 # generated.