1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/micropython/raw.py Tue Apr 20 01:11:41 2010 +0200
1.3 @@ -0,0 +1,74 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Classes used to help with the generation of raw image data.
1.8 +
1.9 +Copyright (C) 2009, 2010 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +class RawObject:
1.26 +
1.27 + "The basis for serialised program objects."
1.28 +
1.29 + def __init__(self, item):
1.30 + self.item = item
1.31 +
1.32 + def set_location(self, location, with_builtins):
1.33 + self.item.location = location
1.34 + return location + 1
1.35 +
1.36 + def finalise_location(self, with_builtins):
1.37 + pass
1.38 +
1.39 + def is_generated(self, with_builtins):
1.40 + return with_builtins or self.item.module.name != "__builtins__" or self.item.astnode.doc is not None
1.41 +
1.42 +class UntranslatableInstruction(Exception):
1.43 + pass
1.44 +
1.45 +class RSVPTranslator:
1.46 +
1.47 + "A class which provides the basis for the translation of RSVP instructions."
1.48 +
1.49 + def get_method(self, instruction):
1.50 +
1.51 + "Return the handler method for the given 'instruction'."
1.52 +
1.53 + instruction_name = instruction.__class__.__name__
1.54 + method = getattr(self, instruction_name, None)
1.55 + if method is None:
1.56 + raise UntranslatableInstruction, instruction_name
1.57 + return method
1.58 +
1.59 + def perform(self, instruction):
1.60 +
1.61 + "Perform the 'instruction', returning the next PC value or None."
1.62 +
1.63 + self.get_method(instruction)()
1.64 +
1.65 + def translate(self, code):
1.66 +
1.67 + "Translate the given 'code'."
1.68 +
1.69 + self.output_code = []
1.70 +
1.71 + for instruction in code:
1.72 + self.perform(instruction)
1.73 +
1.74 + def new_op(self, op):
1.75 + self.output_code.append(op)
1.76 +
1.77 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/micropython/rsvp.py Mon Apr 19 01:30:02 2010 +0200
2.2 +++ b/micropython/rsvp.py Tue Apr 20 01:11:41 2010 +0200
2.3 @@ -21,6 +21,7 @@
2.4
2.5 from micropython.data import Attr, Const, Class, Function, Module
2.6 from micropython.program import Block, DataObject, DataValue
2.7 +from micropython.raw import RawObject
2.8
2.9 def name(attr):
2.10 if isinstance(attr, Attr):
2.11 @@ -32,20 +33,10 @@
2.12
2.13 # Serialisation-related classes.
2.14
2.15 -class RSVPObject:
2.16 +class RSVPObject(RawObject):
2.17
2.18 "A generic data object wrapper."
2.19
2.20 - def __init__(self, item):
2.21 - self.item = item
2.22 -
2.23 - def set_location(self, location, with_builtins):
2.24 - self.item.location = location
2.25 - return location + 1
2.26 -
2.27 - def finalise_location(self, with_builtins):
2.28 - pass
2.29 -
2.30 def _finalise_location(self, with_builtins):
2.31
2.32 """
2.33 @@ -55,7 +46,7 @@
2.34
2.35 item = self.item
2.36
2.37 - if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None:
2.38 + if not self.is_generated(with_builtins):
2.39 item.code_body_location = item.full_name()
2.40 else:
2.41 item.code_body_location = item.get_body_block().location
2.42 @@ -112,9 +103,7 @@
2.43
2.44 # NOTE: The instantiator code is the first block of the class.
2.45
2.46 - with_instantiator = with_builtins or item.module.name != "__builtins__" or item.astnode.doc is not None
2.47 -
2.48 - if not with_instantiator:
2.49 + if not self.is_generated(with_builtins):
2.50 instantiator_code_location = item.full_name()
2.51 else:
2.52 instantiator_code_location = item.get_instantiator().blocks[0].location
2.53 @@ -185,7 +174,7 @@
2.54 # Set the code location only where the code has been
2.55 # generated.
2.56
2.57 - if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None:
2.58 + if not self.is_generated(with_builtins):
2.59 item.code_location = item.full_name()
2.60
2.61 # Skip any defaults for named functions.