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