1.1 --- a/micropython/data.py Sun Feb 22 02:04:30 2009 +0100
1.2 +++ b/micropython/data.py Sun Feb 22 22:31:21 2009 +0100
1.3 @@ -310,6 +310,14 @@
1.4 self.name, shortrepr(self.value), self.assignments
1.5 )
1.6
1.7 + def as_raw(self, objtable):
1.8 + return [
1.9 + (
1.10 + self.context and self.context.location,
1.11 + self.value and self.value.location
1.12 + )
1.13 + ]
1.14 +
1.15 # Instances are special in that they need to be wrapped together with context in
1.16 # a running program, but they are not generally constant.
1.17
1.18 @@ -357,6 +365,26 @@
1.19
1.20 __shortrepr__ = __repr__
1.21
1.22 + def as_raw(self, objtable):
1.23 + # NOTE: Need class details!
1.24 + return [
1.25 + DataObject(
1.26 + objtable.as_list().get_code(self.value_type_name()),
1.27 + objtable.get_index(self.value_type_name()),
1.28 + None,
1.29 + None,
1.30 + 1,
1.31 + self.value_type_name()
1.32 + )
1.33 + ] + self.raw_data()
1.34 +
1.35 + def raw_data(self):
1.36 + # NOTE: Start simple and use single entries for most types.
1.37 + if self.value_type_name() in ("__builtins__.tuple", "__builtins__.list"):
1.38 + return [len(self.value)] + list(self.value)
1.39 + else:
1.40 + return [self.value]
1.41 +
1.42 # Support constants as dictionary keys in order to build constant tables.
1.43
1.44 def __eq__(self, other):
1.45 @@ -433,6 +461,42 @@
1.46 def __shortrepr__(self):
1.47 return "Class(%r, %s)" % (self.name, shortrepr(self.parent))
1.48
1.49 + def as_raw(self, objtable):
1.50 + classcode = objtable.as_list().get_code(self.full_name())
1.51 + attrcode = objtable.get_index(self.full_name())
1.52 +
1.53 + # Append a template of an instance for use when instantiating classes.
1.54 +
1.55 + call_method = self.get("__call__")
1.56 + call_method_code_location = call_method and call_method.value.code_location
1.57 +
1.58 + # NOTE: The instantiator code is the first block of the class.
1.59 +
1.60 + instantiator_code_location = self.get_instantiator().blocks[0].location
1.61 +
1.62 + return [
1.63 + DataObject(
1.64 + classcode, attrcode, call_method_code_location,
1.65 + (
1.66 + call_method and len(call_method.value.positional_names),
1.67 + call_method and len(call_method.value.defaults)
1.68 + ),
1.69 + 1,
1.70 + self.full_name()
1.71 + ),
1.72 + DataObject(
1.73 + classcode, attrcode, instantiator_code_location,
1.74 + (
1.75 + len(self.get_instantiator().positional_names),
1.76 + len(self.get_instantiator().defaults)
1.77 + ),
1.78 + 0,
1.79 + self.full_name()
1.80 + )
1.81 + ]
1.82 +
1.83 + # Namespace-related methods.
1.84 +
1.85 def _context(self, value):
1.86
1.87 """
1.88 @@ -797,6 +861,24 @@
1.89 self.name, shortrepr(self.parent)
1.90 )
1.91
1.92 + def as_raw(self, objtable):
1.93 + # NOTE: Need class and parameter details! Should arguably be types.FunctionType.
1.94 + return [
1.95 + DataObject(
1.96 + objtable.as_list().get_code("__builtins__.function"),
1.97 + objtable.get_index("__builtins__.function"),
1.98 + self.code_location,
1.99 + (
1.100 + len(self.positional_names),
1.101 + len(self.defaults)
1.102 + ),
1.103 + 0,
1.104 + "__builtins__.function"
1.105 + )
1.106 + ]
1.107 +
1.108 + # Namespace-related methods.
1.109 +
1.110 def store_default(self, value):
1.111 attr = Attr(None, self, None, None, value)
1.112 attr.update(value, 1)
1.113 @@ -983,6 +1065,18 @@
1.114 def __shortrepr__(self):
1.115 return "Module(%r)" % self.name
1.116
1.117 + def as_raw(self, objtable):
1.118 + return [
1.119 + DataObject(
1.120 + objtable.as_list().get_code(self.full_name()),
1.121 + None, # module name not used as an attribute
1.122 + None,
1.123 + None,
1.124 + 0,
1.125 + self.full_name()
1.126 + )
1.127 + ]
1.128 +
1.129 # Attribute methods.
1.130
1.131 "Return the module attribute names provided by the module."