1.1 --- a/annotate.py Sat Sep 30 18:54:06 2006 +0200
1.2 +++ b/annotate.py Tue Oct 03 00:50:03 2006 +0200
1.3 @@ -24,18 +24,22 @@
1.4
1.5 --------
1.6
1.7 -To annotate a module, first obtain an Annotator:
1.8 +To use this module, the easiest approach is to use the annotate function:
1.9 +
1.10 +annotate(module, builtins)
1.11 +
1.12 +The more complicated approach involves obtaining an Annotator:
1.13
1.14 annotator = Annotator()
1.15
1.16 -Then, process the Simplifier object which holds the details of the module:
1.17 +Then, processing an existing module with it:
1.18
1.19 -annotator.process(simplifier)
1.20 +annotator.process(module)
1.21
1.22 If a module containing built-in classes and functions has already been
1.23 annotated, such a module should be passed in as an additional argument:
1.24
1.25 -annotator.process(simplifier, builtins_simplifier)
1.26 +annotator.process(module, builtins)
1.27 """
1.28
1.29 from simplified import *
1.30 @@ -95,11 +99,11 @@
1.31
1.32 self.visitor = self
1.33
1.34 - def process(self, visitor, builtins_visitor=None):
1.35 + def process(self, module, builtins=None):
1.36
1.37 """
1.38 - Process the resources of the given 'visitor', using the optional
1.39 - 'builtins_visitor' to access built-in classes and functions.
1.40 + Process the given 'module', using the optional 'builtins' to access
1.41 + built-in classes and functions.
1.42 """
1.43
1.44 self.subprograms = []
1.45 @@ -108,19 +112,19 @@
1.46
1.47 # Give constants their own namespace.
1.48
1.49 - for value, constant in visitor.constants.items():
1.50 + for value, constant in module.simplifier.constants.items():
1.51 constant.namespace = Namespace()
1.52
1.53 # Process the module, supplying builtins if possible.
1.54
1.55 self.global_namespace = Namespace()
1.56
1.57 - if builtins_visitor is not None:
1.58 - self.builtins_namespace = builtins_visitor.result.namespace
1.59 + if builtins is not None:
1.60 + self.builtins_namespace = builtins.namespace
1.61 else:
1.62 self.builtins_namespace = self.global_namespace
1.63
1.64 - return self.process_node(visitor.result)
1.65 + return self.process_node(module)
1.66
1.67 def process_node(self, node, locals=None):
1.68
1.69 @@ -265,14 +269,14 @@
1.70 storeattr.expr = self.dispatch(storeattr.expr)
1.71 expr = self.namespace.types
1.72 storeattr.lvalue = self.dispatch(storeattr.lvalue)
1.73 - accesses = {}
1.74 + writes = {}
1.75 for attr in self.namespace.types:
1.76 if attr is None:
1.77 print "Empty attribute storage attempt"
1.78 continue
1.79 attr.type.namespace.store(storeattr.name, expr)
1.80 - accesses[attr.type] = attr.type.namespace.load(storeattr.name)
1.81 - storeattr.accesses = accesses
1.82 + writes[attr.type] = attr.type.namespace.load(storeattr.name)
1.83 + storeattr.writes = writes
1.84 return storeattr
1.85
1.86 def visitConditional(self, conditional):
1.87 @@ -410,7 +414,7 @@
1.88
1.89 # Provide the correct namespace for the invocation.
1.90
1.91 - if getattr(invoke, "same_frame", 0):
1.92 + if isinstance(invoke, InvokeBlock):
1.93 namespace = Namespace()
1.94 namespace.merge_namespace(self.namespace)
1.95 else:
1.96 @@ -427,7 +431,7 @@
1.97 self.namespace.set_types(self.last_returns)
1.98 self.annotate(invoke)
1.99
1.100 - if getattr(invoke, "same_frame", 0):
1.101 + if isinstance(invoke, InvokeBlock):
1.102 for locals in self.returned_locals:
1.103 self.namespace.merge_namespace(locals)
1.104
1.105 @@ -700,4 +704,24 @@
1.106 results.append((attribute, accessor))
1.107 return results
1.108
1.109 +# Convenience functions.
1.110 +
1.111 +def annotate(module, builtins=None):
1.112 +
1.113 + """
1.114 + Annotate the given 'module', also employing the optional 'builtins' module,
1.115 + if specified.
1.116 + """
1.117 +
1.118 + annotator = Annotator()
1.119 + if builtins is not None:
1.120 + annotator.process(module, builtins)
1.121 + else:
1.122 + annotator.process(module)
1.123 +
1.124 +def annotate_all(modules, builtins):
1.125 + annotate(builtins)
1.126 + for module in modules:
1.127 + annotate(module, builtins)
1.128 +
1.129 # vim: tabstop=4 expandtab shiftwidth=4