def _make_arg_unwrapper(func, argstypes, funcname, has_self=False, simple=False): argtype_tuples = [] min_arg = 0 isdefault = False for i, typ in enumerate(argstypes): default_value = None if isinstance(typ, default): isdefault = True default_value = typ.default typ = typ.typ else: assert not isdefault, "non-default argument %s after default argument" % typ min_arg += 1 unwrapper, errorname = typ.make_unwrapper() type_errormsg = "expected %s as argument %s to %s, got " % ( errorname, i, funcname) argtype_tuples.append((i, unwrapper, isdefault, default_value, type_errormsg)) unroll_argtypes = unroll.unrolling_iterable(argtype_tuples) max_arity = len(argstypes) if min_arg == max_arity: aritystring = max_arity else: aritystring = "%s to %s" % (min_arg, max_arity) errormsg_arity = "expected %s arguments to %s, got " % ( aritystring, funcname) if min_arg == max_arity and not has_self and min_arg in (1, 2) and simple: func_arg_unwrap, call1, call2 = make_direct_arg_unwrapper( func, min_arg, unroll_argtypes, errormsg_arity) else: func_arg_unwrap = make_list_arg_unwrapper( func, has_self, min_arg, max_arity, unroll_argtypes, errormsg_arity) call1 = call2 = None _arity = Arity(range(min_arg, max_arity+1), -1) return func_arg_unwrap, _arity, call1, call2
def _generate_methods(self): """ Generate constructor, predicate, mutator, and accessor """ count = self.total_init_field_cnt self.constructor_arity = Arity([count], -1) self.constructor = W_StructConstructor(self) self.predicate = W_StructPredicate(self) self.accessor = W_StructAccessor(self) self.mutator = W_StructMutator(self)
def _generate_methods(self, constr_name): """ Generate constructor, predicate, mutator, and accessor """ if isinstance(constr_name, values.W_Symbol): constr_name = constr_name.utf8value else: constr_name = "make-" + self.name.utf8value count = self.total_init_field_cnt self.constructor_arity = Arity([count], -1) self.constructor = W_StructConstructor(self, constr_name) self.predicate = W_StructPredicate(self) self.accessor = W_StructAccessor(self) self.mutator = W_StructMutator(self)
def get_arity(self): if self.iscallable(): typ = self.struct_type() proc = typ.prop_procedure if isinstance(proc, values.W_Fixnum): offset = typ.get_offset(typ.procedure_source) proc = self._ref(proc.value + offset) return proc.get_arity() else: # -1 for the self argument arity = proc.get_arity() ls = [-1] * len(arity.arity_list) for i, val in enumerate(arity.arity_list): ls[i] = val - 1 at_least = arity.at_least if arity.at_least != -1: at_least -= 1 return Arity([val for val in ls if val != -1], at_least) else: raise SchemeException("%s does not have arity" % self.tostring())