1.1 --- a/TO_DO.txt Sat Jul 10 23:23:29 2010 +0200
1.2 +++ b/TO_DO.txt Sun Jul 11 01:17:11 2010 +0200
1.3 @@ -17,6 +17,8 @@
1.4 set # could be confused by the local definition at run-time
1.5 ----
1.6
1.7 +Support operator module usage instead of explicit operator code generation.
1.8 +
1.9 Support __init__ traversal (and other implicit names) more effectively.
1.10
1.11 Check context_value initialisation (avoiding or handling None effectively).
2.1 --- a/micropython/__init__.py Sat Jul 10 23:23:29 2010 +0200
2.2 +++ b/micropython/__init__.py Sun Jul 11 01:17:11 2010 +0200
2.3 @@ -511,6 +511,13 @@
2.4 self._collect_attributes("__main__", objtable)
2.5
2.6 def add_attribute_to_visit(self, objname, attrname):
2.7 +
2.8 + """
2.9 + Queue an attribute of the object with the given 'objname', having the
2.10 + given 'attrname', to the list for potential visiting if the specified
2.11 + object is actually referenced.
2.12 + """
2.13 +
2.14 if not self.attributes_to_visit.has_key(objname):
2.15 self.attributes_to_visit[objname] = set()
2.16 self.attributes_to_visit[objname].add(attrname)
3.1 --- a/micropython/data.py Sat Jul 10 23:23:29 2010 +0200
3.2 +++ b/micropython/data.py Sun Jul 11 01:17:11 2010 +0200
3.3 @@ -61,6 +61,16 @@
3.4 else:
3.5 return obj.__shortrepr__()
3.6
3.7 +lambda_index = 0
3.8 +
3.9 +def new_lambda():
3.10 +
3.11 + "Return a new sequence number for a lambda definition."
3.12 +
3.13 + global lambda_index
3.14 + lambda_index += 1
3.15 + return lambda_index
3.16 +
3.17 # Mix-ins and abstract classes.
3.18
3.19 class Naming:
3.20 @@ -81,6 +91,7 @@
3.21 self.module = module
3.22 self.namespace = {}
3.23 self.globals = set()
3.24 + self.lambdas = {} # only really useful for functions
3.25 self.finalised = 0
3.26
3.27 # Attributes accessed on objects, potentially narrowing their types.
3.28 @@ -126,6 +137,19 @@
3.29 def get(self, name, default=None):
3.30 return self.namespace.get(name, default)
3.31
3.32 + # Administrative methods.
3.33 +
3.34 + def items_for_vacuum(self):
3.35 + return self.items() + self.lambdas.items()
3.36 +
3.37 + def vacuum_item(self, name):
3.38 + del self[name]
3.39 +
3.40 + def add_lambda(self, obj):
3.41 + attr = Attr(None, self, obj.name)
3.42 + attr.update([self.get_context_and_value(obj)], single_assignment=1)
3.43 + self.lambdas[obj.name] = attr
3.44 +
3.45 # Specialised access methods.
3.46
3.47 def get_using_node(self, name, node):
3.48 @@ -963,6 +987,8 @@
3.49
3.50 return NamespaceDict.get_updated_context_values(self, results)
3.51
3.52 + # Administrative methods.
3.53 +
3.54 def finalise_attributes(self):
3.55
3.56 "Make sure that all attributes are fully defined."
3.57 @@ -1238,11 +1264,20 @@
3.58 Initialise the function with the given 'name', 'parent', list of
3.59 'argnames', list of 'defaults', the 'has_star' flag (indicating the
3.60 presence of a * parameter), the 'has_dstar' flag (indicating the
3.61 - presence of a ** parameter), optional 'module', and optional AST 'node'.
3.62 + presence of a ** parameter), optional 'dynamic_def' (indicating that the
3.63 + function must be handled dynamically), optional 'module', and optional
3.64 + AST 'node'.
3.65 """
3.66
3.67 NamespaceDict.__init__(self, module)
3.68 - self.name = name
3.69 +
3.70 + if name is None:
3.71 + self.name = "lambda#%d" % new_lambda()
3.72 + self._is_lambda = 1
3.73 + else:
3.74 + self.name = name
3.75 + self._is_lambda = 0
3.76 +
3.77 self.parent = parent
3.78 self.argnames = argnames
3.79 self.defaults = defaults
3.80 @@ -1331,6 +1366,9 @@
3.81 def get_body_block(self):
3.82 return self.body_block
3.83
3.84 + def is_lambda(self):
3.85 + return self._is_lambda
3.86 +
3.87 # Defaults-related methods.
3.88
3.89 def store_default(self, attr_or_value):
3.90 @@ -1431,6 +1469,14 @@
3.91 return 1
3.92 return 0
3.93
3.94 + # Administrative methods.
3.95 +
3.96 + def items_for_vacuum(self):
3.97 + return self.lambdas.items()
3.98 +
3.99 + def vacuum_item(self, name):
3.100 + del self.lambdas[name]
3.101 +
3.102 def finalise_attributes(self):
3.103
3.104 """
4.1 --- a/micropython/inspect.py Sat Jul 10 23:23:29 2010 +0200
4.2 +++ b/micropython/inspect.py Sun Jul 11 01:17:11 2010 +0200
4.3 @@ -192,20 +192,19 @@
4.4 all_objects = list(self.all_objects)
4.5
4.6 for obj in all_objects:
4.7 - if isinstance(obj, Class):
4.8 - self.vacuum_object(obj)
4.9 + self.vacuum_object(obj)
4.10
4.11 def vacuum_object(self, obj, delete_all=0):
4.12
4.13 "Vacuum the given object 'obj'."
4.14
4.15 - for name, attr in obj.items():
4.16 + for name, attr in obj.items_for_vacuum():
4.17
4.18 # Only consider deleting entire unused objects or things accessible
4.19 # via names which are never used.
4.20
4.21 if delete_all or not self.importer.uses_attribute(obj.full_name(), name):
4.22 - del obj[name]
4.23 + obj.vacuum_item(name)
4.24
4.25 # Delete any unambiguous attribute value. Such values can only
4.26 # have been defined within the object and therefore are not
4.27 @@ -217,10 +216,9 @@
4.28 if value is not obj and value in self.all_objects:
4.29 self.all_objects.remove(value)
4.30
4.31 - # Delete class contents.
4.32 + # Delete class contents and lambdas from functions.
4.33
4.34 - if isinstance(value, Class):
4.35 - self.vacuum_object(value, 1)
4.36 + self.vacuum_object(value, 1)
4.37
4.38 def unfinalise(self):
4.39
4.40 @@ -286,6 +284,7 @@
4.41 "Store a lambda function 'obj'."
4.42
4.43 self.add_object(obj)
4.44 + self.get_namespace().add_lambda(obj)
4.45
4.46 def store_module_attr(self, name, module):
4.47
4.48 @@ -382,7 +381,8 @@
4.49
4.50 """
4.51 Note usage on the object having the given 'objname' of the attribute
4.52 - 'attrname'.
4.53 + 'attrname'. If 'objname' is None, the current namespace is chosen as the
4.54 + object providing the attribute.
4.55 """
4.56
4.57 return self.get_namespace()._use_specific_attribute(objname, attrname)
4.58 @@ -881,8 +881,9 @@
4.59 return None
4.60
4.61 def visitLambda(self, node):
4.62 - self.use_name(None) # lambda functions have no names but are assumed to be invoked
4.63 - return self._visitFunction(node, None)
4.64 + fn = self._visitFunction(node, None)
4.65 + self.use_specific_attribute(None, fn.name)
4.66 + return fn
4.67
4.68 visitLeftShift = _visitBinary
4.69
5.1 --- a/micropython/trans.py Sat Jul 10 23:23:29 2010 +0200
5.2 +++ b/micropython/trans.py Sun Jul 11 01:17:11 2010 +0200
5.3 @@ -1097,7 +1097,7 @@
5.4 # Return None for normal functions without explicit return
5.5 # statements.
5.6
5.7 - if fn.name is not None:
5.8 + if not fn.is_lambda():
5.9 self.dispatch(compiler.ast.Name("None"))
5.10
5.11 self.new_op(StoreResult())
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/tests/operator_add.py Sun Jul 11 01:17:11 2010 +0200
6.3 @@ -0,0 +1,13 @@
6.4 +#!/usr/bin/env python
6.5 +
6.6 +import operator
6.7 +
6.8 +a = 1
6.9 +b = 2
6.10 +c = operator.add(a, b)
6.11 +d = operator.add(operator.add(a, b), c)
6.12 +
6.13 +result_3 = c
6.14 +result_6 = d
6.15 +
6.16 +# vim: tabstop=4 expandtab shiftwidth=4