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, 2011, 2012 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, objtable, with_builtins): 30 self.item.location = location 31 return location + 1 32 33 def finalise_location(self, with_builtins): 34 pass 35 36 def finalise_body_location(self, with_builtins): 37 pass 38 39 def is_generated(self, with_builtins): 40 return with_builtins or self.item.module.name not in ("__builtins__", "native") or self.item.astnode.doc is not None 41 42 class UntranslatableInstruction(Exception): 43 pass 44 45 class RSVPTranslator: 46 47 "A class which provides the basis for the translation of RSVP instructions." 48 49 def get_method(self, instruction): 50 51 "Return the handler method for the given 'instruction'." 52 53 instruction_name = instruction.__class__.__name__ 54 method = getattr(self, instruction_name, None) 55 if method is None: 56 raise UntranslatableInstruction, instruction_name 57 return method 58 59 def perform(self, instruction): 60 61 "Perform the 'instruction', returning the next PC value or None." 62 63 self.get_method(instruction)() 64 65 def translate(self, code): 66 67 "Translate the given 'code'." 68 69 self.output_code = [] 70 71 for instruction in code: 72 self.perform(instruction) 73 74 def new_op(self, op): 75 self.output_code.append(op) 76 77 # vim: tabstop=4 expandtab shiftwidth=4