Example #1
0
 def visit_For(self, node):
     new_node = gast.For(
         self._visit(node.target),
         self._visit(node.iter),
         self._visit(node.body),
         self._visit(node.orelse),
         None,  # type_comment
     )
     return gast.copy_location(new_node, node)
Example #2
0
 def visit_For(self, node):
     new_node = gast.For(
         self._visit(node.target),
         self._visit(node.iter),
         self._visit(node.body),
         self._visit(node.orelse),
         []  # type_comment
     )
     gast.copy_location(new_node, node)
     new_node.end_lineno = new_node.end_col_offset = None
     return new_node
 def build(maxval, body):
     return gast.For(target=make_name(fresh_name),
                     iter=gast.Call(func=make_name("range"),
                                    args=[
                                        gast.Call(func=make_name("int"),
                                                  args=[maxval],
                                                  keywords=[])
                                    ],
                                    keywords=[]),
                     body=body,
                     orelse=[],
                     type_comment=None)
Example #4
0
def eval_list_comp(nast, env):
    vn = "dummy@" + new_tensor().name  # 重ならない名前にする(ループ内ループもあるため)
    assert len(nast.generators) >= 1
    tast = gast.ast_to_gast(ast.parse("v.append(w)")).body[0]
    tast.value.func.value.id = vn
    tast.value.args[0] = nast.elt

    for gen in nast.generators:
        # とりあえず、このあたりはまだ実装しません
        assert len(gen.ifs) == 0 and gen.is_async == 0
        tast = gast.For(target=gen.target, iter=gen.iter,
                        body=[tast], orelse=[])

    init = gast.ast_to_gast(ast.parse("v = []")).body[0]
    init.targets[0].id = vn
    tast = [init, tast]

    rv = eval_ast(tast, env)
    assert rv.is_none()
    res = env.pop_var(vn)
    return res
Example #5
0
    def nest_reducer(x, g):
        """
        Create a ast.For node from a comprehension and another node.

        g is an ast.comprehension.
        x is the code that have to be executed.

        Examples
        --------
        >> [i for i in range(2)]

        Becomes

        >> for i in range(2):
        >>    ... x code with if clauses ...

        It is a reducer as it can be call recursively for mutli generator.

        Ex : >> [i, j for i in range(2) for j in range(4)]
        """
        def wrap_in_ifs(node, ifs):
            """
            Wrap comprehension content in all possibles if clauses.

            Examples
            --------
            >> [i for i in range(2) if i < 3 if 0 < i]

            Becomes

            >> for i in range(2):
            >>    if i < 3:
            >>        if 0 < i:
            >>            ... the code from `node` ...

            Note the nested ifs clauses.
            """
            return reduce(lambda n, if_: ast.If(if_, [n], []), ifs, node)

        return ast.For(g.target, g.iter, [wrap_in_ifs(x, g.ifs)], [], None)