Beispiel #1
0
    def get_code(self, cl, bindenv, body):
        vars = self.lhs_vars(cl)
        assert_unique(vars)
        mask = L.mask_from_bounds(vars, bindenv)

        comparison = L.Compare(L.Name(cl.tup), L.Eq(), L.tuplify(cl.elts))

        if L.mask_is_allbound(mask):
            code = (L.If(comparison, body, ()), )
            needs_typecheck = True

        elif mask.m.startswith('b'):
            elts_mask = L.mask_from_bounds(cl.elts, bindenv)
            code = L.bind_by_mask(elts_mask, cl.elts, L.Name(cl.tup))
            if L.mask_is_allunbound(elts_mask):
                code += body
            else:
                code += (L.If(comparison, body, ()), )
            needs_typecheck = True

        elif mask == L.mask('u' + 'b' * len(cl.elts)):
            code = (L.Assign(cl.tup, L.tuplify(cl.elts)), )
            code += body
            needs_typecheck = False

        else:
            raise L.TransformationError('Cannot emit code for TUP clause '
                                        'that would require an auxiliary '
                                        'map; use demand filtering')

        if needs_typecheck and self.use_typecheck:
            code = (L.If(L.HasArity(L.Name(cl.tup), len(cl.elts)), code, ()), )

        return code
Beispiel #2
0
    def get_code(self, cl, bindenv, body):
        vars = self.lhs_vars(cl)
        assert_unique(vars)
        mask = L.mask_from_bounds(vars, bindenv)

        lookup_expr = L.DictLookup(L.Name(cl.map), L.Name(cl.key), None)

        if L.mask_is_allbound(mask):
            comparison = L.Compare(L.Name(cl.value), L.Eq(), lookup_expr)
            code = (L.If(comparison, body, ()), )
            needs_typecheck = True

        elif mask == L.mask('bbu'):
            code = (L.Assign(cl.value, lookup_expr), )
            code += body
            needs_typecheck = True

        elif mask == L.mask('buu'):
            items_expr = L.Parser.pe('_MAP.items()', subst={'_MAP': cl.map})
            code = (L.DecompFor([cl.key, cl.value], items_expr, body), )
            needs_typecheck = True

        else:
            code = super().get_code(cl, bindenv, body)
            needs_typecheck = False

        if needs_typecheck and self.use_typecheck:
            code = (L.If(L.IsMap(L.Name(cl.map)), code, ()), )

        return code
Beispiel #3
0
    def get_code(self, cl, bindenv, body):
        assert_unique(cl.vars)
        mask = L.mask_from_bounds(cl.vars, bindenv)
        check_eq = L.Compare(L.tuplify(cl.vars), L.Eq(), cl.value)

        if L.mask_is_allbound(mask):
            code = (L.If(check_eq, body, ()), )

        elif L.mask_is_allunbound(mask):
            code = (L.DecompAssign(cl.vars, cl.value), )
            code += body

        else:
            code = L.bind_by_mask(mask, cl.vars, cl.value)
            code += (L.If(check_eq, body, ()), )

        return code
Beispiel #4
0
    def get_code(self, cl, bindenv, body):
        vars = self.lhs_vars(cl)
        assert_unique(vars)
        mask = L.mask_from_bounds(vars, bindenv)

        if L.mask_is_allbound(mask):
            comparison = L.Compare(L.Name(cl.elem), L.In(), L.Name(cl.set))
            code = (L.If(comparison, body, ()), )
            needs_typecheck = True

        elif mask == L.mask('bu'):
            code = (L.For(cl.elem, L.Name(cl.set), body), )
            needs_typecheck = True

        else:
            code = super().get_code(cl, bindenv, body)
            needs_typecheck = False

        if needs_typecheck and self.use_typecheck:
            code = (L.If(L.IsSet(L.Name(cl.set)), code, ()), )

        return code
Beispiel #5
0
    def get_code(self, cl, bindenv, body):
        vars = self.lhs_vars(cl)
        rel = self.rhs_rel(cl)
        assert_unique(vars)
        mask = L.mask_from_bounds(vars, bindenv)

        if L.mask_is_allbound(mask):
            comparison = L.Compare(L.tuplify(vars), L.In(), L.Name(rel))
            code = (L.If(comparison, body, ()), )

        elif L.mask_is_allunbound(mask):
            code = (L.DecompFor(vars, L.Name(rel), body), )

        else:
            bvars, uvars = L.split_by_mask(mask, vars)
            lookup = L.ImgLookup(L.Name(rel), mask, bvars)
            # Optimize in the case where there's only one unbound.
            if len(uvars) == 1:
                code = (L.For(uvars[0], L.Unwrap(lookup), body), )
            else:
                code = (L.DecompFor(uvars, lookup, body), )

        return code
Beispiel #6
0
    def get_code(self, cl, bindenv, body):
        vars = self.lhs_vars(cl)
        assert_unique(vars)
        mask = L.mask_from_bounds(vars, bindenv)

        if L.mask_is_allbound(mask):
            comparison = L.Compare(L.Name(cl.value), L.Eq(),
                                   L.Attribute(L.Name(cl.obj), cl.attr))
            code = (L.If(comparison, body, ()), )
            needs_typecheck = True

        elif mask == L.mask('bu'):
            code = (L.Assign(cl.value, L.Attribute(L.Name(cl.obj), cl.attr)), )
            code += body
            needs_typecheck = True

        else:
            code = super().get_code(cl, bindenv, body)
            needs_typecheck = False

        if needs_typecheck and self.use_typecheck:
            code = (L.If(L.HasField(L.Name(cl.obj), cl.attr), code, ()), )

        return code
Beispiel #7
0
def make_eq_cond(left, right):
    """Make a condition of form <var> == <var>."""
    return L.Cond(L.Compare(L.Name(left), L.Eq(), L.Name(right)))
Beispiel #8
0
    'make_eq_cond',
    'SelfJoin',
    'ClauseTools',
    'CoreClauseTools',
]

from enum import Enum

from incoq.util.seq import zip_strict
from incoq.util.collections import OrderedSet, Partitioning
from incoq.compiler.incast import L

from .clause import ClauseVisitor, CoreClauseVisitor, Kind, ShouldFilter

eq_cond_pattern = L.Cond(
    L.Compare(L.Name(L.PatVar('LEFT')), L.Eq(), L.Name(L.PatVar('RIGHT'))))


def match_eq_cond(tree):
    """If tree is a condition clause with form <var> == <var>, return
    a pair of the variables. Otherwise return None.
    """
    result = L.match(eq_cond_pattern, tree)
    if result is None:
        return None
    else:
        return result['LEFT'], result['RIGHT']


def make_eq_cond(left, right):
    """Make a condition of form <var> == <var>."""