1.1 --- a/micropython/data.py Sat Jul 10 23:23:29 2010 +0200
1.2 +++ b/micropython/data.py Sun Jul 11 01:17:11 2010 +0200
1.3 @@ -61,6 +61,16 @@
1.4 else:
1.5 return obj.__shortrepr__()
1.6
1.7 +lambda_index = 0
1.8 +
1.9 +def new_lambda():
1.10 +
1.11 + "Return a new sequence number for a lambda definition."
1.12 +
1.13 + global lambda_index
1.14 + lambda_index += 1
1.15 + return lambda_index
1.16 +
1.17 # Mix-ins and abstract classes.
1.18
1.19 class Naming:
1.20 @@ -81,6 +91,7 @@
1.21 self.module = module
1.22 self.namespace = {}
1.23 self.globals = set()
1.24 + self.lambdas = {} # only really useful for functions
1.25 self.finalised = 0
1.26
1.27 # Attributes accessed on objects, potentially narrowing their types.
1.28 @@ -126,6 +137,19 @@
1.29 def get(self, name, default=None):
1.30 return self.namespace.get(name, default)
1.31
1.32 + # Administrative methods.
1.33 +
1.34 + def items_for_vacuum(self):
1.35 + return self.items() + self.lambdas.items()
1.36 +
1.37 + def vacuum_item(self, name):
1.38 + del self[name]
1.39 +
1.40 + def add_lambda(self, obj):
1.41 + attr = Attr(None, self, obj.name)
1.42 + attr.update([self.get_context_and_value(obj)], single_assignment=1)
1.43 + self.lambdas[obj.name] = attr
1.44 +
1.45 # Specialised access methods.
1.46
1.47 def get_using_node(self, name, node):
1.48 @@ -963,6 +987,8 @@
1.49
1.50 return NamespaceDict.get_updated_context_values(self, results)
1.51
1.52 + # Administrative methods.
1.53 +
1.54 def finalise_attributes(self):
1.55
1.56 "Make sure that all attributes are fully defined."
1.57 @@ -1238,11 +1264,20 @@
1.58 Initialise the function with the given 'name', 'parent', list of
1.59 'argnames', list of 'defaults', the 'has_star' flag (indicating the
1.60 presence of a * parameter), the 'has_dstar' flag (indicating the
1.61 - presence of a ** parameter), optional 'module', and optional AST 'node'.
1.62 + presence of a ** parameter), optional 'dynamic_def' (indicating that the
1.63 + function must be handled dynamically), optional 'module', and optional
1.64 + AST 'node'.
1.65 """
1.66
1.67 NamespaceDict.__init__(self, module)
1.68 - self.name = name
1.69 +
1.70 + if name is None:
1.71 + self.name = "lambda#%d" % new_lambda()
1.72 + self._is_lambda = 1
1.73 + else:
1.74 + self.name = name
1.75 + self._is_lambda = 0
1.76 +
1.77 self.parent = parent
1.78 self.argnames = argnames
1.79 self.defaults = defaults
1.80 @@ -1331,6 +1366,9 @@
1.81 def get_body_block(self):
1.82 return self.body_block
1.83
1.84 + def is_lambda(self):
1.85 + return self._is_lambda
1.86 +
1.87 # Defaults-related methods.
1.88
1.89 def store_default(self, attr_or_value):
1.90 @@ -1431,6 +1469,14 @@
1.91 return 1
1.92 return 0
1.93
1.94 + # Administrative methods.
1.95 +
1.96 + def items_for_vacuum(self):
1.97 + return self.lambdas.items()
1.98 +
1.99 + def vacuum_item(self, name):
1.100 + del self.lambdas[name]
1.101 +
1.102 def finalise_attributes(self):
1.103
1.104 """