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__")