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@19 | 28 | def __init__(self, parent=None): |
paulb@19 | 29 | self.names = {} |
paulb@19 | 30 | self.not_local = [] |
paulb@19 | 31 | self.parent = parent |
paulb@19 | 32 | |
paulb@19 | 33 | def make_global(self, name): |
paulb@19 | 34 | if name not in self.not_local: |
paulb@19 | 35 | self.not_local.append(name) |
paulb@19 | 36 | |
paulb@19 | 37 | def store(self, name, types): |
paulb@19 | 38 | if name not in self.not_local: |
paulb@19 | 39 | self.names[name] = types |
paulb@19 | 40 | else: |
paulb@19 | 41 | self.parent.store(name, types) |
paulb@19 | 42 | |
paulb@19 | 43 | def load(self, name): |
paulb@19 | 44 | if name in self.not_local or not self.names.has_key(name): |
paulb@19 | 45 | return self.parent.load(name) |
paulb@19 | 46 | else: |
paulb@19 | 47 | return self.names[name] |
paulb@19 | 48 | |
paulb@19 | 49 | def merge(self, namespace): |
paulb@19 | 50 | for name, types in namespace.names.items(): |
paulb@19 | 51 | if not self.names.has_key(name): |
paulb@19 | 52 | self.names[name] = types |
paulb@19 | 53 | else: |
paulb@19 | 54 | existing = self.names[name] |
paulb@19 | 55 | for type in types: |
paulb@19 | 56 | if type not in existing: |
paulb@19 | 57 | existing.append(type) |
paulb@19 | 58 | |
paulb@19 | 59 | class System: |
paulb@19 | 60 | def __init__(self): |
paulb@19 | 61 | self.count = 0 |
paulb@19 | 62 | def init(self, node): |
paulb@19 | 63 | if not hasattr(node, "types"): |
paulb@19 | 64 | node.types = [] |
paulb@19 | 65 | def annotate(self, node, types): |
paulb@19 | 66 | for type in types: |
paulb@19 | 67 | if type not in node.types: |
paulb@19 | 68 | node.types.append(type) |
paulb@19 | 69 | self.count += 1 |
paulb@19 | 70 | |
paulb@19 | 71 | class Traverser(Visitor): |
paulb@19 | 72 | def __init__(self): |
paulb@19 | 73 | Visitor.__init__(self) |
paulb@19 | 74 | self.system = System() |
paulb@19 | 75 | self.types = None |
paulb@19 | 76 | self.temp = {} |
paulb@19 | 77 | |
paulb@19 | 78 | def traverse(self, node): |
paulb@19 | 79 | self.namespace = Namespace() |
paulb@19 | 80 | compiler.walk(node, self, self) |
paulb@19 | 81 | if hasattr(node, "structure"): |
paulb@19 | 82 | node.structure.namespace = self.namespace |
paulb@19 | 83 | |
paulb@19 | 84 | def default(self, node): |
paulb@19 | 85 | for attr in ("args", "params"): |
paulb@19 | 86 | value = getattr(node, attr, None) |
paulb@19 | 87 | if value is not None: |
paulb@19 | 88 | self.dispatches(value) |
paulb@19 | 89 | for attr in ("expr", "lvalue", "test", "handler", "star", "dstar"): |
paulb@19 | 90 | value = getattr(node, attr, None) |
paulb@19 | 91 | if value is not None: |
paulb@19 | 92 | self.dispatch(value) |
paulb@19 | 93 | for attr in ("body", "else_", "finally_", "code"): |
paulb@19 | 94 | value = getattr(node, attr, None) |
paulb@19 | 95 | if value is not None: |
paulb@19 | 96 | for n in value: |
paulb@19 | 97 | self.dispatch(n) |
paulb@19 | 98 | print node |
paulb@19 | 99 | |
paulb@19 | 100 | def visitGlobal(self, global_): |
paulb@19 | 101 | for name in global_.names: |
paulb@19 | 102 | self.make_global(name) |
paulb@19 | 103 | |
paulb@19 | 104 | def visitLoadRef(self, loadref): |
paulb@19 | 105 | self.types = [loadref.ref] |
paulb@19 | 106 | |
paulb@19 | 107 | def visitLoadName(self, loadname): |
paulb@19 | 108 | self.types = self.namespace.load(loadname.name) |
paulb@19 | 109 | |
paulb@19 | 110 | def visitStoreName(self, storename): |
paulb@19 | 111 | self.namespace.store(storename.name, self.types) |
paulb@19 | 112 | |
paulb@19 | 113 | def visitLoadTemp(self, loadtemp): |
paulb@19 | 114 | index = getattr(loadtemp, "index", None) |
paulb@19 | 115 | self.types = self.temp[index] |
paulb@19 | 116 | |
paulb@19 | 117 | def visitStoreTemp(self, storetemp): |
paulb@19 | 118 | index = getattr(storetemp, "index", None) |
paulb@19 | 119 | self.temp[index] = self.types |
paulb@19 | 120 | |
paulb@19 | 121 | def visitReleaseTemp(self, releasetemp): |
paulb@19 | 122 | index = getattr(releasetemp, "index", None) |
paulb@19 | 123 | del self.temp[index] |
paulb@19 | 124 | |
paulb@19 | 125 | # vim: tabstop=4 expandtab shiftwidth=4 |