# HG changeset patch # User paulb@jeremy # Date 1153693261 -7200 # Node ID be8dd5b882e40cec8c249fb5442d1a45ecc2cc81 # Parent c9ce39c2076a8ab327aacaf20b439e112e2e75a8 Changed namespace initialisation and structure storage/retrieval. Employed a new annotator for the processing of each invocation. diff -r c9ce39c2076a -r be8dd5b882e4 annotate.py --- a/annotate.py Sun Jul 23 23:57:13 2006 +0200 +++ b/annotate.py Mon Jul 24 00:21:01 2006 +0200 @@ -53,8 +53,9 @@ locals or the initialisation of a structure. """ - def __init__(self, local_is_structure=0): - if local_is_structure: + def __init__(self, structure=None): + self.structure = structure + if structure is not None: self.local = "structure" else: self.local = "local" @@ -142,20 +143,23 @@ module. Note that this method may mutate nodes in the original program. """ - if hasattr(node, "structure"): - self.structure = node.structure; has_structure = 1 - else: - self.structure = None; has_structure = 0 + # Obtain a namespace either based on locals or on a structure. - self.namespace = Namespace(self.structure is not None) + self.namespace = Namespace(structure=getattr(node, "structure", None)) if locals is not None: self.namespace.merge(locals) + # Determine the global namespace. + self.global_namespace = globals or self.namespace # NOTE: Improve this. + node.namespace = self.namespace - if has_structure: + # Add namespace details to any structure involved. + + if hasattr(node, "structure") and node.structure is not None: node.structure.namespace = self.namespace - node.namespace = self.namespace + + # Satisfy visitor issues and then dispatch. self.visitor = self result = self.dispatch(node) @@ -168,6 +172,8 @@ self.system.annotate(node, self.types) + # Visitor methods. + def default(self, node): """ @@ -175,11 +181,7 @@ handler. """ - for attr in ("args",): - value = getattr(node, attr, None) - if value is not None: - setattr(node, attr, self.dispatches(value)) - for attr in ("expr", "lvalue", "test", "handler", "star", "dstar"): + for attr in ("expr", "lvalue", "test", "handler"): value = getattr(node, attr, None) if value is not None: setattr(node, attr, self.dispatch(value)) @@ -205,7 +207,7 @@ def visitLoadName(self, loadname): scope = self.namespace.find_for_load(loadname.name) if scope == "structure": - result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.structure), name=loadname.name)) + result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.namespace.structure), name=loadname.name)) elif scope == "global": result = self.dispatch(LoadGlobal(name=loadname.name)) else: @@ -217,7 +219,7 @@ def visitStoreName(self, storename): scope = self.namespace.find_for_store(storename.name) if scope == "structure": - return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.structure), name=storename.name, expr=storename.expr)) + return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.namespace.structure), name=storename.name, expr=storename.expr)) elif scope == "global": return self.dispatch(StoreGlobal(name=storename.name, expr=storename.expr)) else: @@ -276,6 +278,9 @@ def visitInvoke(self, invoke): invoke.expr = self.dispatch(invoke.expr) expr = self.types + + # NOTE: Consider initialiser invocation for classes. + types = [] args = [] @@ -306,10 +311,15 @@ for subprogram in expr: items = self.make_items(invoke, subprogram) - self.process(subprogram, self.make_namespace(items), self.global_namespace) + annotator = Annotator() + annotator.process(subprogram, self.make_namespace(items), self.global_namespace) + + # NOTE: Annotate the node with invocation details. return invoke + # Utility methods. + def make_items(self, invocation, subprogram): # NOTE: Support star and dstar. args = invocation.args