def _(e): op = OPERATOR_MAP[type(e.op)] *values, x, y = e.values res = expr.BinOp(op, to_expr(x), to_expr(y)) while values: res = expr.BinOp(op, to_expr(values.pop()), res) return res
def _(s): target, = map(to_expr, s.targets) if isinstance(s.value, ast.Yield): return stmt.YieldAssign(target, to_expr(s.value.value)) elif isinstance(target, ast.YieldFrom): return stmt.YieldFromAssign(target, to_expr(s.value.value)) return stmt.Assign(target, to_expr(s.value))
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)
def parse_expr(src): """ Parse a string that represents a Python expression and return the corresponding Expr instance. """ e, = ast_parse(src).body assert isinstance(e, ast.Expr) return to_expr(e.value)
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)
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)
def _(s): if isinstance(s.value, ast.Yield): return stmt.Yield(to_expr(s.value.value)) elif isinstance(s.value, ast.YieldFrom): return stmt.YieldFrom(to_expr(s.value.value)) return stmt.ExprStmt(to_expr(s.value))
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)
def _(e): ops = [OPERATOR_MAP[type(op)] for op in e.ops] values = [to_expr(e.left), *map(to_expr, e.comparators)] return expr.Compare(ops, values)
def _(e): func = to_expr(e.func) args = [*map(to_expr, e.args)] kwargs = {kw.arg: to_expr(kw.value) for kw in e.keywords} return expr.Call(func, args, kwargs)
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]) # Utility functions cte = lambda c: lambda x: c dbg = lambda x, *args: print(x, *args) or x fields = lambda cls, fs, T=to_expr: lambda e: cls(*(T(getattr(e, f)) for f in fs)) emap = lambda xs: map(to_expr, xs) # Simple expression handlers register_expr(ast.Attribute, lambda e: expr.GetAttr(to_expr(e.value), e.attr)) register_expr(ast.BinOp, fields(expr.BinOp, ["op", "left", "right"])) register_expr(ast.Ellipsis, lambda e: expr.Atom(...)) register_expr(ast.IfExp, fields(expr.Ternary, ["test", "body", "orelse"])) register_expr(ast.List, lambda e: expr.List([*emap(e.elts)])) register_expr(ast.Name, lambda e: expr.Name(e.id)) register_expr(ast.NameConstant, lambda e: expr.Atom(e.value)) register_expr(ast.Num, lambda e: expr.Atom(e.n)) register_expr(ast.Set, lambda e: expr.Set([*emap(e.elts)])) register_expr(ast.Str, lambda e: expr.Atom(e.s)) register_expr(ast.Tuple, lambda e: expr.Tuple([*emap(e.elts)])) register_expr(ast.Dict, lambda e: expr.Dict([*zip(emap(e.keys), emap(e.values))])) # Simple statement handlers register_stmt(ast.Return, lambda s: stmt.Return(to_expr(s.value)))