1.1 --- a/docs/invocation.txt Sun Jul 27 02:00:31 2008 +0200
1.2 +++ b/docs/invocation.txt Mon Jul 28 00:26:29 2008 +0200
1.3 @@ -116,6 +116,17 @@
1.4
1.5 <context> is instance: no change
1.6
1.7 +Defaults for unknown callables:
1.8 +
1.9 + f(obj) # f not known at compile-time
1.10 +
1.11 + f -> f
1.12 + -> load context for argument #1
1.13 + obj -> argument #2
1.14 +
1.15 + Then, check the number of arguments and the availability of defaults against
1.16 + the details provided by the callable's structure.
1.17 +
1.18 Functions as methods:
1.19
1.20 def f(x, y, z): ...
2.1 --- a/docs/structures.txt Sun Jul 27 02:00:31 2008 +0200
2.2 +++ b/docs/structures.txt Mon Jul 28 00:26:29 2008 +0200
2.3 @@ -107,11 +107,12 @@
2.4
2.5 A suitable structure layout might be something like this:
2.6
2.7 - Identifier Address Type Object ...
2.8 + Identifier Address Details Type Object ...
2.9
2.10 - 0 1 2 3 4
2.11 - classcode invocation __class__ attribute ...
2.12 - reference reference reference
2.13 + 0 1 2 3 4 5
2.14 + classcode invocation invocation __class__ attribute ...
2.15 + reference #args, reference reference
2.16 + #defaults
2.17
2.18 Here, the classcode refers to the attribute lookup table for the object. Since
2.19 classes and instances share the same classcode, they might resemble the
2.20 @@ -119,16 +120,17 @@
2.21
2.22 Class C:
2.23
2.24 - 0 1 2 3 4
2.25 - code for C __new__ class type attribute ...
2.26 - reference reference reference
2.27 + 0 1 2 3 4 5
2.28 + code for C __new__ __new__ class type attribute ...
2.29 + reference #args, reference reference
2.30 + #defaults
2.31
2.32 Instance of C:
2.33
2.34 - 0 1 2 3 4
2.35 - code for C C.__call__ class C attribute ...
2.36 - reference reference reference
2.37 - (if exists)
2.38 + 0 1 2 3 4 5
2.39 + code for C C.__call__ C.__call__ class C attribute ...
2.40 + reference #args, reference reference
2.41 + (if exists) #defaults
2.42
2.43 The __new__ reference would lead to code consisting of the following
2.44 instructions:
2.45 @@ -144,10 +146,10 @@
2.46
2.47 Function f:
2.48
2.49 - 0 1 2 3 4
2.50 - code for code class attribute ...
2.51 - function reference function reference
2.52 - reference
2.53 + 0 1 2 3 4 5
2.54 + code for code code class attribute ...
2.55 + function reference #args, function reference
2.56 + #defaults reference
2.57
2.58 Here, the code reference would lead to code for the function. Note that the
2.59 function locals are completely distinct from this structure and are not
2.60 @@ -158,10 +160,10 @@
2.61
2.62 Module m:
2.63
2.64 - 0 1 2 3 4
2.65 - code for m (unused) module type attribute ...
2.66 - reference (global)
2.67 - reference
2.68 + 0 1 2 3 4 5
2.69 + code for m (unused) (unused) module type attribute ...
2.70 + reference (global)
2.71 + reference
2.72
2.73 Both classes and modules have code in their definitions, but this would be
2.74 generated in order and not referenced externally.
3.1 --- a/micropython/ast.py Sun Jul 27 02:00:31 2008 +0200
3.2 +++ b/micropython/ast.py Mon Jul 28 00:26:29 2008 +0200
3.3 @@ -220,6 +220,10 @@
3.4 self.new_op(StoreTemp(temp_position))
3.5 return LoadTemp(temp_position)
3.6
3.7 + def ensure_temp(self):
3.8 + if isinstance(self.active, LoadTemp):
3.9 + self.temp_position = max(self.temp_position, self.active.attr + 1)
3.10 +
3.11 def reserve_temp(self, n):
3.12 temp_position = self.temp_position
3.13 self.temp_position += n
3.14 @@ -475,6 +479,11 @@
3.15 if self._should_optimise_temp_storage() and \
3.16 self._have_temp_compatible_access():
3.17
3.18 + # Where an existing temporary storage reference will be retained,
3.19 + # keep it active.
3.20 +
3.21 + self.ensure_temp()
3.22 +
3.23 last = self.last_op()
3.24 self.remove_op()
3.25 return last
3.26 @@ -811,13 +820,13 @@
3.27 for i in range(ncontext, nargs_min):
3.28 if i not in employed_positions:
3.29 raise TranslateError(self.module.full_name(), node,
3.30 - "Argument %r not supplied for %r: need at least %d arguments." % (i+1, target.name, nargs_min))
3.31 + "Argument %r not supplied for %r: need at least %d argument(s)." % (i+1, target.name, nargs_min))
3.32
3.33 nargs = len(args)
3.34
3.35 if nargs > nargs_max and not target.has_star and not target.has_dstar:
3.36 raise TranslateError(self.module.full_name(), node,
3.37 - "Too many arguments for %r: need at most %d arguments." % (target.name, nargs_max))
3.38 + "Too many arguments for %r: need at most %d argument(s)." % (target.name, nargs_max))
3.39
3.40 # Where defaults are involved, put them into the frame.
3.41
3.42 @@ -1613,7 +1622,20 @@
3.43
3.44 def visitTryFinally(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "TryFinally")
3.45
3.46 - def visitTuple(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Tuple")
3.47 + def visitTuple(self, node):
3.48 + self.new_op(MakeObject(len(node.nodes)))
3.49 + temp = self.get_temp()
3.50 +
3.51 + for i, n in enumerate(node.nodes):
3.52 + self.dispatch(n)
3.53 + self.record_value()
3.54 + self.new_op(temp)
3.55 + self.new_op(StoreAttr(Attr(i, None, None, None)))
3.56 + self.set_source()
3.57 + self.discard_value()
3.58 +
3.59 + self.new_op(temp)
3.60 + self.discard_temp(temp)
3.61
3.62 def visitUnaryAdd(self, node):
3.63 self._visitUnary(node, "__pos__")
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/failure/instance.py Mon Jul 28 00:26:29 2008 +0200
4.3 @@ -0,0 +1,18 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +class C:
4.7 + pass
4.8 +
4.9 +class D:
4.10 + def __init__(self):
4.11 + pass
4.12 +
4.13 +class E:
4.14 + def __init__(self, x):
4.15 + pass
4.16 +
4.17 +c = C()
4.18 +d = D()
4.19 +e = E()
4.20 +
4.21 +# vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/tests/instance.py Sun Jul 27 02:00:31 2008 +0200
5.2 +++ b/tests/instance.py Mon Jul 28 00:26:29 2008 +0200
5.3 @@ -7,7 +7,12 @@
5.4 def __init__(self):
5.5 pass
5.6
5.7 +class E:
5.8 + def __init__(self, x):
5.9 + pass
5.10 +
5.11 c = C()
5.12 d = D()
5.13 +e = E(1)
5.14
5.15 # vim: tabstop=4 expandtab shiftwidth=4
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/tests/tuple.py Mon Jul 28 00:26:29 2008 +0200
6.3 @@ -0,0 +1,7 @@
6.4 +#!/usr/bin/env python
6.5 +
6.6 +1, 2, 3
6.7 +a = (1, 2, 3)
6.8 +1, (2, 3), 4, 5
6.9 +
6.10 +# vim: tabstop=4 expandtab shiftwidth=4