1.1 --- a/annotate.py Sun Jul 23 23:57:13 2006 +0200
1.2 +++ b/annotate.py Mon Jul 24 00:21:01 2006 +0200
1.3 @@ -53,8 +53,9 @@
1.4 locals or the initialisation of a structure.
1.5 """
1.6
1.7 - def __init__(self, local_is_structure=0):
1.8 - if local_is_structure:
1.9 + def __init__(self, structure=None):
1.10 + self.structure = structure
1.11 + if structure is not None:
1.12 self.local = "structure"
1.13 else:
1.14 self.local = "local"
1.15 @@ -142,20 +143,23 @@
1.16 module. Note that this method may mutate nodes in the original program.
1.17 """
1.18
1.19 - if hasattr(node, "structure"):
1.20 - self.structure = node.structure; has_structure = 1
1.21 - else:
1.22 - self.structure = None; has_structure = 0
1.23 + # Obtain a namespace either based on locals or on a structure.
1.24
1.25 - self.namespace = Namespace(self.structure is not None)
1.26 + self.namespace = Namespace(structure=getattr(node, "structure", None))
1.27 if locals is not None:
1.28 self.namespace.merge(locals)
1.29
1.30 + # Determine the global namespace.
1.31 +
1.32 self.global_namespace = globals or self.namespace # NOTE: Improve this.
1.33 + node.namespace = self.namespace
1.34
1.35 - if has_structure:
1.36 + # Add namespace details to any structure involved.
1.37 +
1.38 + if hasattr(node, "structure") and node.structure is not None:
1.39 node.structure.namespace = self.namespace
1.40 - node.namespace = self.namespace
1.41 +
1.42 + # Satisfy visitor issues and then dispatch.
1.43
1.44 self.visitor = self
1.45 result = self.dispatch(node)
1.46 @@ -168,6 +172,8 @@
1.47
1.48 self.system.annotate(node, self.types)
1.49
1.50 + # Visitor methods.
1.51 +
1.52 def default(self, node):
1.53
1.54 """
1.55 @@ -175,11 +181,7 @@
1.56 handler.
1.57 """
1.58
1.59 - for attr in ("args",):
1.60 - value = getattr(node, attr, None)
1.61 - if value is not None:
1.62 - setattr(node, attr, self.dispatches(value))
1.63 - for attr in ("expr", "lvalue", "test", "handler", "star", "dstar"):
1.64 + for attr in ("expr", "lvalue", "test", "handler"):
1.65 value = getattr(node, attr, None)
1.66 if value is not None:
1.67 setattr(node, attr, self.dispatch(value))
1.68 @@ -205,7 +207,7 @@
1.69 def visitLoadName(self, loadname):
1.70 scope = self.namespace.find_for_load(loadname.name)
1.71 if scope == "structure":
1.72 - result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.structure), name=loadname.name))
1.73 + result = self.dispatch(LoadAttr(expr=LoadRef(ref=self.namespace.structure), name=loadname.name))
1.74 elif scope == "global":
1.75 result = self.dispatch(LoadGlobal(name=loadname.name))
1.76 else:
1.77 @@ -217,7 +219,7 @@
1.78 def visitStoreName(self, storename):
1.79 scope = self.namespace.find_for_store(storename.name)
1.80 if scope == "structure":
1.81 - return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.structure), name=storename.name, expr=storename.expr))
1.82 + return self.dispatch(StoreAttr(lvalue=LoadRef(ref=self.namespace.structure), name=storename.name, expr=storename.expr))
1.83 elif scope == "global":
1.84 return self.dispatch(StoreGlobal(name=storename.name, expr=storename.expr))
1.85 else:
1.86 @@ -276,6 +278,9 @@
1.87 def visitInvoke(self, invoke):
1.88 invoke.expr = self.dispatch(invoke.expr)
1.89 expr = self.types
1.90 +
1.91 + # NOTE: Consider initialiser invocation for classes.
1.92 +
1.93 types = []
1.94 args = []
1.95
1.96 @@ -306,10 +311,15 @@
1.97
1.98 for subprogram in expr:
1.99 items = self.make_items(invoke, subprogram)
1.100 - self.process(subprogram, self.make_namespace(items), self.global_namespace)
1.101 + annotator = Annotator()
1.102 + annotator.process(subprogram, self.make_namespace(items), self.global_namespace)
1.103 +
1.104 + # NOTE: Annotate the node with invocation details.
1.105
1.106 return invoke
1.107
1.108 + # Utility methods.
1.109 +
1.110 def make_items(self, invocation, subprogram):
1.111 # NOTE: Support star and dstar.
1.112 args = invocation.args