8000 [mypyc] Introduce FormatOp and add a tokenizer for .format() call by 97littleleaf11 · Pull Request #10935 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[mypyc] Introduce FormatOp and add a tokenizer for .format() call #10935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 6, 2021
Prev Previous commit
Next Next commit
Add FormatOp
  • Loading branch information
97littleleaf11 committed Aug 5, 2021
commit 2ac7f4c85705bff2afbdc43b065c3ebbf9a43157
32 changes: 31 additions & 1 deletion mypyc/irbuild/format_str_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@

from typing import List, Tuple, Optional
from typing_extensions import Final
from enum import Enum

from mypy.checkstrformat import (
parse_format_value, ConversionSpecifier, parse_conversion_specifiers
)
from mypy.errors import Errors
from mypy.messages import MessageBuilder
from mypy.nodes import Context
from mypy.nodes import Context, Expression

from mypyc.ir.ops import Value, Integer
from mypyc.ir.rtypes import c_pyssize_t_rprimitive
from mypyc.irbuild.builder import IRBuilder
from mypyc.primitives.str_ops import str_build_op


class ConvType(Enum):
STR = 's'
INT = 'd'


class FormatOp:
is_valid = False
conv_type = ConvType.STR

def __eq__(self, other: 'FormatOp') -> bool:
if self.is_valid and other.is_valid:
return self.conv_type == other.conv_type
return False


def tokenizer_printf_style(format_str: str) -> Tuple[List[str], List[ConversionSpecifier]]:
"""Tokenize a printf-style format string using regex.

Expand Down Expand Up @@ -71,6 +87,20 @@ def tokenizer_format_call(
return literals, specifiers


def convert_expr(builder: IRBuilder, exprs: List[Expression], line: int) -> List[Value]:
converted = []
for x in exprs:
node_type = builder.node_type(x)
if is_str_rprimitive(node_type):
var_str = builder.accept(x)
elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type):
var_str = builder.call_c(int_to_str_op, [builder.accept(x)], line)
else:
var_str = builder.call_c(str_op, [builder.accept(x)], line)
converted.append(var_str)
return converted


def join_formatted_strings(builder: IRBuilder, literals: List[str],
substitutions: List[Value], line: int) -> Value:
"""Merge the list of literals and the list of substitutions
Expand Down
0