Skip to content

typing

Functions:

binary_result_type

binary_result_type(
    op_code: str,
    lhs_type: DataType | None,
    rhs_type: DataType | None,
) -> DataType | None
Source code in src/irx/analysis/typing.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def binary_result_type(
    op_code: str,
    lhs_type: astx.DataType | None,
    rhs_type: astx.DataType | None,
) -> astx.DataType | None:
    """
    title: Compute the semantic result type of a binary operator.
    parameters:
      op_code:
        type: str
      lhs_type:
        type: astx.DataType | None
      rhs_type:
        type: astx.DataType | None
    returns:
      type: astx.DataType | None
    """
    if op_code in {"<", ">", "<=", ">=", "==", "!="}:
        return astx.Boolean()

    if op_code in {"&&", "and", "||", "or"}:
        if is_boolean_type(lhs_type) and is_boolean_type(rhs_type):
            return astx.Boolean()
        if is_numeric_type(lhs_type) and is_numeric_type(rhs_type):
            return common_numeric_type(lhs_type, rhs_type)
        return lhs_type if lhs_type == rhs_type else None

    if op_code == "=":
        return lhs_type

    if (
        op_code == "+"
        and is_string_type(lhs_type)
        and is_string_type(rhs_type)
    ):
        return lhs_type

    if op_code in {"+", "-", "*", "/", "%"}:
        return common_numeric_type(lhs_type, rhs_type)

    if op_code in {"|", "&", "^"}:
        return lhs_type

    return lhs_type if lhs_type == rhs_type else None

unary_result_type

unary_result_type(
    op_code: str, operand_type: DataType | None
) -> DataType | None
Source code in src/irx/analysis/typing.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def unary_result_type(
    op_code: str,
    operand_type: astx.DataType | None,
) -> astx.DataType | None:
    """
    title: Compute the semantic result type of a unary operator.
    parameters:
      op_code:
        type: str
      operand_type:
        type: astx.DataType | None
    returns:
      type: astx.DataType | None
    """
    if op_code == "!":
        if is_boolean_type(operand_type):
            return astx.Boolean()
        return operand_type
    if op_code in {"++", "--"} and is_numeric_type(operand_type):
        return operand_type
    return operand_type