(
op: str, left: ConstantValue, right: ConstantValue
)
| 77 | |
| 78 | |
| 79 | def constant_fold_binary_op( |
| 80 | op: str, left: ConstantValue, right: ConstantValue |
| 81 | ) -> ConstantValue | None: |
| 82 | if isinstance(left, int) and isinstance(right, int): |
| 83 | return constant_fold_binary_int_op(op, left, right) |
| 84 | |
| 85 | # Float and mixed int/float arithmetic. |
| 86 | if isinstance(left, float) and isinstance(right, float): |
| 87 | return constant_fold_binary_float_op(op, left, right) |
| 88 | elif isinstance(left, float) and isinstance(right, int): |
| 89 | return constant_fold_binary_float_op(op, left, right) |
| 90 | elif isinstance(left, int) and isinstance(right, float): |
| 91 | return constant_fold_binary_float_op(op, left, right) |
| 92 | |
| 93 | # String concatenation and multiplication. |
| 94 | if op == "+" and isinstance(left, str) and isinstance(right, str): |
| 95 | return left + right |
| 96 | elif op == "*" and isinstance(left, str) and isinstance(right, int): |
| 97 | return left * right |
| 98 | elif op == "*" and isinstance(left, int) and isinstance(right, str): |
| 99 | return left * right |
| 100 | |
| 101 | # Complex construction. |
| 102 | if op == "+" and isinstance(left, (int, float)) and isinstance(right, complex): |
| 103 | return left + right |
| 104 | elif op == "+" and isinstance(left, complex) and isinstance(right, (int, float)): |
| 105 | return left + right |
| 106 | elif op == "-" and isinstance(left, (int, float)) and isinstance(right, complex): |
| 107 | return left - right |
| 108 | elif op == "-" and isinstance(left, complex) and isinstance(right, (int, float)): |
| 109 | return left - right |
| 110 | |
| 111 | return None |
| 112 | |
| 113 | |
| 114 | def constant_fold_binary_int_op(op: str, left: int, right: int) -> int | float | None: |
no test coverage detected
searching dependent graphs…