Beispiel #1
0
    def generate_For(self, context, node):
        context.stmts.append("for (")

        orig_end_stmt = context.end_stmt

        context.end_stmt = "; "
        context.visit(node.init)
        init = context.body.pop()

        guard = context.visit(node.guard)
        context.stmts.append((guard.code, "; "))

        context.end_stmt = (") {\n", context.tab)
        context.visit(node.update_stmt)
        update_stmt = context.body.pop()

        context.end_stmt = orig_end_stmt

        parent_body = context.body
        body = context.body = []
        for stmt in node.body:
            context.visit(stmt)
        context.body = parent_body

        context.stmts.append((context.untab, "}\n"))

        context.body.append(
            astx.copy_node(
                node,
                target=astx.deep_copy_node(node.target),
                iter=astx.copy_node(node.iter),
                body=body,
                orelse=[],
                init=init,
                guard=guard,
                update_stmt=update_stmt))
Beispiel #2
0
 def visit_For(self, node):
     if node.orelse:
         raise InvalidOperationError(
            "else clauses on for loops are not supported.", node)
     
     # We only support the standard for x in ([start, ]stop[, step]) syntax
     # with positive step sizes.
     
     # insert missing iteration bounds if not specified
     iter = astx.deep_copy_node(node.iter)
     if isinstance(iter, _ast.Tuple):
         elts = iter.elts
         n_elts = len(elts)
         if n_elts == 1:
             start = _ast.Num(n=0)
             stop = elts[0]
             step = _ast.Num(n=1)
         elif n_elts == 2:
             start = elts[0]
             stop = elts[1]
             step = _ast.Num(n=1)
         elif n_elts == 3:
             start = elts[0]
             stop = elts[1]
             step = elts[2]
         else:
             raise InvalidOperationError(
                "Invalid number of elements specified in for-loop index.", node)
     else:
         start = _ast.Num(n=0)
         stop = iter
         step = _ast.Num(n=1)
     
     # visit target
     target = node.target
     if not isinstance(target, _ast.Name):
         raise InvalidOperationError(
             "Invalid target -- only named variables are supported.")
         
     target = astx.copy_node(target)
     init = self.visit(_ast.Assign(targets=[target], value=start))
     
     # to create the guard operation, we need a Load version of the target
     target_load = astx.copy_node(target, ctx=_ast.Load())
     guard = self.visit(_ast.Compare(left=target_load, comparators=[stop], 
                                     ops=[_ast.Lt()]))
     
     # Create an update statement
     update_stmt = self.visit(
         _ast.AugAssign(
             target=target, 
             op=_ast.Add(), 
             value=step
         )
     )
     
     return astx.copy_node(node,
         target=target,
         iter=iter,
         body=[self.visit(stmt) for stmt in node.body],
         orelse=[],
         
         init=init,
         guard=guard,
         update_stmt=update_stmt
     )