1.1 --- a/micropython/syspython.py Mon Nov 25 15:03:12 2013 +0100
1.2 +++ b/micropython/syspython.py Thu Nov 28 15:49:39 2013 +0100
1.3 @@ -70,7 +70,7 @@
1.4 self.module = module
1.5 self.program = program
1.6 self.objtable = program.get_object_table()
1.7 - self.in_main = False
1.8 + self.current_definition = None
1.9 self.units = []
1.10
1.11 def get_unit(self):
1.12 @@ -86,6 +86,18 @@
1.13 module = self.dispatch(self.module.astnode)
1.14 stream.write(str(module))
1.15
1.16 + def get_original_arguments(self, argnames):
1.17 +
1.18 + # NOTE: The special context argument should not be reproduced.
1.19 + # NOTE: For "dynamic" functions, the context is used to access
1.20 + # NOTE: things like defaults, but could be extended for closures to
1.21 + # NOTE: refer to many namespaces.
1.22 +
1.23 + if argnames and argnames[0] == "<context>":
1.24 + return argnames[1:]
1.25 + else:
1.26 + return argnames
1.27 +
1.28 def NOP(self, node):
1.29 return node
1.30
1.31 @@ -94,6 +106,7 @@
1.32 self.units.append(module)
1.33
1.34 definitions = self.process_definitions(node)
1.35 + self.current_definition = None
1.36
1.37 # keywords(name, ...)
1.38
1.39 @@ -116,14 +129,11 @@
1.40 # def __main__():
1.41 # ...
1.42
1.43 - self.in_main = True
1.44 -
1.45 main = compiler.ast.Function(
1.46 [], "__main__", [], [], 0, "Module initialisation.",
1.47 compiler.ast.Stmt(globalnames + self.dispatch(node.node).nodes)
1.48 )
1.49
1.50 - self.in_main = False
1.51 self.units.pop()
1.52
1.53 return compiler.ast.Module(node.doc, compiler.ast.Stmt(keywords + definitions + [main]))
1.54 @@ -163,7 +173,7 @@
1.55 try:
1.56 # Incorporate class body code in the main function.
1.57
1.58 - if self.in_main:
1.59 + if not self.processing_definition(node):
1.60 return self.dispatch(node.code)
1.61 else:
1.62 return self._visitClassDefinition(node)
1.63 @@ -332,7 +342,10 @@
1.64 self.units.append(node.unit)
1.65
1.66 try:
1.67 - if self.in_main:
1.68 + # NOTE: Need to initialise any defaults outside a definition and to
1.69 + # NOTE: transfer defaults to locals inside a definition.
1.70 +
1.71 + if not self.processing_definition(node):
1.72
1.73 # Generate rebindings of functions.
1.74
1.75 @@ -375,8 +388,9 @@
1.76 # NOTE: Should generate guards for attribute usage operations.
1.77
1.78 code = self.dispatch(node.code)
1.79 + argnames = self.get_original_arguments(node.argnames)
1.80
1.81 - return compiler.ast.Function(node.decorators, node.name, node.argnames, defaults, node.flags, node.doc,
1.82 + return compiler.ast.Function(node.decorators, node.name, argnames, defaults, node.flags, node.doc,
1.83 compiler.ast.Stmt(localnames + globalnames + code.nodes))
1.84
1.85 visitGlobal = NOP
1.86 @@ -892,9 +906,13 @@
1.87 def visitLambda(self, node):
1.88 self.units.append(node.unit)
1.89
1.90 + # NOTE: Need to initialise any defaults.
1.91 +
1.92 try:
1.93 + argnames = self.get_original_arguments(node.argnames)
1.94 +
1.95 return compiler.ast.Lambda(
1.96 - node.argnames,
1.97 + argnames,
1.98 [self.dispatch(n) for n in node.defaults],
1.99 node.flags,
1.100 self.dispatch(node.code)