示例#1
0
def translate_safe_generator_call(builder: IRBuilder, expr: CallExpr,
                                  callee: RefExpr) -> Optional[Value]:
    # Special cases for things that consume iterators where we know we
    # can safely compile a generator into a list.
    if (len(expr.args) > 0 and expr.arg_kinds[0] == ARG_POS
            and isinstance(expr.args[0], GeneratorExpr)):
        if isinstance(callee, MemberExpr):
            return builder.gen_method_call(
                builder.accept(callee.expr), callee.name,
                ([builder.translate_list_comprehension(expr.args[0])] +
                 [builder.accept(arg) for arg in expr.args[1:]]),
                builder.node_type(expr), expr.line, expr.arg_kinds,
                expr.arg_names)
        else:
            return builder.call_refexpr_with_args(
                expr, callee,
                ([builder.translate_list_comprehension(expr.args[0])] +
                 [builder.accept(arg) for arg in expr.args[1:]]))
    return None
示例#2
0
def transform_generator_expr(builder: IRBuilder, o: GeneratorExpr) -> Value:
    builder.warning('Treating generator comprehension as list', o.line)
    return builder.primitive_op(iter_op,
                                [builder.translate_list_comprehension(o)],
                                o.line)
示例#3
0
def transform_list_comprehension(builder: IRBuilder,
                                 o: ListComprehension) -> Value:
    return builder.translate_list_comprehension(o.generator)