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
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
def get_code(self, cl, bindenv, body): assert_unique(cl.vars) mask = L.mask_from_bounds(cl.vars, bindenv) keyvars, valvar = L.split_by_mask(cl.mask, cl.vars) valvar = valvar[0] # Can also handle all-unbound case by iterating over dict.items(), # but requires fresh var for decomposing key tuple. if L.mask_is_allbound(mask): comparison = L.Parser.pe('_KEY in _MAP and _MAP[_KEY] == _VALUE', subst={ '_MAP': cl.map, '_KEY': L.tuplify(keyvars), '_VALUE': valvar }) code = (L.If(comparison, body, ()), ) elif mask == cl.mask: code = L.Parser.pc(''' if _KEY in _MAP: _VALUE = _MAP[_KEY] _BODY ''', subst={ '_MAP': cl.map, '_KEY': L.tuplify(keyvars), '_VALUE': valvar, '<c>_BODY': body }) else: code = super().get_code(cl, bindenv, body) return code
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
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
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
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
def get_code(self, cl, bindenv, body): return (L.If(cl.cond, body, []), )