Lichen

Changeset

561:ddfd3abdd4da
2017-02-09 Paul Boddie raw files shortlog changelog graph Made class- and module-level temporary names function locals in output programs.
optimiser.py (file) tests/assign_sequence.py (file) translator.py (file)
     1.1 --- a/optimiser.py	Thu Feb 09 00:37:28 2017 +0100
     1.2 +++ b/optimiser.py	Thu Feb 09 15:36:02 2017 +0100
     1.3 @@ -300,6 +300,10 @@
     1.4              ]:
     1.5  
     1.6              for name, attrnames in source.items():
     1.7 +
     1.8 +                # Remove temporary names from structures.
     1.9 +
    1.10 +                attrnames = filter(lambda x: not x.startswith("$t"), attrnames)
    1.11                  self.all_attrs[(objtype, name)] = attrnames
    1.12  
    1.13          self.locations = get_allocated_locations(self.all_attrs, get_attributes_and_sizes)
     2.1 --- a/tests/assign_sequence.py	Thu Feb 09 00:37:28 2017 +0100
     2.2 +++ b/tests/assign_sequence.py	Thu Feb 09 15:36:02 2017 +0100
     2.3 @@ -19,10 +19,18 @@
     2.4  def h():
     2.5      return 7, 8, 9
     2.6  
     2.7 +def i():
     2.8 +    a, b, c = h()
     2.9 +    print a, b, c
    2.10 +
    2.11 +# Test assignment operations within functions.
    2.12 +
    2.13  f()
    2.14  g(0)                        # [4, [1, 2, 3], 6]
    2.15  g(1)                        # [1, 2, 3]
    2.16  
    2.17 +# Test aliasing, assignment of list elements and direct assignment of elements.
    2.18 +
    2.19  l = [1, 2, 3]
    2.20  x = l
    2.21  a, b, c = l
    2.22 @@ -32,7 +40,12 @@
    2.23  print d, e, f               # 1 2 3
    2.24  print x                     # [1, 2, 3]
    2.25  
    2.26 +# Test embedding of sequences in sequences.
    2.27 +
    2.28  m = [4, l, 6]
    2.29 +
    2.30 +# Test sequence truth value interpretation.
    2.31 +
    2.32  if x:
    2.33      n = l
    2.34  else:
    2.35 @@ -40,5 +53,17 @@
    2.36  
    2.37  print n                     # [1, 2, 3]
    2.38  
    2.39 +# Test temporary variable usage at module level.
    2.40 +
    2.41  a, b, c = h()
    2.42  print a, b, c               # 7 8 9
    2.43 +
    2.44 +# Test temporary variable usage in functions.
    2.45 +
    2.46 +i()                         # 7 8 9
    2.47 +
    2.48 +# Test temporary variable usage in classes.
    2.49 +
    2.50 +class C:
    2.51 +    a, b, c = h()
    2.52 +    print a, b, c           # 7 8 9
     3.1 --- a/translator.py	Thu Feb 09 00:37:28 2017 +0100
     3.2 +++ b/translator.py	Thu Feb 09 15:36:02 2017 +0100
     3.3 @@ -1476,6 +1476,11 @@
     3.4              ref, paths = self.importer.get_module(self.name).special[n.name]
     3.5              return TrResolvedNameRef(n.name, ref)
     3.6  
     3.7 +        # Temporary names are output program locals.
     3.8 +
     3.9 +        elif n.name.startswith("$t"):
    3.10 +            return TrResolvedNameRef(n.name, Reference("<var>"), expr=expr)
    3.11 +
    3.12          # Get the appropriate name for the name reference, using the same method
    3.13          # as in the inspector.
    3.14  
    3.15 @@ -1827,6 +1832,19 @@
    3.16          print >>self.out, "void __main_%s()" % encode_path(self.name)
    3.17          print >>self.out, "{"
    3.18          self.indent += 1
    3.19 +
    3.20 +        # Define temporary variables, excluded from the module structure itself.
    3.21 +
    3.22 +        tempnames = []
    3.23 +
    3.24 +        for n in self.importer.all_module_attrs[self.name]:
    3.25 +            if n.startswith("$t"):
    3.26 +                tempnames.append(encode_path(n))
    3.27 +
    3.28 +        if tempnames:
    3.29 +            tempnames.sort()
    3.30 +            self.writeline("__attr %s;" % ", ".join(tempnames))
    3.31 +
    3.32          self.start_unit()
    3.33  
    3.34      def end_module(self):