# HG changeset patch # User Paul Boddie # Date 1486650962 -3600 # Node ID ddfd3abdd4da84daba56b3fcebc7aca17509fc96 # Parent 1106e953e653f4ff0cbee1f8959559fc8ece7c53 Made class- and module-level temporary names function locals in output programs. diff -r 1106e953e653 -r ddfd3abdd4da optimiser.py --- a/optimiser.py Thu Feb 09 00:37:28 2017 +0100 +++ b/optimiser.py Thu Feb 09 15:36:02 2017 +0100 @@ -300,6 +300,10 @@ ]: for name, attrnames in source.items(): + + # Remove temporary names from structures. + + attrnames = filter(lambda x: not x.startswith("$t"), attrnames) self.all_attrs[(objtype, name)] = attrnames self.locations = get_allocated_locations(self.all_attrs, get_attributes_and_sizes) diff -r 1106e953e653 -r ddfd3abdd4da tests/assign_sequence.py --- a/tests/assign_sequence.py Thu Feb 09 00:37:28 2017 +0100 +++ b/tests/assign_sequence.py Thu Feb 09 15:36:02 2017 +0100 @@ -19,10 +19,18 @@ def h(): return 7, 8, 9 +def i(): + a, b, c = h() + print a, b, c + +# Test assignment operations within functions. + f() g(0) # [4, [1, 2, 3], 6] g(1) # [1, 2, 3] +# Test aliasing, assignment of list elements and direct assignment of elements. + l = [1, 2, 3] x = l a, b, c = l @@ -32,7 +40,12 @@ print d, e, f # 1 2 3 print x # [1, 2, 3] +# Test embedding of sequences in sequences. + m = [4, l, 6] + +# Test sequence truth value interpretation. + if x: n = l else: @@ -40,5 +53,17 @@ print n # [1, 2, 3] +# Test temporary variable usage at module level. + a, b, c = h() print a, b, c # 7 8 9 + +# Test temporary variable usage in functions. + +i() # 7 8 9 + +# Test temporary variable usage in classes. + +class C: + a, b, c = h() + print a, b, c # 7 8 9 diff -r 1106e953e653 -r ddfd3abdd4da translator.py --- a/translator.py Thu Feb 09 00:37:28 2017 +0100 +++ b/translator.py Thu Feb 09 15:36:02 2017 +0100 @@ -1476,6 +1476,11 @@ ref, paths = self.importer.get_module(self.name).special[n.name] return TrResolvedNameRef(n.name, ref) + # Temporary names are output program locals. + + elif n.name.startswith("$t"): + return TrResolvedNameRef(n.name, Reference(""), expr=expr) + # Get the appropriate name for the name reference, using the same method # as in the inspector. @@ -1827,6 +1832,19 @@ print >>self.out, "void __main_%s()" % encode_path(self.name) print >>self.out, "{" self.indent += 1 + + # Define temporary variables, excluded from the module structure itself. + + tempnames = [] + + for n in self.importer.all_module_attrs[self.name]: + if n.startswith("$t"): + tempnames.append(encode_path(n)) + + if tempnames: + tempnames.sort() + self.writeline("__attr %s;" % ", ".join(tempnames)) + self.start_unit() def end_module(self):