paulb@19 | 1 | #!/usr/bin/env python |
paulb@19 | 2 | |
paulb@19 | 3 | """ |
paulb@19 | 4 | Traverse and annotate simplified AST structures. |
paulb@19 | 5 | |
paulb@19 | 6 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@19 | 7 | |
paulb@19 | 8 | This software is free software; you can redistribute it and/or |
paulb@19 | 9 | modify it under the terms of the GNU General Public License as |
paulb@19 | 10 | published by the Free Software Foundation; either version 2 of |
paulb@19 | 11 | the License, or (at your option) any later version. |
paulb@19 | 12 | |
paulb@19 | 13 | This software is distributed in the hope that it will be useful, |
paulb@19 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@19 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@19 | 16 | GNU General Public License for more details. |
paulb@19 | 17 | |
paulb@19 | 18 | You should have received a copy of the GNU General Public |
paulb@19 | 19 | License along with this library; see the file LICENCE.txt |
paulb@19 | 20 | If not, write to the Free Software Foundation, Inc., |
paulb@19 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@19 | 22 | """ |
paulb@19 | 23 | |
paulb@19 | 24 | from simplified import * |
paulb@19 | 25 | import compiler |
paulb@19 | 26 | |
paulb@19 | 27 | class Namespace: |
paulb@21 | 28 | def __init__(self, local_is_structure=0): |
paulb@20 | 29 | if local_is_structure: |
paulb@20 | 30 | self.local = "structure" |
paulb@20 | 31 | else: |
paulb@20 | 32 | self.local = "local" |
paulb@19 | 33 | self.names = {} |
paulb@19 | 34 | self.not_local = [] |
paulb@19 | 35 | |
paulb@19 | 36 | def make_global(self, name): |
paulb@19 | 37 | if name not in self.not_local: |
paulb@19 | 38 | self.not_local.append(name) |
paulb@19 | 39 | |
paulb@21 | 40 | def find_for_store(self, name): |
paulb@20 | 41 | if name not in self.not_local: |
paulb@20 | 42 | return self.local |
paulb@20 | 43 | else: |
paulb@20 | 44 | return "global" |
paulb@20 | 45 | |
paulb@21 | 46 | def find_for_load(self, name): |
paulb@21 | 47 | if name not in self.not_local and self.names.has_key(name): |
paulb@21 | 48 | return self.local |
paulb@21 | 49 | else: |
paulb@21 | 50 | return "global" |
paulb@21 | 51 | |
paulb@19 | 52 | def store(self, name, types): |
paulb@19 | 53 | if name not in self.not_local: |
paulb@19 | 54 | self.names[name] = types |
paulb@19 | 55 | else: |
paulb@21 | 56 | raise KeyError, name |
paulb@19 | 57 | |
paulb@19 | 58 | def load(self, name): |
paulb@19 | 59 | if name in self.not_local or not self.names.has_key(name): |
paulb@21 | 60 | raise KeyError, name |
paulb@19 | 61 | else: |
paulb@19 | 62 | return self.names[name] |
paulb@19 | 63 | |
paulb@19 | 64 | def merge(self, namespace): |
paulb@19 | 65 | for name, types in namespace.names.items(): |
paulb@19 | 66 | if not self.names.has_key(name): |
paulb@19 | 67 | self.names[name] = types |
paulb@19 | 68 | else: |
paulb@19 | 69 | existing = self.names[name] |
paulb@19 | 70 | for type in types: |
paulb@19 | 71 | if type not in existing: |
paulb@19 | 72 | existing.append(type) |
paulb@19 | 73 | |
paulb@19 | 74 | class System: |
paulb@19 | 75 | def __init__(self): |
paulb@19 | 76 | self.count = 0 |
paulb@19 | 77 | def init(self, node): |
paulb@19 | 78 | if not hasattr(node, "types"): |
paulb@19 | 79 | node.types = [] |
paulb@19 | 80 | def annotate(self, node, types): |
paulb@19 | 81 | for type in types: |
paulb@19 | 82 | if type not in node.types: |
paulb@19 | 83 | node.types.append(type) |
paulb@19 | 84 | self.count += 1 |
paulb@19 | 85 | |
paulb@20 | 86 | class Annotator(Visitor): |
paulb@19 | 87 | def __init__(self): |
paulb@19 | 88 | Visitor.__init__(self) |
paulb@19 | 89 | self.system = System() |
paulb@19 | 90 | self.types = None |
paulb@19 | 91 | self.temp = {} |
paulb@19 | 92 | |
paulb@21 | 93 | def process(self, node, global_namespace=None): |
paulb@19 | 94 | if hasattr(node, "structure"): |
paulb@20 | 95 | self.structure = node.structure |
paulb@20 | 96 | has_structure = 1 |
paulb@20 | 97 | else: |
paulb@20 | 98 | self.structure = None |
paulb@20 | 99 | has_structure = 0 |
paulb@20 | 100 | |
paulb@20 | 101 | self.namespace = Namespace(self.structure is not None) |
paulb@21 | 102 | self.global_namespace = global_namespace or self.namespace # NOTE: Improve this. |
paulb@21 | 103 | |
paulb@20 | 104 | if has_structure: |
paulb@19 | 105 | node.structure.namespace = self.namespace |
paulb@21 | 106 | node.namespace = self.namespace |
paulb@19 | 107 | |
paulb@20 | 108 | self.visitor = self |
paulb@20 | 109 | result = self.dispatch(node) |
paulb@20 | 110 | |
paulb@20 | 111 | return result |
paulb@20 | 112 | |
paulb@19 | 113 | def default(self, node): |
paulb@21 | 114 | for attr in ("args",): |
paulb@19 | 115 | value = getattr(node, attr, None) |
paulb@19 | 116 | if value is not None: |
paulb@20 | 117 | setattr(node, attr, self.dispatches(value)) |
paulb@21 | 118 | |
paulb@21 | 119 | # NOTE: This will eventually use both defaults and supplied arguments. |
paulb@21 | 120 | |
paulb@21 | 121 | for attr in ("params",): |
paulb@21 | 122 | value = getattr(node, attr, None) |
paulb@21 | 123 | if value is not None: |
paulb@21 | 124 | params = [] |
paulb@21 | 125 | for name, default in value: |
paulb@21 | 126 | if default is not None: |
paulb@21 | 127 | n = self.dispatch(default) |
paulb@21 | 128 | self.namespace.store(name, self.types) |
paulb@21 | 129 | else: |
paulb@21 | 130 | n = None |
paulb@21 | 131 | self.namespace.store(name, []) |
paulb@21 | 132 | params.append((name, n)) |
paulb@21 | 133 | setattr(node, attr, params) |
paulb@19 | 134 | for attr in ("expr", "lvalue", "test", "handler", "star", "dstar"): |
paulb@19 | 135 | value = getattr(node, attr, None) |
paulb@19 | 136 | if value is not None: |
paulb@20 | 137 | setattr(node, attr, self.dispatch(value)) |
paulb@19 | 138 | for attr in ("body", "else_", "finally_", "code"): |
paulb@19 | 139 | value = getattr(node, attr, None) |
paulb@19 | 140 | if value is not None: |
paulb@20 | 141 | setattr(node, attr, self.dispatches(value)) |
paulb@20 | 142 | return node |
paulb@20 | 143 | |
paulb@20 | 144 | def dispatch(self, node, *args): |
paulb@20 | 145 | return Visitor.dispatch(self, node, *args) |
paulb@19 | 146 | |
paulb@19 | 147 | def visitGlobal(self, global_): |
paulb@19 | 148 | for name in global_.names: |
paulb@21 | 149 | self.namespace.make_global(name) |
paulb@20 | 150 | return global_ |
paulb@19 | 151 | |
paulb@19 | 152 | def visitLoadRef(self, loadref): |
paulb@19 | 153 | self.types = [loadref.ref] |
paulb@20 | 154 | return loadref |
paulb@19 | 155 | |
paulb@19 | 156 | def visitLoadName(self, loadname): |
paulb@21 | 157 | scope = self.namespace.find_for_load(loadname.name) |
paulb@21 | 158 | print "LoadName", scope |
paulb@21 | 159 | if scope == "structure": |
paulb@20 | 160 | return self.dispatch(LoadAttr(expr=LoadRef(ref=self.structure), name=loadname.name)) |
paulb@21 | 161 | elif scope == "global": |
paulb@21 | 162 | return self.dispatch(LoadGlobal(name=loadname.name)) |
paulb@20 | 163 | else: |
paulb@21 | 164 | self.types = self.namespace.load(loadname.name) |
paulb@20 | 165 | return loadname |
paulb@19 | 166 | |
paulb@19 | 167 | def visitStoreName(self, storename): |
paulb@21 | 168 | scope = self.namespace.find_for_store(storename.name) |
paulb@21 | 169 | print "StoreName", scope |
paulb@21 | 170 | if scope == "structure": |
paulb@20 | 171 | return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.structure), name=storename.name, expr=storename.expr)) |
paulb@21 | 172 | elif scope == "global": |
paulb@21 | 173 | return self.dispatch(StoreGlobal(name=storename.name, expr=storename.expr)) |
paulb@20 | 174 | else: |
paulb@21 | 175 | storename.expr = self.dispatch(storename.expr) |
paulb@21 | 176 | self.namespace.store(storename.name, self.types) |
paulb@20 | 177 | return storename |
paulb@19 | 178 | |
paulb@21 | 179 | def visitLoadGlobal(self, loadglobal): |
paulb@21 | 180 | self.types = self.global_namespace.load(loadglobal.name) |
paulb@21 | 181 | return loadglobal |
paulb@21 | 182 | |
paulb@21 | 183 | def visitStoreGlobal(self, storeglobal): |
paulb@21 | 184 | storeglobal.expr = self.dispatch(storeglobal.expr) |
paulb@21 | 185 | |
paulb@21 | 186 | # NOTE: This may always be a merge operation. |
paulb@21 | 187 | |
paulb@21 | 188 | self.global_namespace.store(storeglobal.name, self.types) |
paulb@21 | 189 | return storeglobal |
paulb@21 | 190 | |
paulb@19 | 191 | def visitLoadTemp(self, loadtemp): |
paulb@19 | 192 | index = getattr(loadtemp, "index", None) |
paulb@19 | 193 | self.types = self.temp[index] |
paulb@20 | 194 | return loadtemp |
paulb@19 | 195 | |
paulb@19 | 196 | def visitStoreTemp(self, storetemp): |
paulb@21 | 197 | storetemp.expr = self.dispatch(storetemp.expr) |
paulb@19 | 198 | index = getattr(storetemp, "index", None) |
paulb@19 | 199 | self.temp[index] = self.types |
paulb@20 | 200 | return storetemp |
paulb@19 | 201 | |
paulb@19 | 202 | def visitReleaseTemp(self, releasetemp): |
paulb@19 | 203 | index = getattr(releasetemp, "index", None) |
paulb@19 | 204 | del self.temp[index] |
paulb@20 | 205 | return releasetemp |
paulb@20 | 206 | |
paulb@20 | 207 | def visitLoadAttr(self, loadattr): |
paulb@21 | 208 | loadattr.expr = self.dispatch(loadattr.expr) |
paulb@20 | 209 | types = [] |
paulb@20 | 210 | for ref in self.types: |
paulb@20 | 211 | types += ref.namespace.load(loadattr.name) |
paulb@20 | 212 | self.types = types |
paulb@20 | 213 | return loadattr |
paulb@20 | 214 | |
paulb@20 | 215 | def visitStoreAttr(self, storeattr): |
paulb@21 | 216 | storeattr.expr = self.dispatch(storeattr.expr) |
paulb@20 | 217 | expr = self.types |
paulb@21 | 218 | storeattr.lvalue = self.dispatch(storeattr.lvalue) |
paulb@20 | 219 | for ref in self.types: |
paulb@20 | 220 | ref.namespace.store(storeattr.name, expr) |
paulb@20 | 221 | return storeattr |
paulb@19 | 222 | |
paulb@19 | 223 | # vim: tabstop=4 expandtab shiftwidth=4 |