def _normalize_user_idx(cls, provided_idx): if not isinstance(provided_idx, tuple): provided_idx = (provided_idx, ) idx = _util.odict() for item in provided_idx: if isinstance(item, six.string_types): lbl, ty = (item, _product.unit) elif isinstance(item, slice): lbl, ty = item.start, item.stop if item.step is not None: raise typy.TypeFormationError("Invalid variant: " + str(lbl)) else: raise typy.TypeFormationError("Invalid variant definition.") if (not isinstance(lbl, six.string_types) or not typy._is_Name_constructor_id(lbl)): raise typy.TypeFormationError( "Label '" + str(lbl) + "' is not a non-empty string with initial capital.") if lbl in idx: raise typy.TypeFormationError("Duplicate label '" + lbl + "'.") if not isinstance(ty, typy.Type): raise typy.TypeFormationError("Payload for label '" + lbl + "' is not a type.") idx[lbl] = ty return idx
def translate_pat_Call_constructor(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) args, keywords = pat.args, pat.keywords idx = self.idx conditions = [] binding_translations = _util.odict() for i, arg in enumerate(args): n = _util.odict_idx_of(idx, i) elt_scrutinee_trans = astx.make_Subscript_Num_Index( scrutinee_trans_copy, n) elt_condition, elt_binding_translations = ctx.translate_pat( arg, elt_scrutinee_trans) conditions.append(elt_condition) binding_translations.update(elt_binding_translations) for keyword in keywords: n = _util.odict_idx_of(idx, keyword.arg) elt_scrutinee_trans = astx.make_Subscript_Num_Index( scrutinee_trans_copy, n) elt_condition, elt_binding_translations = ctx.translate_pat( keyword.value, elt_scrutinee_trans) conditions.append(elt_condition) binding_translations.update(elt_binding_translations) condition = ast.BoolOp(op=ast.And(), values=conditions) return (condition, binding_translations)
def ana_pat_Dict(self, ctx, pat): keys, values = pat.keys, pat.values idx = self.idx n_keys, n_idx = len(keys), len(idx) if n_keys < n_idx: raise _errors.TyError("Too few elements in pattern.", pat) elif n_keys > n_idx: raise _errors.TyError("Too many elements in pattern.", keys[n_idx]) used_labels = set() bindings = _util.odict() n_bindings = 0 for key, value in zip(keys, values): label = _read_label(key) if label in used_labels: raise _errors.TyError("Duplicate label: " + str(label), key) used_labels.add(label) key.label = label try: ty = idx[label] except KeyError: raise _errors.TyError("Invalid label: " + str(label), key) elt_bindings = ctx.ana_pat(value, ty) n_elt_bindings = len(elt_bindings) bindings.update(elt_bindings) n_bindings_new = len(bindings) if n_bindings_new != n_bindings + n_elt_bindings: raise _errors.TyError("Duplicate variable in pattern.", pat) n_bindings = n_bindings_new return bindings
def ana_pat_Dict(self, ctx, pat): keys, values = pat.keys, pat.values idx = self.idx n_keys, n_idx = len(keys), len(idx) if n_keys < n_idx: raise _errors.TyError("Too few elements in pattern.", pat) elif n_keys > n_idx: raise _errors.TyError("Too many elements in pattern.", keys[n_idx]) used_labels = set() bindings = _util.odict() n_bindings = 0 for key, value in zip(keys, values): label = _read_label(key) if label in used_labels: raise _errors.TyError("Duplicate label: " + str(label), key) used_labels.add(label) key.label = label try: ty = idx[label] except KeyError: raise _errors.TyError("Invalid label: " + str(label), key) elt_bindings = ctx.ana_pat(value, ty) n_elt_bindings = len(elt_bindings) bindings.update(elt_bindings) n_bindings_new = len(bindings) if n_bindings_new != n_bindings + n_elt_bindings: raise _errors.TyError("Duplicate variable in pattern.", pat) n_bindings = n_bindings_new return bindings
def _normalize_user_idx(cls, provided_idx): if not isinstance(provided_idx, tuple): provided_idx = (provided_idx,) idx = _util.odict() for item in provided_idx: if isinstance(item, six.string_types): lbl, ty = (item, _product.unit) elif isinstance(item, slice): lbl, ty = item.start, item.stop if item.step is not None: raise typy.TypeFormationError( "Invalid variant: " + str(lbl)) else: raise typy.TypeFormationError( "Invalid variant definition.") if(not isinstance(lbl, six.string_types) or not typy._is_Name_constructor_id(lbl)): raise typy.TypeFormationError( "Label '" + str(lbl) + "' is not a non-empty string with initial capital.") if lbl in idx: raise typy.TypeFormationError( "Duplicate label '" + lbl + "'.") if not isinstance(ty, typy.Type): raise typy.TypeFormationError( "Payload for label '" + lbl + "' is not a type.") idx[lbl] = ty return idx
def translate_pat_Num(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) comparator = astx.copy_node(pat) condition = ast.Compare(left=scrutinee_trans_copy, ops=[ast.Eq()], comparators=[comparator]) return (condition, _util.odict())
def translate_pat_Call_constructor(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) args, keywords = pat.args, pat.keywords idx = self.idx conditions = [] binding_translations = _util.odict() for i, arg in enumerate(args): n = _util.odict_idx_of(idx, i) elt_scrutinee_trans = astx.make_Subscript_Num_Index( scrutinee_trans_copy, n) elt_condition, elt_binding_translations = ctx.translate_pat( arg, elt_scrutinee_trans) conditions.append(elt_condition) binding_translations.update(elt_binding_translations) for keyword in keywords: n = _util.odict_idx_of(idx, keyword.arg) elt_scrutinee_trans = astx.make_Subscript_Num_Index( scrutinee_trans_copy, n) elt_condition, elt_binding_translations = ctx.translate_pat( keyword.value, elt_scrutinee_trans) conditions.append(elt_condition) binding_translations.update(elt_binding_translations) condition = ast.BoolOp( op=ast.And(), values=conditions) return (condition, binding_translations)
def translate_pat_Name_constructor(self, ctx, pat, scrutinee_trans): lbl = pat.id condition = ast.Compare( left=scrutinee_trans, ops=[ast.Eq()], comparators=[ast.Str(s=lbl)]) return condition, _util.odict()
def translate_pat_Num(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) comparator = astx.copy_node(pat) condition = ast.Compare( left=scrutinee_trans_copy, ops=[ast.Eq()], comparators=[comparator]) return (condition, _util.odict())
def ana_pat_Unary_Name_constructor(self, ctx, pat): id = pat.operand.id if id != "Inf": raise _errors.TyError("Invalid ieee literal pattern.", pat) if not isinstance(pat.op, (ast.UAdd, ast.USub)): raise _errors.TyError( "Invalid unary operator on ieee literal pattern.", pat) return _util.odict()
def ana_pat_Unary_Name_constructor(self, ctx, pat): id = pat.operand.id if id != "Inf": raise _errors.TyError("Invalid ieee literal pattern.", pat) if not isinstance(pat.op, (ast.UAdd, ast.USub)): raise _errors.TyError( "Invalid unary operator on ieee literal pattern.", pat) return _util.odict()
def translate_pat_Num(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) comparator = astx.copy_node(pat) n = pat.n if not isinstance(n, complex): comparator.n = complex(n) condition = ast.Compare(left=scrutinee_trans_copy, ops=[ast.Eq()], comparators=[comparator]) return (condition, _util.odict())
def translate_pat_Unary_Name_constructor(self, ctx, pat, scrutinee_trans): if isinstance(pat.op, ast.USub): s = "-Inf" else: s = "Inf" condition = ast.Compare( left=scrutinee_trans, ops=[ast.Eq()], comparators=[astx.builtin_call("float", [ast.Str(s=s)])]) return (condition, _util.odict())
def translate_pat_Num(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) comparator = astx.copy_node(pat) n = pat.n if not isinstance(n, complex): comparator.n = complex(n) condition = ast.Compare( left=scrutinee_trans_copy, ops=[ast.Eq()], comparators=[comparator]) return (condition, _util.odict())
def translate_pat_Name_constructor(cls, ctx, pat, scrutinee_trans): id = pat.id if id == "NaN": condition = astx.method_call(astx.import_expr('math'), 'isnan', [scrutinee_trans]) else: condition = ast.Compare( left=scrutinee_trans, ops=[ast.Eq()], comparators=[astx.builtin_call("float", [ast.Str(s=id)])]) return (condition, _util.odict())
def translate_pat_Unary_Name_constructor(self, ctx, pat, scrutinee_trans): if isinstance(pat.op, ast.USub): s = "-Inf" else: s = "Inf" condition = ast.Compare( left=scrutinee_trans, ops=[ast.Eq()], comparators=[ astx.builtin_call("float", [ast.Str(s=s)])] ) return (condition, _util.odict())
def ana_pat_Name_constructor(self, ctx, pat): idx = self.idx lbl = pat.id try: ty = idx[lbl] except KeyError: raise _errors.TyError( "Label not found in finsum: " + lbl, pat) if ty != _product.unit: raise _errors.TyError( "Label has non-unit type but no payload pattern was applied: " + lbl, pat) return _util.odict()
def ana_pat_Name_constructor(self, ctx, pat): idx = self.idx lbl = pat.id try: ty = idx[lbl] except KeyError: raise _errors.TyError("Label not found in finsum: " + lbl, pat) if ty != _product.unit: raise _errors.TyError( "Label has non-unit type but no payload pattern was applied: " + lbl, pat) return _util.odict()
def translate_pat_Tuple(self, ctx, pat, scrutinee_trans): elts = pat.elts rl, im = elts[0], elts[1] rl_scrutinee_trans = astx.make_Attribute(scrutinee_trans, 'real') im_scrutinee_trans = astx.make_Attribute(scrutinee_trans, 'imag') (rl_cond, rl_binding_translations) = ctx.translate_pat(rl, rl_scrutinee_trans) (im_cond, im_binding_translations) = ctx.translate_pat(im, im_scrutinee_trans) condition = astx.make_binary_And(rl_cond, im_cond) binding_translations = _util.odict(rl_binding_translations) binding_translations.update(im_binding_translations) return condition, binding_translations
def translate_pat_Name_constructor(cls, ctx, pat, scrutinee_trans): id = pat.id if id == "NaN": condition = astx.method_call( astx.import_expr('math'), 'isnan', [scrutinee_trans]) else: condition = ast.Compare( left=scrutinee_trans, ops=[ast.Eq()], comparators=[ astx.builtin_call("float", [ast.Str(s=id)])] ) return (condition, _util.odict())
def translate_pat_Tuple(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) elts = pat.elts idx = self.idx conditions = [] binding_translations = _util.odict() for n, (elt, ty) in enumerate(zip(elts, idx.itervalues())): elt_scrutinee_trans = astx.make_Subscript_Num_Index( scrutinee_trans_copy, n) elt_condition, elt_binding_translations = ctx.translate_pat( elt, elt_scrutinee_trans) conditions.append(elt_condition) binding_translations.update(elt_binding_translations) condition = ast.BoolOp(op=ast.And(), values=conditions) return (condition, binding_translations)
def translate_pat_Tuple(self, ctx, pat, scrutinee_trans): elts = pat.elts rl, im = elts[0], elts[1] rl_scrutinee_trans = astx.make_Attribute(scrutinee_trans, 'real') im_scrutinee_trans = astx.make_Attribute(scrutinee_trans, 'imag') (rl_cond, rl_binding_translations) = ctx.translate_pat(rl, rl_scrutinee_trans) (im_cond, im_binding_translations) = ctx.translate_pat(im, im_scrutinee_trans) condition = astx.make_binary_And(rl_cond, im_cond) binding_translations = _util.odict(rl_binding_translations) binding_translations.update(im_binding_translations) return condition, binding_translations
def translate_pat_Dict(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) keys, values = pat.keys, pat.values idx = self.idx conditions = [] binding_translations = _util.odict() for key, value in zip(keys, values): label = key.label n = _util.odict_idx_of(idx, label) elt_scrutinee_trans = astx.make_Subscript_Num_Index( scrutinee_trans_copy, n) elt_condition, elt_binding_translations = ctx.translate_pat( value, elt_scrutinee_trans) conditions.append(elt_condition) binding_translations.update(elt_binding_translations) condition = ast.BoolOp(op=ast.And(), values=conditions) return (condition, binding_translations)
def ana_pat_Tuple(self, ctx, pat): elts = pat.elts if len(elts) != 2: raise _errors.TyError( "Using a tuple pattern for a value of type cplx requires two elements.", pat) rl, im = elts[0], elts[1] rl_bindings = ctx.ana_pat(rl, ieee) im_bindings = ctx.ana_pat(im, ieee) n_rl_bindings = len(rl_bindings) n_im_bindings = len(im_bindings) bindings = _util.odict(rl_bindings) bindings.update(im_bindings) n_bindings = len(bindings) if n_bindings != n_rl_bindings + n_im_bindings: raise _errors.TyError("Duplicated variables in pattern.", pat) return bindings
def ana_pat_Call_constructor(self, ctx, pat): id = pat.func.id if id != 'X': raise _errors.TyError("tpl supports only the 'X' constructor", pat.func) if pat.starargs is not None: raise _errors.TyError("No support for starargs", pat) if pat.kwargs is not None: raise _errors.TyError("No support for kwargs", pat) args = pat.args keywords = pat.keywords idx = self.idx bindings = _util.odict() n_bindings = 0 for i, arg in enumerate(args): try: ty = idx[i] except KeyError: raise _errors.TyError("Invalid label: " + str(i), arg) elt_bindings = ctx.ana_pat(arg, ty) n_elt_bindings = len(elt_bindings) bindings.update(elt_bindings) n_bindings_new = len(bindings) if n_bindings_new != n_bindings + n_elt_bindings: raise _errors.TyError("Duplicate variable in pattern.", arg) n_bindings = n_bindings_new for keyword in keywords: label = keyword.arg try: ty = idx[label] except KeyError: raise _errors.TyError("Invalid label: " + label, keyword) value = keyword.value elt_bindings = ctx.ana_pat(value, ty) n_elt_bindings = len(elt_bindings) bindings.update(elt_bindings) n_bindings_new = len(bindings) if n_bindings_new != n_bindings + n_elt_bindings: raise _errors.TyError("Duplicate variable in pattern.", value) n_bindings = n_bindings_new return bindings
def ana_pat_Tuple(self, ctx, pat): elts = pat.elts if len(elts) != 2: raise _errors.TyError( "Using a tuple pattern for a value of type cplx requires two elements.", pat) rl, im = elts[0], elts[1] rl_bindings = ctx.ana_pat(rl, ieee) im_bindings = ctx.ana_pat(im, ieee) n_rl_bindings = len(rl_bindings) n_im_bindings = len(im_bindings) bindings = _util.odict(rl_bindings) bindings.update(im_bindings) n_bindings = len(bindings) if n_bindings != n_rl_bindings + n_im_bindings: raise _errors.TyError("Duplicated variables in pattern.", pat) return bindings
def ana_pat_Call_constructor(self, ctx, pat): id = pat.func.id if id != 'X': raise _errors.TyError("tpl supports only the 'X' constructor", pat.func) if pat.starargs is not None: raise _errors.TyError("No support for starargs", pat) if pat.kwargs is not None: raise _errors.TyError("No support for kwargs", pat) args = pat.args keywords = pat.keywords idx = self.idx bindings = _util.odict() n_bindings = 0 for i, arg in enumerate(args): try: ty = idx[i] except KeyError: raise _errors.TyError("Invalid label: " + str(i), arg) elt_bindings = ctx.ana_pat(arg, ty) n_elt_bindings = len(elt_bindings) bindings.update(elt_bindings) n_bindings_new = len(bindings) if n_bindings_new != n_bindings + n_elt_bindings: raise _errors.TyError("Duplicate variable in pattern.", arg) n_bindings = n_bindings_new for keyword in keywords: label = keyword.arg try: ty = idx[label] except KeyError: raise _errors.TyError("Invalid label: " + label, keyword) value = keyword.value elt_bindings = ctx.ana_pat(value, ty) n_elt_bindings = len(elt_bindings) bindings.update(elt_bindings) n_bindings_new = len(bindings) if n_bindings_new != n_bindings + n_elt_bindings: raise _errors.TyError("Duplicate variable in pattern.", value) n_bindings = n_bindings_new return bindings
def translate_pat_Tuple(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) elts = pat.elts idx = self.idx conditions = [] binding_translations = _util.odict() for n, (elt, ty) in enumerate(zip(elts, idx.itervalues())): elt_scrutinee_trans = astx.make_Subscript_Num_Index( scrutinee_trans_copy, n) elt_condition, elt_binding_translations = ctx.translate_pat( elt, elt_scrutinee_trans) conditions.append(elt_condition) binding_translations.update(elt_binding_translations) condition = ast.BoolOp( op=ast.And(), values=conditions) return (condition, binding_translations)
def translate_pat_Dict(self, ctx, pat, scrutinee_trans): scrutinee_trans_copy = astx.copy_node(scrutinee_trans) keys, values = pat.keys, pat.values idx = self.idx conditions = [] binding_translations = _util.odict() for key, value in zip(keys, values): label = key.label n = _util.odict_idx_of(idx, label) elt_scrutinee_trans = astx.make_Subscript_Num_Index( scrutinee_trans_copy, n) elt_condition, elt_binding_translations = ctx.translate_pat( value, elt_scrutinee_trans) conditions.append(elt_condition) binding_translations.update(elt_binding_translations) condition = ast.BoolOp( op=ast.And(), values=conditions) return (condition, binding_translations)
def ana_pat_Tuple(self, ctx, pat): elts = pat.elts idx = self.idx n_elts, n_idx = len(elts), len(idx) if n_elts < n_idx: raise _errors.TyError("Too few components in tpl pattern.", pat) elif n_elts > n_idx: raise _errors.TyError("Too many components in tpl pattern.", elts[n_idx]) bindings = _util.odict() n_bindings = 0 for elt, ty in zip(elts, idx.itervalues()): elt_bindings = ctx.ana_pat(elt, ty) n_elt_bindings = len(elt_bindings) bindings.update(elt_bindings) n_bindings_new = len(bindings) if n_bindings_new != n_bindings + n_elt_bindings: raise _errors.TyError("Duplicate variable in pattern.", pat) n_bindings = n_bindings_new return bindings
def ana_pat_Tuple(self, ctx, pat): elts = pat.elts idx = self.idx n_elts, n_idx = len(elts), len(idx) if n_elts < n_idx: raise _errors.TyError( "Too few components in tpl pattern.", pat) elif n_elts > n_idx: raise _errors.TyError( "Too many components in tpl pattern.", elts[n_idx]) bindings = _util.odict() n_bindings = 0 for elt, ty in zip(elts, idx.itervalues()): elt_bindings = ctx.ana_pat(elt, ty) n_elt_bindings = len(elt_bindings) bindings.update(elt_bindings) n_bindings_new = len(bindings) if n_bindings_new != n_bindings + n_elt_bindings: raise _errors.TyError("Duplicate variable in pattern.", pat) n_bindings = n_bindings_new return bindings
def init_idx(cls, idx): return _util.odict(_normalize_tuple_idx(idx))
def ana_pat_Num(self, ctx, pat): n = pat.n if not isinstance(n, (int, long)): raise _errors.TyError( "Pattern for type 'num' must be int or long.", pat) return _util.odict()
def ana_pat_Num(self, ctx, pat): return _util.odict()
def ana_pat_Num(self, ctx, pat): n = pat.n if not isinstance(n, (int, long)): raise _errors.TyError("Pattern for type 'num' must be int or long.", pat) return _util.odict()
def translate_pat_Name_constructor(self, ctx, pat, scrutinee_trans): lbl = pat.id condition = ast.Compare(left=scrutinee_trans, ops=[ast.Eq()], comparators=[ast.Str(s=lbl)]) return condition, _util.odict()
def ana_pat_Num(self, ctx, pat): if not isinstance(pat.n, (int, long, float)): raise _errors.TyError( "Complex literal cannot be used as a pattern for type 'ieee'.", pat) return _util.odict()
def init_idx(cls, idx): return _util.odict(_normalize_tuple_idx(idx))
def ana_pat_Name_constructor(cls, ctx, pat): id = pat.id if id == "NaN" or id == "Inf": return _util.odict() else: raise _errors.TyError("Invalid constructor name: " + id, pat)
def ana_pat_Num(self, ctx, pat): if not isinstance(pat.n, (int, long, float)): raise _errors.TyError( "Complex literal cannot be used as a pattern for type 'ieee'.", pat) return _util.odict()
def ana_pat_Name_constructor(cls, ctx, pat): id = pat.id if id == "NaN" or id == "Inf": return _util.odict() else: raise _errors.TyError("Invalid constructor name: " + id, pat)
def ana_pat_Num(self, ctx, pat): return _util.odict()