Exemplo n.º 1
0
        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
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
 def unprioritized(*args, meta=None):
     head, *tail = args
     return Condition(
         reduce(lambda acc, x: ir.BinaryExpr(meta, acc, "and", x), tail,
                head), None, meta)
Exemplo n.º 5
0
 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)
Exemplo n.º 6
0
 def cmp(lhs, op, rhs, meta=None):
     return LogicSuit.resolve_logical_suits(
         ir.BinaryExpr(meta, lhs, str(op), rhs))
Exemplo n.º 7
0
 def f(val):
     lower_expr = ir.BinaryExpr(meta, val, "==", lower)
     return lower_expr
Exemplo n.º 8
0
        def f(val):
            upper_expr = ir.BinaryExpr(meta, val, "<=", upper)

            return upper_expr