1.1 --- a/micropython/ast.py Mon Jul 21 00:42:54 2008 +0200
1.2 +++ b/micropython/ast.py Sat Jul 26 01:16:02 2008 +0200
1.3 @@ -1087,6 +1087,18 @@
1.4 self.discard_temp(temp1)
1.5 self.discard_temp(temp2)
1.6
1.7 + def _generateFunctionDefaults(self, function):
1.8 +
1.9 + "Generate the default initialisation code for 'function'."
1.10 +
1.11 + for attr, default in zip(function.default_attrs, function.defaults):
1.12 + self.dispatch(default)
1.13 +
1.14 + self.record_value()
1.15 + self.new_op(StoreAddress(attr))
1.16 + self.set_source()
1.17 + self.discard_value()
1.18 +
1.19 # Concrete visitor methods.
1.20
1.21 def visitAdd(self, node):
1.22 @@ -1297,19 +1309,11 @@
1.23 self.new_op(LoadConst(node.unit))
1.24
1.25 self.record_value()
1.26 - self._visitName(node, self.name_store_instructions)
1.27 + self._visitName(node, self.name_store_instructions) # AssName equivalent
1.28 self.set_source()
1.29 self.discard_value()
1.30
1.31 - # Generate the default initialisation code.
1.32 -
1.33 - for attr, default in zip(node.unit.default_attrs, node.unit.defaults):
1.34 - self.dispatch(default)
1.35 -
1.36 - self.record_value()
1.37 - self.new_op(StoreAddress(attr))
1.38 - self.set_source()
1.39 - self.discard_value()
1.40 + self._generateFunctionDefaults(node.unit)
1.41
1.42 # Visiting of the code occurs when get_code is invoked on this node.
1.43
1.44 @@ -1338,7 +1342,11 @@
1.45 first = 1
1.46 exit_label = self.new_label()
1.47
1.48 - for test, body in node.tests + [(None, node.else_)]:
1.49 + clauses = node.tests + [(None, node.else_)]
1.50 + last_clause = clauses[-1]
1.51 +
1.52 + for clause in clauses:
1.53 + test, body = clause
1.54 if body is None:
1.55 break
1.56 if not first:
1.57 @@ -1348,7 +1356,8 @@
1.58 next_label = self.new_label()
1.59 self.new_op(JumpIfFalse(next_label))
1.60 self.dispatch(body)
1.61 - self.new_op(Jump(exit_label))
1.62 + if clause is not last_clause:
1.63 + self.new_op(Jump(exit_label))
1.64 first = 0
1.65
1.66 self.set_label(exit_label)
1.67 @@ -1360,7 +1369,21 @@
1.68
1.69 def visitKeyword(self, node): pass
1.70
1.71 - def visitLambda(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Lambda")
1.72 + def visitLambda(self, node):
1.73 +
1.74 + # Produce the reference to this function when visiting this node from
1.75 + # outside.
1.76 +
1.77 + if self.unit is not node.unit:
1.78 + self._generateFunctionDefaults(node.unit)
1.79 + self.new_op(LoadConst(node.unit))
1.80 +
1.81 + # Visiting of the code occurs when get_code is invoked on this node.
1.82 +
1.83 + else:
1.84 + self.dispatch(node.code)
1.85 + self.new_op(StoreResult())
1.86 + self.new_op(Return())
1.87
1.88 def visitLeftShift(self, node):
1.89 self._visitBinary(node, "__lshift__", "__rlshift__")
2.1 --- a/tests/failure/importer.py Mon Jul 21 00:42:54 2008 +0200
2.2 +++ b/tests/failure/importer.py Sat Jul 26 01:16:02 2008 +0200
2.3 @@ -10,6 +10,10 @@
2.4
2.5 imported.x = 1 # detected with warning (despite no collision)
2.6
2.7 +class C: # hack: make an attribute called y known to the system
2.8 + y = None # hack: this prevents compilation errors with the statements
2.9 + # hack: below
2.10 +
2.11 n = None
2.12 n = imported
2.13 n.y = 2 # not detected due to reassignment of n
3.1 --- a/tests/importer.py Mon Jul 21 00:42:54 2008 +0200
3.2 +++ b/tests/importer.py Sat Jul 26 01:16:02 2008 +0200
3.3 @@ -2,13 +2,11 @@
3.4
3.5 import imported
3.6
3.7 -imported.attr = 456 # detected with warning
3.8 +imported.attr # cannot assign to this
3.9
3.10 m = imported
3.11 -imported.a = 9 # detected with warning
3.12 -m.a = 8 # detected with warning (m is always imported)
3.13 -
3.14 -imported.x = 1 # detected with warning (despite no collision)
3.15 +imported.a # cannot assign to this
3.16 +m.a # cannot assign to this (m is known)
3.17
3.18 class C: # hack: make an attribute called y known to the system
3.19 y = None # hack: this prevents compilation errors with the statements
3.20 @@ -16,6 +14,7 @@
3.21
3.22 n = None
3.23 n = imported
3.24 +n.a = 1 # not detected due to reassignment of n
3.25 n.y = 2 # not detected due to reassignment of n
3.26 n.y = 3 # not detected due to reassignment of n
3.27
4.1 --- a/tests/lambda.py Mon Jul 21 00:42:54 2008 +0200
4.2 +++ b/tests/lambda.py Sat Jul 26 01:16:02 2008 +0200
4.3 @@ -1,6 +1,18 @@
4.4 #!/usr/bin/env python
4.5
4.6 identity = lambda x: x
4.7 -lambda a, b=2: a + b
4.8 +
4.9 +def f():
4.10 + return lambda a, b=2: a + b
4.11 +
4.12 +def f2(c):
4.13 + return lambda a, b=c: a + b
4.14 +
4.15 +def g(f, x):
4.16 + return f(x)
4.17 +
4.18 +g(identity, 1)
4.19 +g(f(), 1)
4.20 +g(f2(3), 1)
4.21
4.22 # vim: tabstop=4 expandtab shiftwidth=4