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