# HG changeset patch # User Paul Boddie # Date 1232848576 -3600 # Node ID 831ab0ba3de61ba9b7cba1db5d45c60edf121e5a # Parent 09d961170f35a4f1b2adc05c08b6a5daeef6ade6 Renamed global_namespace attributes to module in data instances. Changed code generation to emit built-in objects minus code when with_builtins is false. Introduced nicer representations of some objects. Expanded IllegalAddress, testing for inappropriate types of address in the RSVP machine. diff -r 09d961170f35 -r 831ab0ba3de6 micropython/__init__.py --- a/micropython/__init__.py Sat Jan 24 02:41:59 2009 +0100 +++ b/micropython/__init__.py Sun Jan 25 02:56:16 2009 +0100 @@ -101,10 +101,6 @@ last_module = self.importer.modules_ordered[-1] for module in self.importer.modules_ordered: - - if not with_builtins and module.name == "__builtins__": - continue - pos = len(self.code) # Position the module in the image and make a translation. @@ -164,17 +160,29 @@ self.code += obj.default_attrs pos += len(obj.default_attrs) + # Omit built-in function code where requested. + + if not with_builtins and module.name == "__builtins__": + pass + # Append the function code to the image. - code = trans.get_code(obj) - self.code += code - pos += len(code) + else: + code = trans.get_code(obj) + self.code += code + pos += len(code) + + # Omit built-in module code where requested. + + if not with_builtins and module.name == "__builtins__": + pass # Append the module top-level code to the image. - code = trans.get_module_code(final=(module is last_module)) - self.code += code - pos += len(code) + else: + code = trans.get_module_code(final=(module is last_module)) + self.code += code + pos += len(code) return self.code @@ -191,17 +199,36 @@ pos = 0 for item in self.code: + + # Blocks are positioned leaving space for their expansion. + if isinstance(item, Block): item.location = pos pos += len(item.code) + + # Other multi-location objects. + elif isinstance(item, (micropython.data.Class, micropython.data.Const, micropython.data.Function, micropython.data.Module)): + item.location = pos pos += 1 + + # Code and details are associated with certain objects. + if isinstance(item, micropython.data.Function): - item.code_location = pos + + # Set the code location only where the code has been + # generated. + + if not with_builtins and item.module.name == "__builtins__": + item.code_location = item.full_name() + else: + item.code_location = pos + elif isinstance(item, micropython.data.Const): pos += len(self.raw_data(item)) + else: pos += 1 @@ -270,7 +297,10 @@ 0 )) - assert item.code_location == len(self.raw_code) + # Check the code location only where the code has been generated. + + assert (not with_builtins and item.module.name == "__builtins__") or \ + item.code_location == len(self.raw_code) elif isinstance(item, micropython.data.Module): assert item.location == len(self.raw_code) diff -r 09d961170f35 -r 831ab0ba3de6 micropython/data.py --- a/micropython/data.py Sat Jan 24 02:41:59 2009 +0100 +++ b/micropython/data.py Sun Jan 25 02:56:16 2009 +0100 @@ -58,10 +58,10 @@ "A mix-in providing dictionary methods." - def __init__(self, global_namespace=None): + def __init__(self, module=None): self.namespace = {} self.globals = set() - self.global_namespace = global_namespace + self.module = module self.finalised = 0 def __delitem__(self, name): @@ -98,7 +98,7 @@ """ if name in self.globals: - self.global_namespace.set(name, value, 0) + self.module.set(name, value, 0) else: attr = self._set(name, value) attr.update(attr.value, single_assignment) @@ -362,14 +362,14 @@ "An inspected class." - def __init__(self, name, parent, global_namespace=None, node=None): + def __init__(self, name, parent, module=None, node=None): """ Initialise the class with the given 'name', 'parent' object, optional - 'global_namespace' and optional AST 'node'. + 'module' and optional AST 'node'. """ - NamespaceDict.__init__(self, global_namespace) + NamespaceDict.__init__(self, module) self.name = name self.parent = parent self.astnode = node @@ -700,17 +700,16 @@ "An inspected function." - def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, global_namespace=None, node=None): + def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, module=None, node=None): """ Initialise the function with the given 'name', 'parent', list of 'argnames', list of 'defaults', the 'has_star' flag (indicating the presence of a * parameter), the 'has_dstar' flag (indicating the - presence of a ** parameter), optional 'global_namespace', and optional - AST 'node'. + presence of a ** parameter), optional 'module', and optional AST 'node'. """ - NamespaceDict.__init__(self, global_namespace) + NamespaceDict.__init__(self, module) self.name = name self.parent = parent self.argnames = argnames @@ -878,7 +877,7 @@ "Make a function from a method." function = Function(self.name, self.parent, self.argnames[1:], self.defaults, - self.has_star, self.has_dstar, self.global_namespace, self.astnode) + self.has_star, self.has_dstar, self.module, self.astnode) function.default_attrs = self.default_attrs return function @@ -886,8 +885,8 @@ "A module, class or function which was mentioned but could not be imported." - def __init__(self, name, parent_name, global_namespace=None): - NamespaceDict.__init__(self, global_namespace) + def __init__(self, name, parent_name, module=None): + NamespaceDict.__init__(self, module) self.name = name self.parent_name = parent_name self.parent = None diff -r 09d961170f35 -r 831ab0ba3de6 micropython/rsvp.py --- a/micropython/rsvp.py Sat Jan 24 02:41:59 2009 +0100 +++ b/micropython/rsvp.py Sun Jan 25 02:56:16 2009 +0100 @@ -3,7 +3,7 @@ """ RSVP instruction classes. -Copyright (C) 2007, 2008 Paul Boddie +Copyright (C) 2007, 2008, 2009 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,13 +19,15 @@ this program. If not, see . """ -from micropython.data import Attr +from micropython.data import Attr, Const def name(attr): if isinstance(attr, Attr): return attr.name or "" + elif isinstance(attr, Const): + return attr else: - return attr or "" + return attr.full_name() or "" class Instruction: diff -r 09d961170f35 -r 831ab0ba3de6 rsvp.py --- a/rsvp.py Sat Jan 24 02:41:59 2009 +0100 +++ b/rsvp.py Sun Jan 25 02:56:16 2009 +0100 @@ -5,7 +5,7 @@ ignore low-level operations and merely concentrate on variable access, structure access, structure allocation and function invocations. -Copyright (C) 2007, 2008 Paul Boddie +Copyright (C) 2007, 2008, 2009 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -56,7 +56,12 @@ pass class IllegalAddress(Exception): - pass + def __init__(self, address): + self.address = address + def __repr__(self): + return "IllegalAddress(%r)" % self.address + def __str__(self): + return repr(self) class EmptyPCStack(Exception): pass @@ -161,7 +166,9 @@ try: return self.memory[address] except IndexError: - raise IllegalAddress, address + raise IllegalAddress(address) + except TypeError: + raise IllegalAddress(address) def save(self, address, value): @@ -170,7 +177,9 @@ try: self.memory[address] = value except IndexError: - raise IllegalAddress, address + raise IllegalAddress(address) + except TypeError: + raise IllegalAddress(address) def new(self, size):