def f(val): upper_expr = ir.BinaryExpr(meta, val, "<=", upper) lower_expr = ir.BinaryExpr( meta, ir.Atom("SUIT_POINTS", meta, ir.Suit(meta, "@")), ">=", lower) result = ir.BinaryExpr(meta, lower_expr, "and", upper_expr) return result
def resolve_logical_suits(expr: ir.BinaryExpr): if isinstance(expr.lhs, ir.BinaryExpr): expr.lhs = LogicSuit.resolve_logical_suits(expr.lhs) if isinstance(expr.rhs, ir.BinaryExpr): expr.rhs = LogicSuit.resolve_logical_suits(expr.rhs) # Probably can assume only one side is LogicSuit if isinstance(expr.rhs, LogicSuit) and isinstance(expr.lhs, LogicSuit): raise NotImplementedError( "Only one side of expression can be a logic suit") if isinstance(logic_suit := getattr(expr.lhs, "child", None), LogicSuit): variant1 = deepcopy(expr) variant2 = deepcopy(expr) variant1.lhs = logic_suit.lhs variant2.lhs = logic_suit.rhs return ir.BinaryExpr(expr.meta, variant1, logic_suit.type, variant2)
class LogicSuit: type: str lhs: ir.Suit rhs: ir.Suit meta: Optional[Any] = field(repr=False, compare=False) @staticmethod def resolve_logical_suits(expr: ir.BinaryExpr): if isinstance(expr.lhs, ir.BinaryExpr): expr.lhs = LogicSuit.resolve_logical_suits(expr.lhs) if isinstance(expr.rhs, ir.BinaryExpr): expr.rhs = LogicSuit.resolve_logical_suits(expr.rhs) # Probably can assume only one side is LogicSuit if isinstance(expr.rhs, LogicSuit) and isinstance(expr.lhs, LogicSuit): raise NotImplementedError( "Only one side of expression can be a logic suit") if isinstance(logic_suit := getattr(expr.lhs, "child", None), LogicSuit): variant1 = deepcopy(expr) variant2 = deepcopy(expr) variant1.lhs = logic_suit.lhs variant2.lhs = logic_suit.rhs return ir.BinaryExpr(expr.meta, variant1, logic_suit.type, variant2) elif isinstance(getattr(expr.rhs, "child", None), LogicSuit): variant1 = deepcopy(expr) variant2 = deepcopy(expr) variant1.lhs = logic_suit.lhs variant2.lhs = logic_suit.rhs return ir.BinaryExpr(expr.meta, variant1, logic_suit.type, variant2)
def unprioritized(*args, meta=None): head, *tail = args return Condition( reduce(lambda acc, x: ir.BinaryExpr(meta, acc, "and", x), tail, head), None, meta)
def prioritized(*args, meta=None): *conditions, priority = args head, *tail = conditions return Condition( reduce(lambda acc, x: ir.BinaryExpr(meta, acc, "and", x), tail, head), priority, meta)
def cmp(lhs, op, rhs, meta=None): return LogicSuit.resolve_logical_suits( ir.BinaryExpr(meta, lhs, str(op), rhs))
def f(val): lower_expr = ir.BinaryExpr(meta, val, "==", lower) return lower_expr
def f(val): upper_expr = ir.BinaryExpr(meta, val, "<=", upper) return upper_expr