1.1 --- a/micropython/inspect.py Sun Nov 17 00:14:17 2013 +0100
1.2 +++ b/micropython/inspect.py Sun Nov 17 00:23:36 2013 +0100
1.3 @@ -1218,13 +1218,81 @@
1.4 def visitFunction(self, node):
1.5 return self._visitFunction(node, node.name)
1.6
1.7 - visitGenExpr = OP
1.8 + # NOTE: GenExpr support is very similar to ListComp support.
1.9 +
1.10 + def visitGenExpr(self, node):
1.11 + self.dispatch(node.code)
1.12 +
1.13 + def visitGenExprInner(self, node):
1.14 +
1.15 + # Note that explicit dispatch is performed.
1.16 +
1.17 + if node.quals:
1.18 + self.visitGenExprFor(node.quals[0], node.quals[1:], node.expr)
1.19 + return make_instance()
1.20 +
1.21 + def visitGenExprFor(self, node, following_quals, expr):
1.22 + self.new_branchpoint()
1.23 +
1.24 + # Declare names which will be used by generated code.
1.25
1.26 - visitGenExprFor = NOP
1.27 + self.use_name("__iter__", node.iter)
1.28 + self.use_name("next")
1.29 +
1.30 + in_loop = self.in_loop
1.31 + self.in_loop = True
1.32 + self.dispatch(node.iter)
1.33 +
1.34 + # NOTE: Could generate AST nodes for the actual operations instead of
1.35 + # NOTE: manually generating code in micropython.ast.
1.36 +
1.37 + self.expr = make_instance() # each element is a result of a function call
1.38 + self.dispatch(node.assign)
1.39 +
1.40 + # Enter the loop.
1.41 + # Propagate attribute usage to branches.
1.42 +
1.43 + self.new_branch(node)
1.44 +
1.45 + # Note that explicit dispatch is performed.
1.46
1.47 - visitGenExprIf = NOP
1.48 + if node.ifs:
1.49 + self.visitGenExprIf(node.ifs[0], node.ifs[1:], following_quals, expr)
1.50 + elif following_quals:
1.51 + self.visitGenExprFor(following_quals[0], following_quals[1:], expr)
1.52 + else:
1.53 + self.dispatch(expr)
1.54 +
1.55 + self.shelve_branch()
1.56 + self.in_loop = in_loop
1.57 +
1.58 + self.merge_branches()
1.59 +
1.60 + def visitGenExprIf(self, node, following_ifs, following_quals, expr):
1.61 + self.use_name("__bool__", node)
1.62 + self.new_branchpoint()
1.63 +
1.64 + # Propagate attribute usage to branches.
1.65
1.66 - visitGenExprInner = NOP
1.67 + self.dispatch(node.test)
1.68 +
1.69 + # Note that explicit dispatch is performed.
1.70 +
1.71 + if following_ifs:
1.72 + self.visitGenExprIf(following_ifs[0], following_ifs[1:], following_quals, expr)
1.73 + elif following_quals:
1.74 + self.visitGenExprFor(following_quals[0], following_quals[1:], expr)
1.75 + else:
1.76 + self.new_branch(expr)
1.77 + self.dispatch(expr)
1.78 + self.shelve_branch()
1.79 +
1.80 + # Maintain a branch for the else clause.
1.81 +
1.82 + self.new_branch(NullBranch())
1.83 + self.shelve_branch()
1.84 +
1.85 + self.merge_branches()
1.86
1.87 def visitGetattr(self, node):
1.88 node._expr = self.dispatch(node.expr)