def from_raw_key(w_key, total_field_cnt=0): init_field_cnt = -1 auto_field_cnt = 0 auto_v = values.w_false super_key = None mutables = [] if isinstance(w_key, values.W_Symbol): name = w_key.utf8value init_field_cnt = total_field_cnt else: key = values.from_list(w_key) w_name = key[0] assert isinstance(w_name, values.W_Symbol) name = w_name.utf8value idx = 1 w_init_field_cnt = key[idx] if isinstance(w_init_field_cnt, values.W_Fixnum): init_field_cnt = w_init_field_cnt.value idx += 1 if len(key) > idx: w_auto = key[idx] if isinstance(w_auto, values.W_Cons): auto = values.from_list(w_auto) w_auto_field_cnt = auto[0] assert isinstance(w_auto_field_cnt, values.W_Fixnum) auto_field_cnt = w_auto_field_cnt.value auto_v = auto[1] idx += 1 if len(key) > idx: v = key[idx] if isinstance(v, values_vector.W_Vector): for i in range(v.len): mutable = v.ref(i) assert isinstance(mutable, values.W_Fixnum) mutables.append(mutable.value) idx += 1 if len(key) > idx: w_super_key = values.to_list(key[idx:]) super_key = W_PrefabKey.from_raw_key(w_super_key) if init_field_cnt == -1: init_field_cnt = total_field_cnt s_key = super_key while s_key: super_name, super_init_field_cnt, super_auto_field_cnt,\ super_auto_v, super_mutables, s_key = s_key.make_key_tuple() init_field_cnt -= super_init_field_cnt return W_PrefabKey.make(name, init_field_cnt, auto_field_cnt, auto_v, mutables, super_key)
def is_prefab_key(v): if isinstance(v, values.W_Symbol): return values.w_true elif isinstance(v, values.W_Cons): key = values.from_list(v) if not isinstance(key[0], values.W_Symbol): return values.w_false idx = 1 if isinstance(key[idx], values.W_Fixnum): idx += 1 else: if not isinstance(key[idx], values.W_Cons): return values.w_false idx += 1 if len(key) > idx: if not isinstance(key[idx], values.W_Cons): return values.w_false idx += 1 if len(key) > idx: if not isinstance(key[idx], values_vector.W_Vector): return values.w_false idx += 1 if len(key) > idx: return W_PrefabKey.is_prefab_key(key[idx]) return values.w_true else: return values.w_false
def equal_hash_args(w_prop): if isinstance(w_prop, values_vector.W_Vector): return w_prop.ref(0), w_prop.ref(1), w_prop.ref(2) if isinstance(w_prop, values.W_List): lst = values.from_list(w_prop) assert len(lst) >= 3 return lst[0], lst[1], lst[2] raise SchemeException("invalid prop:equal+hash arg " + w_prop.tostring())
def append(lists): if not lists: return values.w_null lists, acc = lists[:-1], lists[-1] while lists: vals = values.from_list(lists.pop()) acc = values.to_improper(vals, acc) return acc
def from_assocs(assocs, fname): lsts = values.from_list(assocs) keys = [None] * len(lsts) vals = [None] * len(lsts) for i, lst in enumerate(lsts): if not isinstance(lst, values.W_Cons): raise SchemeException("%s: expected list of pairs" % fname) keys[i], vals[i] = lst.car(), lst.cdr() return keys, vals
def is_struct_info(v): if isinstance(v, values.W_Cons): struct_info = values.from_list(v) if len(struct_info) == 6: if not isinstance(struct_info[0], values_struct.W_StructType) and\ struct_info[0] is not values.w_false: return False if not isinstance(struct_info[1], values_struct.W_StructConstructor) and\ struct_info[1] is not values.w_false: return False if not isinstance(struct_info[2], values_struct.W_StructPredicate) and\ struct_info[2] is not values.w_false: return False accessors = struct_info[3] if isinstance(accessors, values.W_Cons): for accessor in values.from_list(accessors): if not isinstance(accessor, values_struct.W_StructFieldAccessor): if accessor is not values.w_false and\ accessor is values.from_list(accessors)[-1]: return False else: return False mutators = struct_info[4] if isinstance(mutators, values.W_Cons): for mutator in values.from_list(mutators): if not isinstance(mutator, values_struct.W_StructFieldAccessor): if mutator is not values.w_false and\ mutator is values.from_list(mutators)[-1]: return False else: return False if not isinstance(struct_info[5], values_struct.W_StructType) and\ not isinstance(struct_info[5], values.W_Bool): return False return True return False elif isinstance(v, values.W_Prim): if v.name == "make-struct-info": return True # TODO: it can be also: # 1. a structure with the prop:struct-info property # 2. a structure type derived from struct:struct-info or # with prop:struct-info and wrapped with make-set!-transformer return False
def make_hasheqv(pairs): lsts = values.from_list(pairs) keys = [] vals = [] for lst in lsts: if not isinstance(lst, values.W_Cons): raise SchemeException("make-hash: expected list of pairs") keys.append(lst.car()) vals.append(lst.cdr()) return make_simple_table(W_EqvHashTable, keys, vals)
def list_tail(lst, pos): start_pos = pos.value if start_pos == 0: return lst else: if isinstance(lst, values.W_Cons): assert start_pos > 0 return values.to_list(values.from_list(lst)[start_pos:]) else: return values.w_null
def make_immutable_hasheq(assocs): pairs = values.from_list(assocs) keys = [None] * len(pairs) vals = [None] * len(pairs) for i, pair in enumerate(pairs): if not isinstance(pair, values.W_Cons): raise SchemeException("make-immutable-hasheq: expected list of pairs") keys[i] = pair.car() vals[i] = pair.cdr() return make_simple_table(W_EqHashTable, keys, vals, immutable=True)
def make_immutable_hash(assocs): # FIXME: Not annotated as immutable lsts = values.from_list(assocs) keys = [] vals = [] for lst in lsts: if not isinstance(lst, values.W_Cons): raise SchemeException("make-hash: expected list of pairs") keys.append(lst.car()) vals.append(lst.cdr()) return W_EqualHashTable(keys, vals)
def make_immutable_hasheq(assocs): pairs = values.from_list(assocs) keys = [None] * len(pairs) vals = [None] * len(pairs) for i, pair in enumerate(pairs): if not isinstance(pair, values.W_Cons): raise SchemeException( "make-immutable-hasheq: expected list of pairs") keys[i] = pair.car() vals[i] = pair.cdr() return make_simple_table(W_EqHashTable, keys, vals, immutable=True)
def list_to_bytes(w_list): l = values.from_list(w_list) ll = [' '] * len(l) for (i,x) in enumerate(l): if not isinstance(x, values.W_Fixnum): raise SchemeException("list->bytes: expected fixnum, got %s" % x) if x.value < 0 or x.value >= 256: raise SchemeException( "list->bytes: expected number between 0 and 255, got %s" % x) ll[i] = chr(x.value) return values.W_MutableBytes(ll)
def list_tail(lst, pos): start_pos = pos.value if start_pos == 0: return lst else: if isinstance(lst, values.W_Cons): assert start_pos > 0 # XXX inefficient return values.to_list(values.from_list(lst)[start_pos:]) else: return values.w_null
def list_to_bytes(w_list): l = values.from_list(w_list) ll = [' '] * len(l) for (i, x) in enumerate(l): if not isinstance(x, values.W_Fixnum): raise SchemeException("list->bytes: expected fixnum, got %s" % x) if x.value < 0 or x.value >= 256: raise SchemeException( "list->bytes: expected number between 0 and 255, got %s" % x) ll[i] = chr(x.value) return values.W_MutableBytes(ll)
def do_is_procedure_arity(n): if isinstance(n, values.W_Fixnum): if n.value >= 0: return values.w_true elif isinstance(n, values_struct.W_RootStruct) and\ n.struct_type().name == "arity-at-least": return values.w_true elif isinstance(n, values.W_List): for item in values.from_list(n): if not (isinstance(item, values.W_Fixnum) or\ (isinstance(item, values_struct.W_RootStruct) and\ item.struct_type().name == "arity-at-least")): return values.w_false return values.w_true return values.w_false
def apply(args, env, cont): if not args: raise SchemeException("apply expected at least one argument, got 0") fn = args[0] if not fn.iscallable(): raise SchemeException("apply expected a procedure, got something else") lst = args[-1] if not listp_loop(lst): raise SchemeException( "apply expected a list as the last argument, got something else") args_len = len(args)-1 assert args_len >= 0 others = args[1:args_len] new_args = others + values.from_list(lst) return fn.call(new_args, env, cont)
def is_module_path(v): if isinstance(v, values.W_Symbol): # FIXME: not always right return True if isinstance(v, values.W_Path): return True if isinstance(v, values_string.W_String): return True if isinstance(v, values.W_List): vs = values.from_list(v) for p in vs: if not is_module_path(p): return False return True # FIXME return False
def apply(args, env, cont, extra_call_info): if not args: raise SchemeException("apply expected at least one argument, got 0") fn = args[0] if not fn.iscallable(): raise SchemeException("apply expected a procedure, got something else") lst = args[-1] try: rest = values.from_list(lst) except SchemeException: raise SchemeException( "apply expected a list as the last argument, got something else") args_len = len(args)-1 assert args_len >= 0 others = args[1:args_len] new_args = others + rest return fn.call_with_extra_info(new_args, env, cont, extra_call_info)
def apply(args, env, cont, extra_call_info): if not args: raise SchemeException("apply expected at least one argument, got 0") fn = args[0] if not fn.iscallable(): raise SchemeException("apply expected a procedure, got something else") lst = args[-1] try: rest = values.from_list(lst) except SchemeException: raise SchemeException( "apply expected a list as the last argument, got something else") args_len = len(args) - 1 assert args_len >= 0 others = args[1:args_len] new_args = others + rest return fn.call_with_extra_info(new_args, env, cont, extra_call_info)
def from_raw_params(w_name, w_init_field_cnt, w_auto_field_cnt, auto_v, w_immutables, super_type): assert isinstance(w_name, values.W_Symbol) name = w_name.utf8value assert isinstance(w_init_field_cnt, values.W_Fixnum) init_field_cnt = w_init_field_cnt.value assert isinstance(w_auto_field_cnt, values.W_Fixnum) auto_field_cnt = w_auto_field_cnt.value mutables = [] prev_idx = 1 for i in values.from_list(w_immutables): assert isinstance(i, values.W_Fixnum) for j in range(prev_idx, i.value): mutables.append(j) prev_idx = i.value + 1 super_key = W_PrefabKey.from_struct_type(super_type) if\ super_type is not values.w_false else None return W_PrefabKey.make(name, init_field_cnt, auto_field_cnt, auto_v, mutables, super_key)
def initialize_props(self, props, proc_spec, env, cont): """ Properties initialization contains few steps: 1. call initialize_prop for each property from the input list, it extracts all super values and stores them into props array with a flat structure 2. recursively call attach_prop for each property from props and prepare the value: * if the current property has a subproperty, the value is the result of calling value procedure with a sub value as an argument * if the current property has a guard, the value is the result of calling guard with a value and struct type info as arguments * in other case, just keep the current value """ proplist = values.from_list(props) props = [] for p in proplist: self.initialize_prop(props, p) if proc_spec is not values.w_false: self.initialize_prop(props, values.W_Cons.make(w_prop_procedure, proc_spec)) return self.attach_prop(props, 0, False, env, cont)
def is_prefab_key(v): if isinstance(v, values.W_Symbol): return values.w_true elif isinstance(v, values.W_Cons): key = values.from_list(v) if not isinstance(key[0], values.W_Symbol): return values.w_false idx = 1 if isinstance(key[idx], values.W_Fixnum): idx += 1 if len(key) > idx: if isinstance(key[idx], values.W_Cons): idx += 1 if len(key) > idx: if isinstance(key[idx], values_vector.W_Vector): idx += 1 if len(key) > idx: w_super_key = values.to_list(key[idx:]) return W_PrefabKey.is_prefab_key(w_super_key) return values.W_Bool.make(len(key) == idx) else: return values.w_false
def __init__(self, name, super_type, init_field_cnt, auto_field_cnt, auto_v, inspector, proc_spec, immutables, guard, constr_name): self.name = name.utf8value self.super = super_type self.init_field_cnt = init_field_cnt.value self.auto_field_cnt = auto_field_cnt.value self.total_field_cnt = self.init_field_cnt + self.auto_field_cnt + \ (super_type.total_field_cnt if isinstance(super_type, W_StructType) else 0) self.auto_v = auto_v self.props = [] self.prop_procedure = None self.procedure_source = None self.inspector = inspector imm = [] if isinstance(proc_spec, values.W_Fixnum): imm.append(proc_spec.value) for i in values.from_list(immutables): assert isinstance(i, values.W_Fixnum) imm.append(i.value) self.immutables = imm[:] self.guard = guard self.auto_values = [self.auto_v] * self.auto_field_cnt self.isprefab = inspector is PREFAB if self.isprefab: self.isopaque = False else: self.isopaque = self.inspector is not values.w_false self.calculate_offsets() constr_name = (constr_name.utf8value if isinstance(constr_name, values.W_Symbol) else "make-" + self.name) self.constr = W_StructConstructor(self, constr_name) self.predicate = W_StructPredicate(self) self.accessor = W_StructAccessor(self) self.mutator = W_StructMutator(self)
def apply(args, env, cont, extra_call_info): if not args: raise SchemeException("apply expected at least one argument, got 0") fn = args[0] if not fn.iscallable(): raise SchemeException("apply expected a procedure, got something else") lst = args[-1] try: fn_arity = fn.get_arity() if fn_arity is Arity.unknown or fn_arity.at_least == -1: unroll_to = 1 elif fn_arity.arity_list: unroll_to = fn_arity.arity_list[-1] - (len(args) - 2) else: unroll_to = 1 rest = values.from_list(lst, unroll_to=unroll_to) except SchemeException: raise SchemeException( "apply expected a list as the last argument, got something else") args_len = len(args) - 1 assert args_len >= 0 others = args[1:args_len] new_args = others + rest return fn.call_with_extra_info(new_args, env, cont, extra_call_info)
def __init__(self, name, guard, supers=values.w_null, can_imp=False): self.name = name.utf8value self.guard = guard self.supers = values.from_list(supers) self.can_imp = can_imp
def time_apply(a, args, env, cont): initial = time.clock() return a.call(values.from_list(args), env, time_apply_cont(initial, env, cont))
def time_apply(a, args, env, cont, extra_call_info): initial = time.clock() return a.call_with_extra_info(values.from_list(args), env, time_apply_cont(initial, env, cont), extra_call_info)
def list2vector(l): return values_vector.W_Vector.fromelements(values.from_list(l))
def list_ref(lst, pos): # XXX inefficient return values.from_list(lst)[pos.value]
def list_to_string(w_list): l = values.from_list(w_list) return string(l)
def run_values(p, stdlib=False): e = "(call-with-values (lambda () %s) list)" % p v = run_mod_expr(e, stdlib=stdlib) return values.from_list(v)
def run_values(p, stdlib=False): e = "(call-with-values (lambda () %s) list)"%p v = run_mod_expr(e, stdlib=stdlib) return values.from_list(v)
def list_ref(lst, pos): return values.from_list(lst)[pos.value]
def continuation_mark_set_to_list_star(mark_set, key_list, none_v, prompt_tag): cont = mark_set.cont keys = values.from_list(key_list) return get_marks_all(cont, keys, none_v, upto=[prompt_tag])