"""Operator overloading with automatic span capture and expression normalization."""
from __future__ import annotations
__all__ = ["make_binary", "truediv"]
import inspect
from pypto.pypto_impl import ir as _ir
from ._utils import _normalize_expr
def make_binary(op_name: str, left: _ir.Expr, right: _ir.Expr, span: _ir.Span | None = None) -> _ir.Expr:
"""Build a binary scalar expression through the C++ promotion rules."""
actual_span = span if span is not None else _ir.Span.unknown()
return getattr(_ir, op_name)(left, right, actual_span)
def truediv(left: _ir.Expr, right: _ir.Expr, span: _ir.Span | None = None) -> _ir.Expr:
"""True division with Python-consistent promotion (kept for backward compat)."""
return make_binary("truediv", left, right, span)
def capture_call_span() -> _ir.Span:
"""Capture span from the caller's location.
Goes back through the call stack to find the actual user code that invoked the operator.
Returns:
Span: Source location of the caller
"""
frame = inspect.currentframe()
if frame is not None and frame.f_back is not None:
frame = frame.f_back.f_back
if frame is not None:
info = inspect.getframeinfo(frame)
return _ir.Span(info.filename, info.lineno, -1)
return _ir.Span.unknown()
def _make_binary_op(op_name: str):
"""Create a binary operator wrapper with span capture and normalization.
Args:
op_name: Name of the operator (e.g., "add")
Returns:
Wrapper function
"""
def wrapper(self: _ir.Expr, other: int | float | _ir.Expr) -> _ir.Expr:
span = capture_call_span()
other_expr = _normalize_expr(other, span)
return make_binary(op_name, self, other_expr, span)
return wrapper
def _make_reverse_binary_op(op_name: str):
"""Create a binary operator wrapper with span capture and normalization.
Args:
op_name: Name of the operator (e.g., "add")
Returns:
Wrapper function
"""
def wrapper(self: _ir.Expr, other: int | float | _ir.Expr) -> _ir.Expr:
span = capture_call_span()
other_expr = _normalize_expr(other, span)
return make_binary(op_name, other_expr, self, span)
return wrapper
def _make_unary_op(op_name: str):
"""Create a unary operator wrapper with span capture.
Args:
op_name: Name of the operator (e.g., "neg")
Returns:
Wrapper function
"""
def wrapper(self) -> _ir.Expr:
span = capture_call_span()
return getattr(_ir, op_name)(self, span)
return wrapper
def _patch_operators():
"""Patch Expr and Var classes with operator overloads."""
_ir.Expr.__add__ = _make_binary_op("add")
_ir.Expr.__sub__ = _make_binary_op("sub")
_ir.Expr.__mul__ = _make_binary_op("mul")
_ir.Expr.__truediv__ = _make_binary_op("truediv")
_ir.Expr.__floordiv__ = _make_binary_op("floordiv")
_ir.Expr.__mod__ = _make_binary_op("mod")
_ir.Expr.__pow__ = _make_binary_op("pow")
_ir.Expr.__eq__ = _make_binary_op("eq")
_ir.Expr.__ne__ = _make_binary_op("ne")
_ir.Expr.__lt__ = _make_binary_op("lt")
_ir.Expr.__le__ = _make_binary_op("le")
_ir.Expr.__gt__ = _make_binary_op("gt")
_ir.Expr.__ge__ = _make_binary_op("ge")
_ir.Expr.__and__ = _make_binary_op("bit_and")
_ir.Expr.__or__ = _make_binary_op("bit_or")
_ir.Expr.__xor__ = _make_binary_op("bit_xor")
_ir.Expr.__lshift__ = _make_binary_op("bit_shift_left")
_ir.Expr.__rshift__ = _make_binary_op("bit_shift_right")
_ir.Expr.__neg__ = _make_unary_op("neg")
_ir.Expr.__invert__ = _make_unary_op("bit_not")
_ir.Expr.__radd__ = _make_reverse_binary_op("add")
_ir.Expr.__rmul__ = _make_reverse_binary_op("mul")
_ir.Expr.__rsub__ = _make_reverse_binary_op("sub")
_ir.Expr.__rtruediv__ = _make_reverse_binary_op("truediv")
_ir.Expr.__rfloordiv__ = _make_reverse_binary_op("floordiv")
_ir.Expr.__rmod__ = _make_reverse_binary_op("mod")
_ir.Expr.__rpow__ = _make_reverse_binary_op("pow")
_ir.Expr.__rand__ = _make_reverse_binary_op("bit_and")
_ir.Expr.__ror__ = _make_reverse_binary_op("bit_or")
_ir.Expr.__rxor__ = _make_reverse_binary_op("bit_xor")
_ir.Expr.__rlshift__ = _make_reverse_binary_op("bit_shift_left")
_ir.Expr.__rrshift__ = _make_reverse_binary_op("bit_shift_right")
_patch_operators()