# HG changeset patch # User Paul Boddie # Date 1224980069 -7200 # Node ID 63855c5d72afa61c746337b8ebf808eefeb782e4 # Parent b21ccdf5fb579ce095febf4151426a44ad198c98 Added methods to convert from blocks to code sequences. Added the missing location attribute to Block instances. diff -r b21ccdf5fb57 -r 63855c5d72af micropython/ast.py --- a/micropython/ast.py Sat Oct 25 21:51:23 2008 +0200 +++ b/micropython/ast.py Sun Oct 26 02:14:29 2008 +0200 @@ -106,7 +106,22 @@ self.optimiser.reset() + def get_code_for_blocks(self, blocks, location): + + """ + Return the code for the given 'blocks', appearing at the given location. + """ + + code = [] + for block in blocks: + block.location = len(code) + location + code += block.code + return code + def get_module_code(self, final=0): + return self.get_code_for_blocks(self.get_module_blocks(final), self.module.code_location) + + def get_module_blocks(self, final=0): """ Return the top-level module code including finalising code if 'final' is @@ -131,6 +146,9 @@ return self.blocks def get_code(self, unit): + return self.get_code_for_blocks(self.get_blocks(unit), unit.code_location) + + def get_blocks(self, unit): "Return the code for the given 'unit'." @@ -147,6 +165,9 @@ return self.blocks def get_instantiator_code(self, cls): + return self.get_code_for_blocks(self.get_instantiator_blocks(cls), cls.get_instantiator().code_location) + + def get_instantiator_blocks(self, cls): "Return the code for the given class 'cls'." @@ -409,7 +430,7 @@ """ ntemp = self.max_temp_position + 1 - extend.attr = ntemp + node.unit.local_usage # NOTE: See get_code for similar code. + extend.attr = ntemp + node.unit.local_usage # NOTE: See get_blocks for similar code. # Code writing methods. @@ -1677,7 +1698,7 @@ self._generateFunctionDefaults(node.unit) - # Visiting of the code occurs when get_code is invoked on this node. + # Visiting of the code occurs when get_blocks is invoked on this node. else: extend = ExtendFrame() @@ -1729,7 +1750,7 @@ self.new_op(temp) #self.discard_temp(temp) - # Visiting of the code occurs when get_code is invoked on this node. + # Visiting of the code occurs when get_blocks is invoked on this node. else: self.dispatch(node.code) diff -r b21ccdf5fb57 -r 63855c5d72af micropython/common.py --- a/micropython/common.py Sat Oct 25 21:51:23 2008 +0200 +++ b/micropython/common.py Sun Oct 26 02:14:29 2008 +0200 @@ -79,9 +79,10 @@ def __init__(self): self.code = [] + self.location = None def __repr__(self): - return "Block(%r)" % id(self) + return "Block(%r, location=%r)" % (id(self), self.location) class Label: