paulb@31 | 1 | #!/usr/bin/env python |
paulb@31 | 2 | |
paulb@31 | 3 | """ |
paulb@31 | 4 | Fix name-related operations. The code in this module operates upon nodes which |
paulb@31 | 5 | are produced when simplifying AST node trees originating from the compiler |
paulb@31 | 6 | module. |
paulb@31 | 7 | |
paulb@31 | 8 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@31 | 9 | |
paulb@31 | 10 | This software is free software; you can redistribute it and/or |
paulb@31 | 11 | modify it under the terms of the GNU General Public License as |
paulb@31 | 12 | published by the Free Software Foundation; either version 2 of |
paulb@31 | 13 | the License, or (at your option) any later version. |
paulb@31 | 14 | |
paulb@31 | 15 | This software is distributed in the hope that it will be useful, |
paulb@31 | 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@31 | 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@31 | 18 | GNU General Public License for more details. |
paulb@31 | 19 | |
paulb@31 | 20 | You should have received a copy of the GNU General Public |
paulb@31 | 21 | License along with this library; see the file LICENCE.txt |
paulb@31 | 22 | If not, write to the Free Software Foundation, Inc., |
paulb@31 | 23 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@31 | 24 | """ |
paulb@31 | 25 | |
paulb@31 | 26 | from simplified import * |
paulb@31 | 27 | import compiler |
paulb@31 | 28 | |
paulb@31 | 29 | # Fixing of name-related operations. |
paulb@31 | 30 | |
paulb@31 | 31 | class Fixer(Visitor): |
paulb@31 | 32 | |
paulb@31 | 33 | """ |
paulb@31 | 34 | The name fixer which traverses the program nodes, typically depth-first, |
paulb@31 | 35 | and maintains a record of name usage in the different namespaces. As a |
paulb@31 | 36 | consequence of various observations, some parts of the program node tree are |
paulb@31 | 37 | modified with different operations employed to those originally defined. |
paulb@31 | 38 | """ |
paulb@31 | 39 | |
paulb@31 | 40 | def __init__(self): |
paulb@31 | 41 | Visitor.__init__(self) |
paulb@31 | 42 | |
paulb@31 | 43 | # Satisfy visitor issues. |
paulb@31 | 44 | |
paulb@31 | 45 | self.visitor = self |
paulb@31 | 46 | |
paulb@31 | 47 | def process_all(self, visitor): |
paulb@31 | 48 | subprograms = [] |
paulb@31 | 49 | for subprogram in visitor.subprograms: |
paulb@31 | 50 | subprograms.append(self.process(subprogram)) |
paulb@31 | 51 | visitor.subprograms = subprograms |
paulb@31 | 52 | visitor.result = self.process(visitor.result) |
paulb@31 | 53 | return visitor |
paulb@31 | 54 | |
paulb@31 | 55 | def process(self, node): |
paulb@31 | 56 | |
paulb@31 | 57 | """ |
paulb@43 | 58 | Process a subprogram or module 'node', discovering from attributes on |
paulb@43 | 59 | 'node' any initial locals. Return a modified subprogram or module. |
paulb@31 | 60 | """ |
paulb@31 | 61 | |
paulb@31 | 62 | # Obtain a namespace either based on locals or on a structure. |
paulb@31 | 63 | |
paulb@34 | 64 | self.namespace = NameOrganiser(structure=getattr(node, "structure", None)) |
paulb@31 | 65 | |
paulb@43 | 66 | # NOTE: Check this. |
paulb@43 | 67 | |
paulb@43 | 68 | if hasattr(node, "params"): |
paulb@43 | 69 | for param, default in node.params: |
paulb@43 | 70 | self.namespace.store(param) |
paulb@43 | 71 | if hasattr(node, "star"): |
paulb@43 | 72 | param = node.star |
paulb@43 | 73 | self.namespace.store(param) |
paulb@43 | 74 | if hasattr(node, "dstar"): |
paulb@43 | 75 | param = node.dstar |
paulb@43 | 76 | self.namespace.store(param) |
paulb@43 | 77 | |
paulb@31 | 78 | # Add namespace details to any structure involved. |
paulb@31 | 79 | |
paulb@31 | 80 | if hasattr(node, "structure") and node.structure is not None: |
paulb@31 | 81 | |
paulb@31 | 82 | # Initialise bases where appropriate. |
paulb@31 | 83 | |
paulb@31 | 84 | if hasattr(node.structure, "bases"): |
paulb@31 | 85 | bases = [] |
paulb@31 | 86 | for base in node.structure.bases: |
paulb@31 | 87 | bases.append(self.dispatch(base)) |
paulb@31 | 88 | node.structure.bases = bases |
paulb@31 | 89 | |
paulb@31 | 90 | # Dispatch to the code itself. |
paulb@31 | 91 | |
paulb@31 | 92 | result = self.dispatch(node) |
paulb@31 | 93 | return result |
paulb@31 | 94 | |
paulb@31 | 95 | # Visitor methods. |
paulb@31 | 96 | |
paulb@31 | 97 | def default(self, node): |
paulb@31 | 98 | |
paulb@31 | 99 | """ |
paulb@31 | 100 | Process the given 'node', given that it does not have a specific |
paulb@31 | 101 | handler. |
paulb@31 | 102 | """ |
paulb@31 | 103 | |
paulb@31 | 104 | for attr in ("args",): |
paulb@31 | 105 | value = getattr(node, attr, None) |
paulb@31 | 106 | if value is not None: |
paulb@31 | 107 | setattr(node, attr, self.dispatches(value)) |
paulb@36 | 108 | for attr in ("expr", "lvalue", "test", "star", "dstar"): |
paulb@31 | 109 | value = getattr(node, attr, None) |
paulb@31 | 110 | if value is not None: |
paulb@31 | 111 | setattr(node, attr, self.dispatch(value)) |
paulb@36 | 112 | for attr in ("body", "else_", "handler", "finally_", "code", "choices"): |
paulb@31 | 113 | value = getattr(node, attr, None) |
paulb@31 | 114 | if value is not None: |
paulb@31 | 115 | setattr(node, attr, self.dispatches(value)) |
paulb@31 | 116 | return node |
paulb@31 | 117 | |
paulb@31 | 118 | def dispatch(self, node, *args): |
paulb@31 | 119 | return Visitor.dispatch(self, node, *args) |
paulb@31 | 120 | |
paulb@31 | 121 | def visitGlobal(self, global_): |
paulb@31 | 122 | for name in global_.names: |
paulb@31 | 123 | self.namespace.make_global(name) |
paulb@31 | 124 | return global_ |
paulb@31 | 125 | |
paulb@31 | 126 | def visitLoadName(self, loadname): |
paulb@43 | 127 | print "Name", loadname.name, "in", self.namespace |
paulb@31 | 128 | scope = self.namespace.find_for_load(loadname.name) |
paulb@31 | 129 | if scope == "structure": |
paulb@31 | 130 | result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.namespace.structure), name=loadname.name)) |
paulb@31 | 131 | elif scope == "global": |
paulb@31 | 132 | result = self.dispatch(LoadGlobal(name=loadname.name)) |
paulb@31 | 133 | else: |
paulb@31 | 134 | result = loadname |
paulb@31 | 135 | return result |
paulb@31 | 136 | |
paulb@31 | 137 | def visitStoreName(self, storename): |
paulb@31 | 138 | scope = self.namespace.find_for_store(storename.name) |
paulb@31 | 139 | if scope == "structure": |
paulb@31 | 140 | return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.namespace.structure), name=storename.name, expr=storename.expr)) |
paulb@31 | 141 | elif scope == "global": |
paulb@31 | 142 | return self.dispatch(StoreGlobal(name=storename.name, expr=storename.expr)) |
paulb@31 | 143 | else: |
paulb@31 | 144 | storename.expr = self.dispatch(storename.expr) |
paulb@31 | 145 | self.namespace.store(storename.name) |
paulb@31 | 146 | return storename |
paulb@31 | 147 | |
paulb@34 | 148 | class NameOrganiser: |
paulb@32 | 149 | |
paulb@32 | 150 | """ |
paulb@32 | 151 | A local namespace which may either relate to a genuine set of function |
paulb@32 | 152 | locals or the initialisation of a structure. |
paulb@32 | 153 | """ |
paulb@32 | 154 | |
paulb@32 | 155 | def __init__(self, structure=None): |
paulb@32 | 156 | self.structure = structure |
paulb@32 | 157 | if structure is not None: |
paulb@32 | 158 | self.local = "structure" |
paulb@32 | 159 | else: |
paulb@32 | 160 | self.local = "local" |
paulb@32 | 161 | self.names = {} |
paulb@32 | 162 | self.not_local = [] |
paulb@32 | 163 | |
paulb@32 | 164 | def make_global(self, name): |
paulb@32 | 165 | if name not in self.not_local: |
paulb@32 | 166 | self.not_local.append(name) |
paulb@32 | 167 | |
paulb@32 | 168 | def find_for_store(self, name): |
paulb@32 | 169 | if name not in self.not_local: |
paulb@32 | 170 | return self.local |
paulb@32 | 171 | else: |
paulb@32 | 172 | return "global" |
paulb@32 | 173 | |
paulb@32 | 174 | def find_for_load(self, name): |
paulb@32 | 175 | if name not in self.not_local and self.names.has_key(name): |
paulb@32 | 176 | return self.local |
paulb@32 | 177 | else: |
paulb@32 | 178 | return "global" |
paulb@32 | 179 | |
paulb@32 | 180 | def store(self, name, types=None): |
paulb@32 | 181 | if name not in self.not_local: |
paulb@32 | 182 | self.names[name] = types |
paulb@32 | 183 | else: |
paulb@32 | 184 | raise KeyError, name |
paulb@32 | 185 | |
paulb@32 | 186 | def load(self, name): |
paulb@32 | 187 | if name in self.not_local or not self.names.has_key(name): |
paulb@32 | 188 | raise KeyError, name |
paulb@32 | 189 | else: |
paulb@32 | 190 | return self.names[name] |
paulb@32 | 191 | |
paulb@43 | 192 | def __repr__(self): |
paulb@43 | 193 | return repr(self.names) |
paulb@43 | 194 | |
paulb@31 | 195 | # vim: tabstop=4 expandtab shiftwidth=4 |