1 #!/usr/bin/env python 2 3 """ 4 Classes used to help with the generation of raw image data. 5 6 Copyright (C) 2009, 2010 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 class RawObject: 23 24 "The basis for serialised program objects." 25 26 def __init__(self, item): 27 self.item = item 28 29 def set_location(self, location, with_builtins): 30 self.item.location = location 31 return location + 1 32 33 def finalise_location(self, with_builtins): 34 pass 35 36 def is_generated(self, with_builtins): 37 return with_builtins or self.item.module.name != "__builtins__" or self.item.astnode.doc is not None 38 39 class UntranslatableInstruction(Exception): 40 pass 41 42 class RSVPTranslator: 43 44 "A class which provides the basis for the translation of RSVP instructions." 45 46 def get_method(self, instruction): 47 48 "Return the handler method for the given 'instruction'." 49 50 instruction_name = instruction.__class__.__name__ 51 method = getattr(self, instruction_name, None) 52 if method is None: 53 raise UntranslatableInstruction, instruction_name 54 return method 55 56 def perform(self, instruction): 57 58 "Perform the 'instruction', returning the next PC value or None." 59 60 self.get_method(instruction)() 61 62 def translate(self, code): 63 64 "Translate the given 'code'." 65 66 self.output_code = [] 67 68 for instruction in code: 69 self.perform(instruction) 70 71 def new_op(self, op): 72 self.output_code.append(op) 73 74 # vim: tabstop=4 expandtab shiftwidth=4