# HG changeset patch # User Paul Boddie # Date 1271718701 -7200 # Node ID 84a1131131c25596f372337faf2b1b61b8fe98b8 # Parent b6469cba3861a7bd9a28aacdbf393fb88788d5d4 Moved common functionality into a superclass, introducing generic raw image support. diff -r b6469cba3861 -r 84a1131131c2 micropython/raw.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/micropython/raw.py Tue Apr 20 01:11:41 2010 +0200 @@ -0,0 +1,74 @@ +#!/usr/bin/env python + +""" +Classes used to help with the generation of raw image data. + +Copyright (C) 2009, 2010 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 +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +class RawObject: + + "The basis for serialised program objects." + + def __init__(self, item): + self.item = item + + def set_location(self, location, with_builtins): + self.item.location = location + return location + 1 + + def finalise_location(self, with_builtins): + pass + + def is_generated(self, with_builtins): + return with_builtins or self.item.module.name != "__builtins__" or self.item.astnode.doc is not None + +class UntranslatableInstruction(Exception): + pass + +class RSVPTranslator: + + "A class which provides the basis for the translation of RSVP instructions." + + def get_method(self, instruction): + + "Return the handler method for the given 'instruction'." + + instruction_name = instruction.__class__.__name__ + method = getattr(self, instruction_name, None) + if method is None: + raise UntranslatableInstruction, instruction_name + return method + + def perform(self, instruction): + + "Perform the 'instruction', returning the next PC value or None." + + self.get_method(instruction)() + + def translate(self, code): + + "Translate the given 'code'." + + self.output_code = [] + + for instruction in code: + self.perform(instruction) + + def new_op(self, op): + self.output_code.append(op) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r b6469cba3861 -r 84a1131131c2 micropython/rsvp.py --- a/micropython/rsvp.py Mon Apr 19 01:30:02 2010 +0200 +++ b/micropython/rsvp.py Tue Apr 20 01:11:41 2010 +0200 @@ -21,6 +21,7 @@ from micropython.data import Attr, Const, Class, Function, Module from micropython.program import Block, DataObject, DataValue +from micropython.raw import RawObject def name(attr): if isinstance(attr, Attr): @@ -32,20 +33,10 @@ # Serialisation-related classes. -class RSVPObject: +class RSVPObject(RawObject): "A generic data object wrapper." - def __init__(self, item): - self.item = item - - def set_location(self, location, with_builtins): - self.item.location = location - return location + 1 - - def finalise_location(self, with_builtins): - pass - def _finalise_location(self, with_builtins): """ @@ -55,7 +46,7 @@ item = self.item - if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None: + if not self.is_generated(with_builtins): item.code_body_location = item.full_name() else: item.code_body_location = item.get_body_block().location @@ -112,9 +103,7 @@ # NOTE: The instantiator code is the first block of the class. - with_instantiator = with_builtins or item.module.name != "__builtins__" or item.astnode.doc is not None - - if not with_instantiator: + if not self.is_generated(with_builtins): instantiator_code_location = item.full_name() else: instantiator_code_location = item.get_instantiator().blocks[0].location @@ -185,7 +174,7 @@ # Set the code location only where the code has been # generated. - if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None: + if not self.is_generated(with_builtins): item.code_location = item.full_name() # Skip any defaults for named functions.