Compute expression result types for operators without embedding that logic
directly into the main semantic visitor.
Functions:
binary_result_type
binary_result_type(
op_code: str,
lhs_type: DataType | None,
rhs_type: DataType | None,
) -> DataType | None
Source code in packages/irx/src/irx/analysis/typing.py
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 | @typechecked
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()
return 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)
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 packages/irx/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
83 | @typechecked
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 None
if op_code in {"++", "--"} and is_numeric_type(operand_type):
return operand_type
return operand_type
|