micropython

Changeset

214:398f466a7011
2009-05-16 Paul Boddie raw files shortlog changelog graph Added notes about star parameter handling approaches. Made minor documentation/comment fixes.
docs/concepts.txt (file) docs/invocation.txt (file) micropython/ast.py (file) micropython/trans.py (file) rsvp.py (file)
     1.1 --- a/docs/concepts.txt	Mon May 11 02:17:12 2009 +0200
     1.2 +++ b/docs/concepts.txt	Sat May 16 01:34:03 2009 +0200
     1.3 @@ -221,6 +221,8 @@
     1.4  removed and the attrcode is not specified for classes: the presence of an
     1.5  attrcode indicates that a given object is an instance.
     1.6  
     1.7 +See below for details of attrcodes.
     1.8 +
     1.9  Invocation Reference
    1.10  --------------------
    1.11  
    1.12 @@ -301,7 +303,7 @@
    1.13  the attribute locations to store their elements, where each element is a
    1.14  reference to a separately stored object.
    1.15  
    1.16 -Testing Instance Compatibility with Classes (attrcode)
    1.17 +Testing Instance Compatibility with Classes (Attrcode)
    1.18  ------------------------------------------------------
    1.19  
    1.20  Although it would be possible to have a data structure mapping classes to
     2.1 --- a/docs/invocation.txt	Mon May 11 02:17:12 2009 +0200
     2.2 +++ b/docs/invocation.txt	Sat May 16 01:34:03 2009 +0200
     2.3 @@ -40,9 +40,9 @@
     2.4  Awkward cases (extra arguments):
     2.5  
     2.6    f(1, 2, 3, 4)   # put arguments in frame
     2.7 -                  # if f is not known, add arguments vs. parameters check
     2.8 -                  # target unpacks superfluous arguments from the end of the
     2.9 -                  # frame
    2.10 +                  # if f is not known, add arguments vs. parameters check;
    2.11 +                  # to handle superfluous arguments, make a suitable object
    2.12 +                  # and fill it with all such arguments
    2.13  
    2.14  Very awkward cases:
    2.15  
    2.16 @@ -179,3 +179,52 @@
    2.17                    # gives context c
    2.18    fn(1, 2)        # instance context -> no explicit context required
    2.19                    # context c inserted in call
    2.20 +
    2.21 +Star parameters are a convenience:
    2.22 +
    2.23 +  max(1, 2, 3)    # call to max(*args) where args == (1, 2, 3)
    2.24 +  max((1, 2, 3))  # but why not just do this instead?
    2.25 +
    2.26 +  One motivation: avoid explicitly making sequences.
    2.27 +  Opportunity: avoid expensive dynamic allocation of sequences?
    2.28 +
    2.29 +Star parameters, known callables and sequences:
    2.30 +
    2.31 +  g(1, 2, 3, 4)   # g known as function g(a, *args) at compile-time
    2.32 +
    2.33 +    g   -> don't get any context information
    2.34 +    1   -> argument #1
    2.35 +    2   -> reference to sequence containing arguments #2, #3, #4
    2.36 +
    2.37 +  (This according to approach #1 described for unknown callables. With approach
    2.38 +   #2, normal argument positioning would occur.)
    2.39 +
    2.40 +Star parameters, unknown callables:
    2.41 +
    2.42 +  g(1, 2, 3, 4)   # g not known at compile-time
    2.43 +
    2.44 +    g   -> g
    2.45 +        -> load context for argument #1
    2.46 +    1   -> argument #2
    2.47 +    2   -> argument #3
    2.48 +    3   -> argument #4
    2.49 +    4   -> argument #5
    2.50 +
    2.51 +  Then, check the context and shift the frame if necessary (described above).
    2.52 +
    2.53 +  If g has a star parameter - g(a, *args) - then...
    2.54 +
    2.55 +  Approach #1 - move arguments #3, #4, #5 (or shifted to #2, #3, #4) into a
    2.56 +                sequence, adding a reference to the sequence in their place
    2.57 +
    2.58 +  Approach #2 - maintain special access rules to arguments #3, #4, #5 (perhaps
    2.59 +                shifted to #2, #3, #4) as a C-like array
    2.60 +
    2.61 +Tradeoffs for star parameter approaches:
    2.62 +
    2.63 +  Approach #1 - potentially costly at run-time as arguments need moving around,
    2.64 +                but the arguments would behave normally in functions
    2.65 +
    2.66 +  Approach #2 - need to track usage of the star parameter and to possibly copy
    2.67 +                its contents if assigned, as well as providing special access
    2.68 +                mechanisms, but the invocation procedure would be simpler
     3.1 --- a/micropython/ast.py	Mon May 11 02:17:12 2009 +0200
     3.2 +++ b/micropython/ast.py	Sat May 16 01:34:03 2009 +0200
     3.3 @@ -596,11 +596,6 @@
     3.4              extend = ExtendFrame()
     3.5              self.new_op(extend)
     3.6  
     3.7 -            # Handle * parameters.
     3.8 -            # NOTE: Not handling ** parameters yet.
     3.9 -
    3.10 -            # Make a tuple from the arguments corresponding to the * parameter.
    3.11 -
    3.12              self.dispatch(node.code)
    3.13              if not isinstance(self.last_op(), Return):
    3.14                  self.dispatch(compiler.ast.Name("None"))
     4.1 --- a/micropython/trans.py	Mon May 11 02:17:12 2009 +0200
     4.2 +++ b/micropython/trans.py	Sat May 16 01:34:03 2009 +0200
     4.3 @@ -719,6 +719,9 @@
     4.4              if target.has_star or target.has_dstar:
     4.5                  frame_size = max(nargs, nargs_max)
     4.6  
     4.7 +                # NOTE: We now need to pack these arguments into a suitable
     4.8 +                # NOTE: structure for the * parameter.
     4.9 +
    4.10              # For other parameter lists, only accept as many arguments as we are
    4.11              # allowed.
    4.12  
     5.1 --- a/rsvp.py	Mon May 11 02:17:12 2009 +0200
     5.2 +++ b/rsvp.py	Sat May 16 01:34:03 2009 +0200
     5.3 @@ -424,8 +424,6 @@
     5.4              self.exception = self.attr_error
     5.5              return self.RaiseException()
     5.6  
     5.7 -    # NOTE: LoadAttrIndexContextCond will test context compatibility.
     5.8 -
     5.9      def StoreAttrIndex(self):
    5.10          context, ref = self.value
    5.11          data = self.load(ref)