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@20 | 28 | def __init__(self, local_is_structure=0, parent=None): |
paulb@20 | 29 | if local_is_structure: |
paulb@20 | 30 | self.local = "structure" |
paulb@20 | 31 | else: |
paulb@20 | 32 | self.local = "local" |
paulb@20 | 33 | self.parent = parent |
paulb@19 | 34 | self.names = {} |
paulb@19 | 35 | self.not_local = [] |
paulb@19 | 36 | |
paulb@19 | 37 | def make_global(self, name): |
paulb@19 | 38 | if name not in self.not_local: |
paulb@19 | 39 | self.not_local.append(name) |
paulb@19 | 40 | |
paulb@20 | 41 | def find(self, name): |
paulb@20 | 42 | if name not in self.not_local: |
paulb@20 | 43 | return self.local |
paulb@20 | 44 | else: |
paulb@20 | 45 | return "global" |
paulb@20 | 46 | |
paulb@19 | 47 | def store(self, name, types): |
paulb@19 | 48 | if name not in self.not_local: |
paulb@19 | 49 | self.names[name] = types |
paulb@19 | 50 | else: |
paulb@19 | 51 | self.parent.store(name, types) |
paulb@19 | 52 | |
paulb@19 | 53 | def load(self, name): |
paulb@19 | 54 | if name in self.not_local or not self.names.has_key(name): |
paulb@19 | 55 | return self.parent.load(name) |
paulb@19 | 56 | else: |
paulb@19 | 57 | return self.names[name] |
paulb@19 | 58 | |
paulb@19 | 59 | def merge(self, namespace): |
paulb@19 | 60 | for name, types in namespace.names.items(): |
paulb@19 | 61 | if not self.names.has_key(name): |
paulb@19 | 62 | self.names[name] = types |
paulb@19 | 63 | else: |
paulb@19 | 64 | existing = self.names[name] |
paulb@19 | 65 | for type in types: |
paulb@19 | 66 | if type not in existing: |
paulb@19 | 67 | existing.append(type) |
paulb@19 | 68 | |
paulb@19 | 69 | class System: |
paulb@19 | 70 | def __init__(self): |
paulb@19 | 71 | self.count = 0 |
paulb@19 | 72 | def init(self, node): |
paulb@19 | 73 | if not hasattr(node, "types"): |
paulb@19 | 74 | node.types = [] |
paulb@19 | 75 | def annotate(self, node, types): |
paulb@19 | 76 | for type in types: |
paulb@19 | 77 | if type not in node.types: |
paulb@19 | 78 | node.types.append(type) |
paulb@19 | 79 | self.count += 1 |
paulb@19 | 80 | |
paulb@20 | 81 | class Annotator(Visitor): |
paulb@19 | 82 | def __init__(self): |
paulb@19 | 83 | Visitor.__init__(self) |
paulb@19 | 84 | self.system = System() |
paulb@19 | 85 | self.types = None |
paulb@19 | 86 | self.temp = {} |
paulb@19 | 87 | |
paulb@20 | 88 | def process(self, node): |
paulb@19 | 89 | if hasattr(node, "structure"): |
paulb@20 | 90 | self.structure = node.structure |
paulb@20 | 91 | has_structure = 1 |
paulb@20 | 92 | else: |
paulb@20 | 93 | self.structure = None |
paulb@20 | 94 | has_structure = 0 |
paulb@20 | 95 | |
paulb@20 | 96 | self.namespace = Namespace(self.structure is not None) |
paulb@20 | 97 | if has_structure: |
paulb@19 | 98 | node.structure.namespace = self.namespace |
paulb@19 | 99 | |
paulb@20 | 100 | self.visitor = self |
paulb@20 | 101 | result = self.dispatch(node) |
paulb@20 | 102 | |
paulb@20 | 103 | return result |
paulb@20 | 104 | |
paulb@19 | 105 | def default(self, node): |
paulb@19 | 106 | for attr in ("args", "params"): |
paulb@19 | 107 | value = getattr(node, attr, None) |
paulb@19 | 108 | if value is not None: |
paulb@20 | 109 | setattr(node, attr, self.dispatches(value)) |
paulb@19 | 110 | for attr in ("expr", "lvalue", "test", "handler", "star", "dstar"): |
paulb@19 | 111 | value = getattr(node, attr, None) |
paulb@19 | 112 | if value is not None: |
paulb@20 | 113 | setattr(node, attr, self.dispatch(value)) |
paulb@19 | 114 | for attr in ("body", "else_", "finally_", "code"): |
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@20 | 118 | return node |
paulb@20 | 119 | |
paulb@20 | 120 | def dispatch(self, node, *args): |
paulb@20 | 121 | return Visitor.dispatch(self, node, *args) |
paulb@19 | 122 | |
paulb@19 | 123 | def visitGlobal(self, global_): |
paulb@19 | 124 | for name in global_.names: |
paulb@19 | 125 | self.make_global(name) |
paulb@20 | 126 | return global_ |
paulb@19 | 127 | |
paulb@19 | 128 | def visitLoadRef(self, loadref): |
paulb@19 | 129 | self.types = [loadref.ref] |
paulb@20 | 130 | return loadref |
paulb@19 | 131 | |
paulb@19 | 132 | def visitLoadName(self, loadname): |
paulb@20 | 133 | if self.namespace.find(loadname.name) == "structure": |
paulb@20 | 134 | return self.dispatch(LoadAttr(expr=LoadRef(ref=self.structure), name=loadname.name)) |
paulb@20 | 135 | else: |
paulb@20 | 136 | self.types = self.namespace.load(loadname, loadname.name) |
paulb@20 | 137 | return loadname |
paulb@19 | 138 | |
paulb@19 | 139 | def visitStoreName(self, storename): |
paulb@20 | 140 | if self.namespace.find(storename.name) == "structure": |
paulb@20 | 141 | return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.structure), name=storename.name, expr=storename.expr)) |
paulb@20 | 142 | else: |
paulb@20 | 143 | self.namespace.store(storename, storename.name, self.types) |
paulb@20 | 144 | return storename |
paulb@19 | 145 | |
paulb@19 | 146 | def visitLoadTemp(self, loadtemp): |
paulb@19 | 147 | index = getattr(loadtemp, "index", None) |
paulb@19 | 148 | self.types = self.temp[index] |
paulb@20 | 149 | return loadtemp |
paulb@19 | 150 | |
paulb@19 | 151 | def visitStoreTemp(self, storetemp): |
paulb@19 | 152 | index = getattr(storetemp, "index", None) |
paulb@19 | 153 | self.temp[index] = self.types |
paulb@20 | 154 | return storetemp |
paulb@19 | 155 | |
paulb@19 | 156 | def visitReleaseTemp(self, releasetemp): |
paulb@19 | 157 | index = getattr(releasetemp, "index", None) |
paulb@19 | 158 | del self.temp[index] |
paulb@20 | 159 | return releasetemp |
paulb@20 | 160 | |
paulb@20 | 161 | def visitLoadAttr(self, loadattr): |
paulb@20 | 162 | self.dispatch(loadattr.expr) |
paulb@20 | 163 | types = [] |
paulb@20 | 164 | for ref in self.types: |
paulb@20 | 165 | types += ref.namespace.load(loadattr.name) |
paulb@20 | 166 | self.types = types |
paulb@20 | 167 | return loadattr |
paulb@20 | 168 | |
paulb@20 | 169 | def visitStoreAttr(self, storeattr): |
paulb@20 | 170 | self.dispatch(storeattr.expr) |
paulb@20 | 171 | expr = self.types |
paulb@20 | 172 | self.dispatch(storeattr.lvalue) |
paulb@20 | 173 | for ref in self.types: |
paulb@20 | 174 | ref.namespace.store(storeattr.name, expr) |
paulb@20 | 175 | return storeattr |
paulb@19 | 176 | |
paulb@19 | 177 | # vim: tabstop=4 expandtab shiftwidth=4 |