1.1 --- a/micropython/__init__.py Sun Feb 22 02:04:30 2009 +0100
1.2 +++ b/micropython/__init__.py Sun Feb 22 22:31:21 2009 +0100
1.3 @@ -29,8 +29,14 @@
1.4 importer.load_from_file(filename)
1.5 importer.vacuum()
1.6
1.7 -Such importer objects are the most convenient mechanism through which the
1.8 -functionality of the micropython package may be accessed.
1.9 +To generate programs, the above importer should be supplied in the
1.10 +initialisation of a program instance, and then various methods are called:
1.11 +
1.12 +program = Program(importer)
1.13 +image = program.get_raw_image()
1.14 +
1.15 +Such importer and program objects are the most convenient mechanism through
1.16 +which the functionality of the micropython package may be accessed.
1.17 """
1.18
1.19 from micropython.common import *
1.20 @@ -242,7 +248,7 @@
1.21 item.code_location = pos + len(item.defaults)
1.22
1.23 elif isinstance(item, micropython.data.Const):
1.24 - pos += len(self.raw_data(item))
1.25 + pos += len(item.raw_data())
1.26
1.27 else:
1.28 pos += 1
1.29 @@ -254,97 +260,26 @@
1.30 for item in self.code:
1.31
1.32 if isinstance(item, micropython.data.Attr):
1.33 - self.raw_code.append((
1.34 - item.context and item.context.location,
1.35 - item.value and item.value.location # no useful context is provided
1.36 - ))
1.37 + self.raw_code += item.as_raw(objtable)
1.38
1.39 elif isinstance(item, Block):
1.40 - self.raw_code += self.raw_block(item)
1.41 + assert item.location == len(self.raw_code)
1.42 + self.raw_code += item.as_raw(objtable)
1.43
1.44 # Using classcode, attrcode, codeaddr, codedetails, instance.
1.45
1.46 elif isinstance(item, micropython.data.Class):
1.47 assert item.instance_template_location == len(self.raw_code)
1.48 -
1.49 - classcode = objtable.as_list().get_code(item.full_name())
1.50 - attrcode = objtable.get_index(item.full_name())
1.51 -
1.52 - # Append a template of an instance for use when
1.53 - # instantiating classes.
1.54 -
1.55 - call_method = item.get("__call__")
1.56 - call_method_code_location = call_method and call_method.value.code_location
1.57 -
1.58 - self.raw_code.append(
1.59 - DataObject(
1.60 - classcode,
1.61 - attrcode,
1.62 - call_method_code_location,
1.63 - (
1.64 - call_method and len(call_method.value.positional_names),
1.65 - call_method and len(call_method.value.defaults)
1.66 - ),
1.67 - 1,
1.68 - item.full_name()
1.69 - )
1.70 - )
1.71 -
1.72 - assert item.location == len(self.raw_code)
1.73 -
1.74 - # NOTE: The instantiator code is the first block of the class.
1.75 -
1.76 - instantiator_code_location = item.get_instantiator().blocks[0].location
1.77 -
1.78 - # NOTE: Need initialiser details!
1.79 - self.raw_code.append(
1.80 - DataObject(
1.81 - classcode,
1.82 - attrcode,
1.83 - instantiator_code_location,
1.84 - (
1.85 - len(item.get_instantiator().positional_names),
1.86 - len(item.get_instantiator().defaults)
1.87 - ),
1.88 - 0,
1.89 - item.full_name()
1.90 - )
1.91 - )
1.92 + self.raw_code += item.as_raw(objtable)
1.93 + assert item.location == len(self.raw_code) - 1
1.94
1.95 elif isinstance(item, micropython.data.Const):
1.96 assert item.location == len(self.raw_code)
1.97 -
1.98 - # NOTE: Need class details!
1.99 - self.raw_code.append(
1.100 - DataObject(
1.101 - objtable.as_list().get_code(item.value_type_name()),
1.102 - objtable.get_index(item.value_type_name()),
1.103 - None,
1.104 - None,
1.105 - 1,
1.106 - item.value_type_name()
1.107 - )
1.108 - )
1.109 -
1.110 - self.raw_code += self.raw_data(item)
1.111 + self.raw_code += item.as_raw(objtable)
1.112
1.113 elif isinstance(item, micropython.data.Function):
1.114 assert item.location == len(self.raw_code)
1.115 -
1.116 - # NOTE: Need class and parameter details! Should arguably be types.FunctionType.
1.117 - self.raw_code.append(
1.118 - DataObject(
1.119 - objtable.as_list().get_code("__builtins__.function"),
1.120 - objtable.get_index("__builtins__.function"),
1.121 - item.code_location,
1.122 - (
1.123 - len(item.positional_names),
1.124 - len(item.defaults)
1.125 - ),
1.126 - 0,
1.127 - "__builtins__.function"
1.128 - )
1.129 - )
1.130 + self.raw_code += item.as_raw(objtable)
1.131
1.132 # Check the code location only where the code has been generated.
1.133
1.134 @@ -353,17 +288,7 @@
1.135
1.136 elif isinstance(item, micropython.data.Module):
1.137 assert item.location == len(self.raw_code)
1.138 -
1.139 - self.raw_code.append(
1.140 - DataObject(
1.141 - objtable.as_list().get_code(item.full_name()),
1.142 - None, # module name not used as an attribute
1.143 - None,
1.144 - None,
1.145 - 0,
1.146 - item.full_name()
1.147 - )
1.148 - )
1.149 + self.raw_code += item.as_raw(objtable)
1.150
1.151 else:
1.152 self.raw_code.append(item)
1.153 @@ -380,29 +305,6 @@
1.154 self.code_location = self.importer.modules["__main__"].code_location
1.155 return self.raw_code
1.156
1.157 - def raw_block(self, block):
1.158 -
1.159 - "Return the code for the given 'block'."
1.160 -
1.161 - assert block.location == len(self.raw_code)
1.162 - for i, item in enumerate(block.code):
1.163 - if hasattr(item, "location"):
1.164 - item.location = location + i
1.165 - return block.code
1.166 -
1.167 - def raw_data(self, item):
1.168 -
1.169 - "Return the data for the given 'item'."
1.170 -
1.171 - datatype = item.value_type_name()
1.172 -
1.173 - # NOTE: Start simple and use single entries for most types.
1.174 -
1.175 - if datatype in ("__builtins__.tuple", "__builtins__.list"):
1.176 - return [len(item.value)] + list(item.value)
1.177 - else:
1.178 - return [item.value]
1.179 -
1.180 def get_object_table(self):
1.181
1.182 "Return a table with details of attributes for classes and modules."