# HG changeset patch # User Paul Boddie # Date 1200189021 -3600 # Node ID 84d41da209193bdbc417d82f85b24f9d9959c17f # Parent 33a0e574dfa036e26a3cec3959ab50bd80b6a25c Added header/descriptor details for classes, modules and functions to the image. Added support for getting classcodes/function codes from tables. Improved constant support. Improved keyword argument support. diff -r 33a0e574dfa0 -r 84d41da20919 micropython/__init__.py --- a/micropython/__init__.py Sun Jan 13 02:14:46 2008 +0100 +++ b/micropython/__init__.py Sun Jan 13 02:50:21 2008 +0100 @@ -90,9 +90,14 @@ module.location = pos trans = micropython.ast.Translation(module, objtable, paramtable) + # Add header details. + + image.append(module) + pos += 1 + # Append constants to the image. - for const in module.constants: + for const in module.constants(): const.location = pos image.append(const) pos += 1 @@ -113,7 +118,12 @@ # Position the class in the image. - obj.code_location = obj.location = pos + obj.location = pos + + # Add header details. + + image.append(obj) + pos += 1 # Append class attributes to the image. @@ -131,6 +141,7 @@ # NOTE: methods and only other attribute-related code being # NOTE: generated here. + obj.code_location = obj.location = pos code = trans.get_code(obj) image += code pos += len(code) @@ -141,10 +152,16 @@ # Position the function in the image. - obj.code_location = obj.location = pos + obj.location = pos + + # Add header details. + + image.append(obj) + pos += 1 # Append the function code to the image. + obj.code_location = obj.location = pos code = trans.get_code(obj) image += code pos += len(code) diff -r 33a0e574dfa0 -r 84d41da20919 micropython/ast.py --- a/micropython/ast.py Sun Jan 13 02:14:46 2008 +0100 +++ b/micropython/ast.py Sun Jan 13 02:50:21 2008 +0100 @@ -64,7 +64,6 @@ self.objtable = objtable self.paramtable = paramtable self.unit = None - self.constants = {} # Wiring within the code. @@ -120,9 +119,7 @@ label.location = len(self.code) + self.unit.code_location def new_op(self, op): - print self.module.name, len(self.code) + self.unit.code_location, self.code.append(op) - print op # Visitor methods. @@ -217,8 +214,14 @@ # Evaluate the arguments. + positional = 1 + for i, arg in enumerate(node.args): if isinstance(arg, compiler.ast.Keyword): + if positional: + self.new_op(ReserveFrame(len(node.args) - i)) + positional = 0 + self.dispatch(arg.expr) # Combine the target details with the name to get the location. @@ -256,7 +259,9 @@ # Provide short-circuiting. """ - def visitConst(self, node): pass + def visitConst(self, node): + const = self.module.constant_values[node.value] + self.new_op(LoadConst(const)) def visitContinue(self, node): pass @@ -288,6 +293,9 @@ if self.unit is self.module: self.new_op(LoadConst(node.function)) self._visitName(node, (StoreName, StoreAttr)) + + # Visiting of the code occurs when get_code is invoked on this node. + else: self.dispatch(node.code) self.new_op(Return()) diff -r 33a0e574dfa0 -r 84d41da20919 micropython/inspect.py --- a/micropython/inspect.py Sun Jan 13 02:14:46 2008 +0100 +++ b/micropython/inspect.py Sun Jan 13 02:50:21 2008 +0100 @@ -391,6 +391,11 @@ self.all_objects = [] + # Constant records. + + self.constant_values = {} + self.constant_list = None # cache for constants + # Image generation details. self.location = None @@ -428,6 +433,15 @@ return self.modattr + def constants(self): + + "Return a list of constants." + + if self.constant_list is None: + self.constant_list = list(self.constant_values.values()) + + return self.constant_list + # Program visitors. class InspectedModule(ASTVisitor, Module): @@ -454,7 +468,6 @@ self.in_init = 0 self.namespaces = [] self.module = None - self.constants = [] def parse(self, filename): @@ -604,8 +617,7 @@ def visitConst(self, node): const = Const(node.value) - if not const in self.constants: - self.constants.append(const) + self.constant_values[node.value] = const visitContinue = NOP diff -r 33a0e574dfa0 -r 84d41da20919 micropython/rsvp.py --- a/micropython/rsvp.py Sun Jan 13 02:14:46 2008 +0100 +++ b/micropython/rsvp.py Sun Jan 13 02:50:21 2008 +0100 @@ -48,6 +48,7 @@ # Access to invocation frames in preparation. class MakeFrame(Instruction): pass +class ReserveFrame(Instruction): pass class DropFrame(Instruction): pass class StoreFrame(Instruction): pass diff -r 33a0e574dfa0 -r 84d41da20919 micropython/table.py --- a/micropython/table.py Sun Jan 13 02:14:46 2008 +0100 +++ b/micropython/table.py Sun Jan 13 02:50:21 2008 +0100 @@ -138,6 +138,7 @@ def __init__(self): self.attributes = set() self.table = {} + self.objnames = [] self.names = [] self.displaced_list = None @@ -152,6 +153,13 @@ if self.displaced_list is not None: self.displaced_list.add(objname, self.matrix_row(attributes)) + def object_names(self): + + "Return the object names used in the table." + + self.objnames = self.objnames or list(self.table.keys()) + return self.objnames + def attribute_names(self): "Return the attribute names used in the table." @@ -159,6 +167,12 @@ self.names = self.names or list(self.attributes) return self.names + def get_code(self, name): + + "Return the code of the given 'name'." + + return self.object_names().index(name) + def get_index(self, name): "Return the index of the given 'name'." diff -r 33a0e574dfa0 -r 84d41da20919 test.py --- a/test.py Sun Jan 13 02:14:46 2008 +0100 +++ b/test.py Sun Jan 13 02:50:21 2008 +0100 @@ -3,6 +3,10 @@ import micropython import sys +def show(importer): + for i, x in enumerate(importer.get_image()): + print i, x + i = micropython.Importer(sys.path) if len(sys.argv) < 2: m = i.load("micropython")