Пример #1
0
def translate_printf_style_formatting(builder: IRBuilder,
                                      format_expr: Union[StrExpr, BytesExpr],
                                      rhs: Expression) -> Optional[Value]:
    tokens = tokenizer_printf_style(format_expr.value)
    if tokens is not None:
        literals, format_ops = tokens

        exprs = []
        if isinstance(rhs, TupleExpr):
            exprs = rhs.items
        elif isinstance(rhs, Expression):
            exprs.append(rhs)

        if isinstance(format_expr, BytesExpr):
            substitutions = convert_format_expr_to_bytes(builder, format_ops,
                                                         exprs, format_expr.line)
            if substitutions is not None:
                return join_formatted_bytes(builder, literals, substitutions, format_expr.line)
        else:
            substitutions = convert_format_expr_to_str(builder, format_ops,
                                                       exprs, format_expr.line)
            if substitutions is not None:
                return join_formatted_strings(builder, literals, substitutions, format_expr.line)

    return None
Пример #2
0
def translate_str_format_percent_sign(builder: IRBuilder, format_expr: StrExpr,
                                      rhs: Expression) -> Value:
    tokens = tokenizer_printf_style(format_expr.value)
    if tokens is not None:
        literals, format_ops = tokens

        exprs = []
        if isinstance(rhs, TupleExpr):
            exprs = rhs.items
        elif isinstance(rhs, Expression):
            exprs.append(rhs)

        substitutions = convert_expr(builder, format_ops, exprs,
                                     format_expr.line)
        if substitutions is not None:
            return join_formatted_strings(builder, literals, substitutions,
                                          format_expr.line)

    call_c_ops_candidates = binary_ops.get('%', [])
    ret = builder.builder.matching_call_c(
        call_c_ops_candidates,
        [builder.accept(format_expr),
         builder.accept(rhs)], format_expr.line)
    assert ret is not None, 'Cannot use binary op % at line {}'.format(
        format_expr.line)
    return ret