Beispiel #1
0
def _(s):
    body = stmt.Block(list(map(to_stmt, s.body)))
    items = []
    for item in s.items:
        name = stmt.Symbol(item.optional_vars and item.optional_vars.id)
        items.append((to_expr(item.context_expr), name))
    return stmt.While(items, body)
Beispiel #2
0
def parse_stmt(src):
    """
    Parse a string that represents a Python statement and return the
    corresponding Expr instance.
    """
    body = ast_parse(src).body
    if len(body) == 1:
        return to_stmt(body[0])
    else:
        return stmt.Block([to_stmt(x) for x in body])
Beispiel #3
0
def _(s):
    # TODO: better function representation
    # (probably we should use a safer way than what Python does with arguments)
    name = s.name
    a = s.args
    arg_name = a.vararg and a.vararg.arg
    kwarg_name = a.vararg and a.vararg.arg
    kw_args = [arg.arg for arg in a.kwonlyargs]
    kw_defaults = list(map(to_expr, a.kw_defaults))

    defaults = list(map(to_expr, a.defaults))
    args = [arg.arg for arg in a.args]
    kwargs = dict(zip(args[-len(defaults):], defaults))
    args = args[:len(args) - len(defaults)]
    body = stmt.Block(list(map(to_stmt, s.body)))
    return stmt.Def(name, args, kwargs, body)
Beispiel #4
0
def _(s):
    body = stmt.Block(list(map(to_stmt, s.body)))
    other = stmt.Block(list(map(to_stmt, s.orelse)))
    return stmt.While(to_expr(s.test), body, other)
Beispiel #5
0
def _(s):
    body = stmt.Block(list(map(to_stmt, s.body)))
    other = stmt.Block(list(map(to_stmt, s.orelse)))
    return stmt.For(to_expr(s.target), to_expr(s.descendants), body, other)
Beispiel #6
0
def _(s):
    cond = to_expr(s.test)
    then = stmt.Block(map(to_stmt, s.body))
    other = stmt.Block(map(to_stmt, s.orelse))
    return stmt.If(cond, then, other)