1.1 --- a/micropython/data.py Thu Sep 16 20:02:26 2010 +0200
1.2 +++ b/micropython/data.py Fri Sep 17 00:48:32 2010 +0200
1.3 @@ -53,7 +53,7 @@
1.4 """
1.5
1.6 from micropython.program import DataObject, DataValue, ReplaceableContext, PlaceholderContext
1.7 -from micropython.common import AtLeast, InspectError
1.8 +from micropython.common import AtLeast, InspectError, merge_set_dicts
1.9
1.10 def shortrepr(obj):
1.11 if obj is None:
1.12 @@ -103,6 +103,10 @@
1.13 self.attribute_users = [{}] # stack of assignments and branches
1.14 self.attribute_user_shelves = []
1.15
1.16 + # Suspended user details plus loop details.
1.17 +
1.18 + self.suspended_users = [] # stack of lists of users
1.19 +
1.20 # Scope usage, indicating the origin of names.
1.21
1.22 self.scope_usage = [{}] # stack of scope usage
1.23 @@ -580,7 +584,7 @@
1.24
1.25 # Branch management methods.
1.26
1.27 - def _new_branchpoint(self):
1.28 + def _new_branchpoint(self, is_loop=0):
1.29
1.30 """
1.31 Establish a new branchpoint where several control-flow branches diverge
1.32 @@ -590,6 +594,9 @@
1.33 self.attribute_user_shelves.append([])
1.34 self.scope_shelves.append([])
1.35
1.36 + if is_loop:
1.37 + self.suspended_users.append([])
1.38 +
1.39 def _new_branch(self, node):
1.40
1.41 """
1.42 @@ -641,6 +648,16 @@
1.43 self.attribute_users[-1] = AbandonedBranchUsers()
1.44 self.scope_usage[-1] = AbandonedBranchScope()
1.45
1.46 + def _suspend_branch(self):
1.47 +
1.48 + """
1.49 + Suspend a branch for resumption after the current loop.
1.50 + """
1.51 +
1.52 + users = self.suspended_users[-1]
1.53 + users.append(self.attribute_users[-1])
1.54 + self._abandon_branch()
1.55 +
1.56 def _shelve_branch(self):
1.57
1.58 """
1.59 @@ -667,29 +684,8 @@
1.60 # Combine the attribute users. This ensures that a list of users
1.61 # affected by attribute usage is maintained for the current branch.
1.62
1.63 - new_users = {}
1.64 -
1.65 all_shelved_users = self.attribute_user_shelves.pop()
1.66 - all_user_names = set()
1.67 -
1.68 - # Find all the names defined by the branches.
1.69 -
1.70 - for shelved_users in all_shelved_users:
1.71 - all_user_names.update(shelved_users.keys())
1.72 -
1.73 - # Copy all definitions from the branches for the names.
1.74 -
1.75 - for shelved_users in all_shelved_users:
1.76 - for name in all_user_names:
1.77 -
1.78 - if shelved_users.has_key(name):
1.79 - nodes = shelved_users[name]
1.80 -
1.81 - if not new_users.has_key(name):
1.82 - new_users[name] = set(nodes)
1.83 - else:
1.84 - new_users[name].update(nodes)
1.85 -
1.86 + new_users = merge_set_dicts(all_shelved_users)
1.87 self.attribute_users[-1] = new_users
1.88
1.89 # Combine the scope usage.
1.90 @@ -735,6 +731,18 @@
1.91
1.92 self.scope_usage[-1] = new_scope_usage
1.93
1.94 + def _resume_branches(self):
1.95 +
1.96 + """
1.97 + Incorporate users from suspended branches into the current set of active
1.98 + users.
1.99 + """
1.100 +
1.101 + users = self.suspended_users.pop()
1.102 + current_users = self.attribute_users[-1]
1.103 + new_users = merge_set_dicts(users + [current_users])
1.104 + self.attribute_users[-1] = new_users
1.105 +
1.106 # Scope usage methods.
1.107
1.108 def define_scope(self, name, scope):