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