# HG changeset patch # User Paul Boddie # Date 1384644216 -3600 # Node ID fff7445f3c593900124deb293679a34817bd66dc # Parent 69cdb7cd6c3dbdeb2dca12cc59d2c0bead09ed27 Added initial generator expression inspection support. diff -r 69cdb7cd6c3d -r fff7445f3c59 micropython/inspect.py --- a/micropython/inspect.py Sun Nov 17 00:14:17 2013 +0100 +++ b/micropython/inspect.py Sun Nov 17 00:23:36 2013 +0100 @@ -1218,13 +1218,81 @@ def visitFunction(self, node): return self._visitFunction(node, node.name) - visitGenExpr = OP + # NOTE: GenExpr support is very similar to ListComp support. + + def visitGenExpr(self, node): + self.dispatch(node.code) + + def visitGenExprInner(self, node): + + # Note that explicit dispatch is performed. + + if node.quals: + self.visitGenExprFor(node.quals[0], node.quals[1:], node.expr) + return make_instance() + + def visitGenExprFor(self, node, following_quals, expr): + self.new_branchpoint() + + # Declare names which will be used by generated code. - visitGenExprFor = NOP + self.use_name("__iter__", node.iter) + self.use_name("next") + + in_loop = self.in_loop + self.in_loop = True + self.dispatch(node.iter) + + # NOTE: Could generate AST nodes for the actual operations instead of + # NOTE: manually generating code in micropython.ast. + + self.expr = make_instance() # each element is a result of a function call + self.dispatch(node.assign) + + # Enter the loop. + # Propagate attribute usage to branches. + + self.new_branch(node) + + # Note that explicit dispatch is performed. - visitGenExprIf = NOP + if node.ifs: + self.visitGenExprIf(node.ifs[0], node.ifs[1:], following_quals, expr) + elif following_quals: + self.visitGenExprFor(following_quals[0], following_quals[1:], expr) + else: + self.dispatch(expr) + + self.shelve_branch() + self.in_loop = in_loop + + self.merge_branches() + + def visitGenExprIf(self, node, following_ifs, following_quals, expr): + self.use_name("__bool__", node) + self.new_branchpoint() + + # Propagate attribute usage to branches. - visitGenExprInner = NOP + self.dispatch(node.test) + + # Note that explicit dispatch is performed. + + if following_ifs: + self.visitGenExprIf(following_ifs[0], following_ifs[1:], following_quals, expr) + elif following_quals: + self.visitGenExprFor(following_quals[0], following_quals[1:], expr) + else: + self.new_branch(expr) + self.dispatch(expr) + self.shelve_branch() + + # Maintain a branch for the else clause. + + self.new_branch(NullBranch()) + self.shelve_branch() + + self.merge_branches() def visitGetattr(self, node): node._expr = self.dispatch(node.expr)