Exemple #1
0
def regexp_match(w_re, w_str, inp_start, inp_end, output_port, prefix):
    start = inp_start.value
    if inp_end is values.w_false:
        end = sys.maxint
    elif isinstance(inp_end, values.W_Fixnum):
        end = inp_end.value
    else:
        raise SchemeException("regexp-match: expected fixnum or #f for argument 3")
    assert output_port is values.w_false, "output port not supported yet"
    result = match(w_re, w_str, start, end)
    if result is None:
        return values.w_false
    elif (isinstance(w_str, values_string.W_String) or \
          isinstance(w_str, values.W_StringInputPort)) \
        and \
         (isinstance(w_re, values_regex.W_PRegexp) or \
          isinstance(w_re, values_regex.W_Regexp) or \
          isinstance(w_re, values_string.W_String)):
        return values.to_list([values_string.W_String.fromstr_utf8(r)
                               if r else values.w_false
                               for r in result])
    else:
        return values.to_list([values.W_Bytes.from_string(r)
                               if r else values.w_false
                               for r in result])
Exemple #2
0
def regexp_match(w_re, w_str, inp_start, inp_end, output_port, prefix):
    start = inp_start.value
    if inp_end is values.w_false:
        end = sys.maxint
    elif isinstance(inp_end, values.W_Fixnum):
        end = inp_end.value
    else:
        raise SchemeException("regexp-match: expected fixnum or #f for argument 3")
    assert output_port is values.w_false, "output port not supported yet"
    result = match(w_re, w_str, start, end)
    if result is None:
        return values.w_false
    elif (isinstance(w_str, values_string.W_String) or \
          isinstance(w_str, values.W_StringInputPort)) \
        and \
         (isinstance(w_re, values_regex.W_PRegexp) or \
          isinstance(w_re, values_regex.W_Regexp) or \
          isinstance(w_re, values_string.W_String)):
        return values.to_list([values_string.W_String.fromstr_utf8(r)
                               if r else values.w_false
                               for r in result])
    else:
        return values.to_list([values.W_Bytes.from_string(r)
                               if r else values.w_false
                               for r in result])
Exemple #3
0
def make_known_char_range_list(args):
    all_known = [None] * LEN_CHAR_RANGES
    for i, (start, end, same_props) in enumerate(known_char_ranges):
        all_known[i] = to_list(
            [W_Fixnum(start),
             W_Fixnum(end),
             W_Bool.make(same_props)])
    return to_list(all_known)
Exemple #4
0
def cms_context(marks):
    from pycket.values_string import W_String
    # TODO: Pycket does not have a mark to denote context. We need to fix that.

    k = marks.cont
    n = 0
    # find out the length
    while isinstance(k, Cont):
        if is_ast_cont_with_surrounding_lambda(k):
            n += 1
        k = k.get_previous_continuation()

    # second traversal saves us from reversing it later
    ls = [None] * n
    k = marks.cont
    i = n - 1
    while isinstance(k, Cont):
        if is_ast_cont_with_surrounding_lambda(k):
            surrounding_lam = k.get_ast().surrounding_lambda
            lam_str = W_String.make(surrounding_lam.tostring())
            ls[i] = values.W_Cons.make(lam_str, values.w_false)
            i -= 1
        k = k.get_previous_continuation()

    return values.to_list(ls)
Exemple #5
0
 def attach_prop(self, props, idx, is_checked, env, cont):
     from pycket.interpreter import return_multi_vals
     if idx < len(props):
         (prop, prop_val, sub_prop) = props[idx]
         if sub_prop is not None:
             for p in props:
                 if p[0] is sub_prop:
                     return prop_val.call([p[1]], env,
                         self.save_prop_value(props, idx, False, env, cont))
         assert isinstance(prop, W_StructProperty)
         if not is_checked and prop.guard.iscallable():
             return prop.guard.call([prop_val, values.to_list(self.struct_type_info())],
                 env, self.save_prop_value(props, idx, True, env, cont))
         if prop.isinstance(w_prop_procedure):
             self.prop_procedure = prop_val
         self.props.append((prop, prop_val))
         return self.attach_prop(props, idx + 1, False, env, cont)
     # at this point all properties are saved, next step is to copy
     # propertyes from super types
     struct_type = self.super
     while isinstance(struct_type, W_StructType):
         self.props = self.props + struct_type.props
         if not self.prop_procedure and struct_type.prop_procedure:
             self.prop_procedure = struct_type.prop_procedure
             self.procedure_source = struct_type.procedure_source
         struct_type = struct_type.super
     struct_tuple = self.make_struct_tuple()
     return return_multi_vals(values.Values.make(struct_tuple), env, cont)
Exemple #6
0
def cms_context(marks):
    from pycket.values_string import W_String
    # TODO: Pycket does not have a mark to denote context. We need to fix that.

    k = marks.cont
    n = 0
    # find out the length
    while isinstance(k, Cont):
        if is_ast_cont_with_surrounding_lambda(k):
            n += 1
        k = k.get_previous_continuation()

    # second traversal saves us from reversing it later
    ls = [None]*n
    k = marks.cont
    i = n-1
    while isinstance(k, Cont):
        if is_ast_cont_with_surrounding_lambda(k):
            surrounding_lam = k.get_ast().surrounding_lambda
            lam_str = W_String.make(surrounding_lam.tostring())
            ls[i] = values.W_Cons.make(lam_str, values.w_false)
            i -= 1
        k = k.get_previous_continuation()

    return values.to_list(ls)
Exemple #7
0
 def attach_prop(self, props, idx, is_checked, env, cont):
     from pycket.interpreter import return_multi_vals
     if idx < len(props):
         (prop, prop_val, sub_prop) = props[idx]
         if sub_prop is not None:
             for p in props:
                 if p[0] is sub_prop:
                     return prop_val.call([p[1]], env,
                                          self.save_prop_value(
                                              props, idx, False, env, cont))
         assert isinstance(prop, W_StructProperty)
         if not is_checked and prop.guard.iscallable():
             return prop.guard.call(
                 [prop_val,
                  values.to_list(self.struct_type_info(cont))], env,
                 self.save_prop_value(props, idx, True, env, cont))
         if prop.isinstance(w_prop_procedure):
             self.prop_procedure = prop_val
         self.props.append((prop, prop_val))
         return self.attach_prop(props, idx + 1, False, env, cont)
     # at this point all properties are saved, next step is to copy
     # propertyes from super types
     struct_type = self.super
     while isinstance(struct_type, W_StructType):
         self.props = self.props + struct_type.props
         if not self.prop_procedure and struct_type.prop_procedure:
             self.prop_procedure = struct_type.prop_procedure
             self.procedure_source = struct_type.procedure_source
         struct_type = struct_type.super
     struct_tuple = self.make_struct_tuple()
     return return_multi_vals(values.Values.make(struct_tuple), env, cont)
Exemple #8
0
def to_value(json):
    dbgprint("to_value", json)
    if json is pycket_json.json_false:
        return values.w_false
    elif json is pycket_json.json_true:
        return values.w_true
    if json.is_object:
        # The json-object should only contain one element
        obj = json.value_object()
        if "vector" in obj:
            return vector.W_Vector.fromelements(
                [to_value(v) for v in obj["vector"].value_array()],
                immutable=True)
        if "struct" in obj:
            key = to_value(obj["prefab-key"])
            fields = [to_value(v) for v in obj["struct"].value_array()]
            return values_struct.W_Struct.make_prefab(key, fields)
        if "box" in obj:
            return values.W_IBox(to_value(obj["box"]))
        if "number" in obj:
            return _to_num(obj["number"])
        if "path" in obj:
            return values.W_Path(obj["path"].value_string())
        if "char" in obj:
            return values.W_Character.make(
                unichr(int(obj["char"].value_string())))
        if "hash-keys" in obj and "hash-vals" in obj:
            return values_hash.W_EqualHashTable(
                [to_value(i) for i in obj["hash-keys"].value_array()],
                [to_value(i) for i in obj["hash-vals"].value_array()])
        if "regexp" in obj:
            return values_regex.W_Regexp(obj["regexp"].value_string())
        if "byte-regexp" in obj:
            arr = decode_byte_array(obj["byte-regexp"])
            return values_regex.W_ByteRegexp("".join(arr))
        if "pregexp" in obj:
            return values_regex.W_PRegexp(obj["pregexp"].value_string())
        if "byte-pregexp" in obj:
            arr = decode_byte_array(obj["byte-pregexp"])
            return values_regex.W_BytePRegexp("".join(arr))
        if "bytes" in obj:
            arr = decode_byte_array(obj["bytes"])
            return values.W_ImmutableBytes(arr)
        if "string" in obj:
            return values_string.W_String.make(
                str(obj["string"].value_string()))
        if "keyword" in obj:
            return values.W_Keyword.make(str(obj["keyword"].value_string()))
        if "improper" in obj:
            improper = obj["improper"].value_array()
            return values.to_improper(
                [to_value(v) for v in improper[0].value_array()],
                to_value(improper[1]))
        for i in ["toplevel", "lexical", "module", "source-name"]:
            if i in obj:
                return values.W_Symbol.make(obj[i].value_string())
    if json.is_array:
        return values.to_list([to_value(j) for j in json.value_array()])
    assert 0, "Unexpected json value: %s" % json.tostring()
Exemple #9
0
def rmp(pat, input):
    matches = match_positions(pat, input)
    xs = []
    for start, end in matches:
        s = values.W_Fixnum(start)
        e = values.W_Fixnum(end)
        xs.append(values.W_Cons.make(s, e))
    return values.to_list(xs)
Exemple #10
0
def time_apply_cont(initial, env, cont, vals):
    from pycket.interpreter import return_multi_vals
    final = time.clock()
    ms = values.W_Fixnum(int((final - initial) * 1000))
    vals_w = vals.get_all_values()
    results = values.Values.make([values.to_list(vals_w),
                                  ms, ms, values.W_Fixnum.ZERO])
    return return_multi_vals(results, env, cont)
Exemple #11
0
def time_apply_cont(initial, env, cont, vals):
    from pycket.interpreter import return_multi_vals
    final = time.clock()
    ms = values.W_Fixnum(int((final - initial) * 1000))
    vals_l = vals._get_full_list()
    results = values.Values.make([values.to_list(vals_l),
                                  ms, ms, values.W_Fixnum(0)])
    return return_multi_vals(results, env, cont)
Exemple #12
0
def rmp(pat, input):
    matches = match_positions(pat, input)
    xs = []
    for start, end in matches:
        s = values.W_Fixnum(start)
        e = values.W_Fixnum(end)
        xs.append(values.W_Cons.make(s, e))
    return values.to_list(xs)
Exemple #13
0
def regexp_match(w_re, w_str):
    result = match(w_re, w_str)
    if result is None:
        return values.w_false
    elif (isinstance(w_str, values_string.W_String) or \
          isinstance(w_str, values.W_StringInputPort)) \
        and \
         (isinstance(w_re, values_regex.W_PRegexp) or \
          isinstance(w_re, values_regex.W_Regexp) or \
          isinstance(w_re, values_string.W_String)):
        return values.to_list([values_string.W_String.fromascii(r)
                               if r else values.w_false
                               for r in result])
    else:
        return values.to_list([values.W_Bytes.from_string(r)
                               if r else values.w_false
                               for r in result])
Exemple #14
0
def to_value(json):
    dbgprint("to_value", json)
    if json is pycket_json.json_false:
        return values.w_false
    elif json is pycket_json.json_true:
        return values.w_true
    if json.is_object:
        # The json-object should only contain one element
        obj = json.value_object()
        if "vector" in obj:
            return vector.W_Vector.fromelements([to_value(v) for v in obj["vector"].value_array()], immutable=True)
        if "struct" in obj:
            key = to_value(obj["prefab-key"])
            fields = [to_value(v) for v in obj["struct"].value_array()]
            return values_struct.W_Struct.make_prefab(key, fields)
        if "box" in obj:
            return values.W_IBox(to_value(obj["box"]))
        if "number" in obj:
            return _to_num(obj["number"])
        if "path" in obj:
            return values.W_Path(obj["path"].value_string())
        if "char" in obj:
            return values.W_Character.make(unichr(int(obj["char"].value_string())))
        if "hash-keys" in obj and "hash-vals" in obj:
            return W_EqualHashTable(
                    [to_value(i) for i in obj["hash-keys"].value_array()],
                    [to_value(i) for i in obj["hash-vals"].value_array()],
                    immutable=True)
        if "regexp" in obj:
            return values_regex.W_Regexp(obj["regexp"].value_string())
        if "byte-regexp" in obj:
            arr = decode_byte_array(obj["byte-regexp"])
            return values_regex.W_ByteRegexp("".join(arr))
        if "pregexp" in obj:
            return values_regex.W_PRegexp(obj["pregexp"].value_string())
        if "byte-pregexp" in obj:
            arr = decode_byte_array(obj["byte-pregexp"])
            return values_regex.W_BytePRegexp("".join(arr))
        if "bytes" in obj:
            arr = decode_byte_array(obj["bytes"])
            return values.W_ImmutableBytes(arr)
        if "string" in obj:
            return values_string.W_String.make(str(obj["string"].value_string()))
        if "keyword" in obj:
            return values.W_Keyword.make(str(obj["keyword"].value_string()))
        if "improper" in obj:
            improper = obj["improper"].value_array()
            return values.to_improper([to_value(v) for v in improper[0].value_array()], to_value(improper[1]))
        if "void" in obj:
            return values.w_void
        for i in ["lexical", "module", "source-name", "toplevel"]:
            if i in obj:
                return values.W_Symbol.make(obj[i].value_string())
    if json.is_array:
        return values.to_list([to_value(j) for j in json.value_array()])
    assert 0, "Unexpected json value: %s" % json.tostring()
Exemple #15
0
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
Exemple #16
0
def regexp_match(w_re, w_str):
    result = match(w_re, w_str)
    if result is None:
        return values.w_false
    elif (isinstance(w_str, values_string.W_String) or \
          isinstance(w_str, values.W_StringInputPort)) \
        and \
         (isinstance(w_re, values_regex.W_PRegexp) or \
          isinstance(w_re, values_regex.W_Regexp) or \
          isinstance(w_re, values_string.W_String)):
        return values.to_list([
            values_string.W_String.fromascii(r) if r else values.w_false
            for r in result
        ])
    else:
        return values.to_list([
            values.W_Bytes.from_string(r) if r else values.w_false
            for r in result
        ])
Exemple #17
0
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
Exemple #18
0
def do_procedure_arity(proc):
    result = []
    (ls, at_least) = proc.get_arity()
    for item in ls:
        result.append(values.W_Fixnum(item))
    if at_least != -1:
        result.append(values_struct.W_Struct.make([values.W_Fixnum(at_least)],\
            arity_at_least))
    if len(result) == 1:
        return result[0]
    return values.to_list(result[:])
Exemple #19
0
def do_procedure_arity(proc, env, cont):
    from pycket.interpreter import return_value
    result = []
    arity = proc.get_arity()
    for item in arity.arity_list:
        result.append(values.W_Fixnum(item))
    if arity.at_least != -1:
        val = [values.W_Fixnum(arity.at_least)]
        return arity_at_least.constr.call(val, env, proc_arity_cont(result, env, cont))
    if len(result) == 1:
        return return_value(result[0], env, cont)
    return return_value(values.to_list(result[:]), env, cont)
Exemple #20
0
def do_procedure_arity(proc, env, cont):
    from pycket.interpreter import return_value
    result = []
    arity = proc.get_arity()
    for item in arity.arity_list:
        result.append(values.W_Fixnum(item))
    if arity.at_least != -1:
        val = [values.W_Fixnum(arity.at_least)]
        return arity_at_least.constr.call(val, env,
                                          proc_arity_cont(result, env, cont))
    if len(result) == 1:
        return return_value(result[0], env, cont)
    return return_value(values.to_list(result[:]), env, cont)
Exemple #21
0
 def struct_type_info(self):
     name = values.W_Symbol.make(self.name)
     init_field_cnt = values.W_Fixnum(self.init_field_cnt)
     auto_field_cnt = values.W_Fixnum(self.auto_field_cnt)
     immutable_k_list = values.to_list([values.W_Fixnum(i) for i in self.immutables])
     # TODO: value of the super variable should be a structure type descriptor
     # for the most specific ancestor of the type that is controlled by the current inspector,
     # or #f if no ancestor is controlled by the current inspector
     super = self.super
     # TODO: #f if the seventh result is the most specific ancestor type or
     # if the type has no supertype, #t otherwise
     skipped = values.w_false
     return [name, init_field_cnt, auto_field_cnt, self.acc, self.mut,
             immutable_k_list, super, skipped]
Exemple #22
0
 def key(self):
     key = []
     key.append(values.W_Symbol.make(self.name))
     key.append(values.W_Fixnum.make(self.init_field_cnt))
     if self.auto_field_cnt > 0:
         key.append(values.to_list(
             [values.W_Fixnum.make(self.auto_field_cnt), self.auto_v]))
     mutables = []
     for i in self.mutables:
         mutables.append(values.W_Fixnum.make(i))
     if mutables:
         key.append(values_vector.W_Vector.fromelements(mutables))
     if self.super_key:
         key.extend(self.super_key.key())
     return key
Exemple #23
0
 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)
Exemple #24
0
 def struct_type_info(self):
     name = values.W_Symbol.make(self.name)
     init_field_cnt = values.W_Fixnum.make(self.init_field_cnt)
     auto_field_cnt = values.W_Fixnum.make(self.auto_field_cnt)
     immutable_k_list = values.to_list(
         [values.W_Fixnum.make(i) for i in self.immutables])
     super = values.w_false
     typ = self.super
     while isinstance(typ, W_StructType):
         if current_inspector.has_control(typ):
             super = typ
         typ = typ.super
     skipped = values.W_Bool.make(super is values.w_false and
         isinstance(self.super, W_StructType))
     return [name, init_field_cnt, auto_field_cnt, self.accessor,
             self.mutator, immutable_k_list, super, skipped]
Exemple #25
0
 def make_prefab(prefab_key):
     if prefab_key in W_StructType.unbound_prefab_types:
         w_struct_type = W_StructType.unbound_prefab_types[prefab_key]
     else:
         name, init_field_cnt, auto_field_cnt, auto_v, mutables, super_key =\
             prefab_key.make_key_tuple()
         super_type = W_StructType.make_prefab(super_key) if super_key else\
             values.w_false
         immutables = []
         for i in range(init_field_cnt):
             if i not in mutables:
                 immutables.append(values.W_Fixnum.make(i))
         w_struct_type = W_StructType.make_simple(values.W_Symbol.make(name),
             super_type, values.W_Fixnum.make(init_field_cnt),
             values.W_Fixnum.make(auto_field_cnt), auto_v, values.w_null,
             PREFAB, values.w_false, values.to_list(immutables))
         W_StructType.unbound_prefab_types[prefab_key] = w_struct_type
     return w_struct_type
Exemple #26
0
def define_struct(name, super=values.w_null, fields=[]):
    immutables = []
    for i in range(len(fields)):
        immutables.append(values.W_Fixnum(i))
    struct_type, struct_constr, struct_pred, struct_acc, struct_mut = \
        values_struct.W_StructType.make_simple(values.W_Symbol.make(name),
            super, values.W_Fixnum(len(fields)), values.W_Fixnum(0),
            values.w_false, values.w_null, values.w_false, values.w_false,
            values.to_list(immutables)).make_struct_tuple()
    expose_val("struct:" + name, struct_type)
    expose_val(name, struct_constr)
    # this is almost always also provided
    expose_val("make-" + name, struct_constr)
    expose_val(name + "?", struct_pred)
    for field, field_name in enumerate(fields):
        w_num = values.W_Fixnum(field)
        w_name = values.W_Symbol.make(field_name)
        acc = values_struct.W_StructFieldAccessor(struct_acc, w_num, w_name)
        expose_val(name + "-" + field_name, acc)
    return struct_type
Exemple #27
0
def define_struct(name, super=values.w_null, fields=[]):
    immutables = []
    for i in range(len(fields)):
        immutables.append(values.W_Fixnum(i))
    struct_type, struct_constr, struct_pred, struct_acc, struct_mut = \
        values_struct.W_StructType.make_simple(values.W_Symbol.make(name),
            super, values.W_Fixnum(len(fields)), values.W_Fixnum(0),
            values.w_false, values.w_null, values.w_false, values.w_false,
            values.to_list(immutables)).make_struct_tuple()
    expose_val("struct:" + name, struct_type)
    expose_val(name, struct_constr)
    # this is almost always also provided
    expose_val("make-" + name, struct_constr)
    expose_val(name + "?", struct_pred)
    for field, field_name in enumerate(fields):
        w_num = values.W_Fixnum(field)
        w_name =  values.W_Symbol.make(field_name)
        acc = values_struct.W_StructFieldAccessor(struct_acc, w_num, w_name)
        expose_val(name + "-" + field_name, acc)
    return struct_type
Exemple #28
0
 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
Exemple #29
0
 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
Exemple #30
0
def proc_arity_cont(result, env, cont, _vals):
    from pycket.interpreter import check_one_val, return_value
    result.append(check_one_val(_vals))
    if len(result) == 1:
        return return_value(result[0], env, cont)
    return return_value(values.to_list(result[:]), env, cont)
Exemple #31
0
def do_list(args):
    return values.to_list(args)
Exemple #32
0
def vector2list(v):
    es = []
    for i in range(v.len):
        es.append(v.ref(i))
    return values.to_list(es)
Exemple #33
0
def dir_list(w_str):
    s = extract_path(w_str)
    dir = [values.W_Path(p) for p in os.listdir(s)]
    return values.to_list(dir)
Exemple #34
0
def proc_arity_cont(result, env, cont, _vals):
    from pycket.interpreter import check_one_val, return_value
    result.append(check_one_val(_vals))
    if len(result) == 1:
        return return_value(result[0], env, cont)
    return return_value(values.to_list(result[:]), env, cont)
Exemple #35
0
def explode_path(w_path):
    sep = os.sep
    path = extract_path(w_path)
    parts = [_explode_element(p) for p in path.split(sep)]
    return values.to_list(parts)
Exemple #36
0
def eval(linkl, target, imports=[], just_return=False):
    result = instantiate_linklet.call_interpret(
        [linkl, to_list(imports), target, w_false], get_testing_config())
    return result, target
Exemple #37
0
def dir_list(w_str):
    s = extract_path(w_str)
    dir = [values.W_Path(p) for p in os.listdir(s)]
    return values.to_list(dir)
Exemple #38
0
def explode_path(w_path):
    sep = os.sep
    path = extract_path(w_path)
    parts = [_explode_element(p) for p in path.split(sep)]
    return values.to_list(parts)
Exemple #39
0
 def short_key(self):
     key = self.key()
     short_key = key[:1] + key[2:]
     return values.to_list(short_key) if len(short_key) > 1 else key[0]
Exemple #40
0
def ast_to_sexp(form):
    from pycket.prims.linklet import W_Linklet, W_LinkletBundle, W_LinkletDirectory

    #util.console_log("ast->sexp is called with form : %s" % form.tostring(), 8)

    if is_val_type(form, extra=[vector.W_Vector, base.W_HashTable, values.W_List, values.W_Symbol]):
        return form
    elif isinstance(form, W_Linklet):
        name = form.name # W_Symbol
        importss = form.importss # [[Import ...] ...]
        exports = form.exports # {int_id:Export ...}
        body_forms = form.forms # rlist of ASTs

        # The AST contains gensymed references to all the variables
        # the linklet closes on, so we need to serialize all the
        # information we have in Import and Export objects to recreate
        # the same gensymed bindings at the instantiation of the
        # deserialized linklet.
        # So it will look like this when serialized:
        #
        # (linklet name (((Import grp gen_id int_id ext_id) ...) ...) ((Export int_id gen_int_id ext_id) ...) body)

        importss_rlist = [None]*len(importss)
        for index, imp_group in enumerate(importss):
            len_group = len(imp_group)
            importss_inst = [None]*len_group
            for i, imp_obj in enumerate(imp_group):
                importss_inst[i] = values.to_list([import_sym, imp_obj.group, imp_obj.id, imp_obj.int_id, imp_obj.ext_id])
            importss_rlist[index] = values.to_list(importss_inst)
        importss_list = values.to_list(importss_rlist)

        exports_rlist = [None]*len(exports)
        i = 0
        for k, exp_obj in exports.iteritems():
            exports_rlist[i] = values.to_list([export_sym, k, exp_obj.int_id, exp_obj.ext_id])
            i += 1

        exports_list = values.to_list(exports_rlist)

        body_forms_rlist = [None]*len(body_forms)
        for index, ast_form in enumerate(body_forms):
            body_forms_rlist[index] = ast_form.to_sexp()

        linklet_rlist = [linklet_sym, name, importss_list, exports_list] + body_forms_rlist
        linklet_s_exp = values.to_list(linklet_rlist)

        return linklet_s_exp
    elif isinstance(form, W_LinkletBundle) or isinstance(form, W_LinkletDirectory):
        bd_sym = None
        if isinstance(form, W_LinkletBundle):
            bd_sym = mksym(":B:")
        else:
            bd_sym = mksym(":D:")

        mapping = form.get_mapping()
        l = mapping.length()
        keys = [None]*l
        vals = [None]*l

        if isinstance(mapping, equal.W_EqualHashTable):
            i = 0
            for k, v in mapping.hash_items():
                keys[i] = k
                vals[i] = ast_to_sexp(v)
                i += 1

            return values.W_Cons.make(bd_sym, equal.W_EqualHashTable(keys, vals, immutable=True))
        elif isinstance(mapping, simple.W_EqImmutableHashTable):
            i = 0
            for k, v in mapping.iteritems():
                keys[i] = k
                vals[i] = ast_to_sexp(v)
                i += 1

            return values.W_Cons.make(bd_sym, simple.make_simple_immutable_table(simple.W_EqImmutableHashTable, keys, vals))
        else:
            raise SchemeException("Something wrong with the bundle/directory mapping : %s" % mapping.tostring())
    else:
        return form.to_sexp()
Exemple #41
0
def ast_to_sexp(form):
    from pycket.prims.linklet import W_Linklet, W_LinkletBundle, W_LinkletDirectory

    #util.console_log("ast->sexp is called with form : %s" % form.tostring(), 8)

    if is_val_type(form,
                   extra=[
                       vector.W_Vector, base.W_HashTable, values.W_List,
                       values.W_Symbol
                   ]):
        return form
    elif isinstance(form, W_Linklet):
        name = form.name  # W_Symbol
        importss = form.importss  # [[Import ...] ...]
        exports = form.exports  # {int_id:Export ...}
        body_forms = form.forms  # rlist of ASTs

        # The AST contains gensymed references to all the variables
        # the linklet closes on, so we need to serialize all the
        # information we have in Import and Export objects to recreate
        # the same gensymed bindings at the instantiation of the
        # deserialized linklet.
        # So it will look like this when serialized:
        #
        # (linklet name (((Import grp gen_id int_id ext_id) ...) ...) ((Export int_id gen_int_id ext_id) ...) body)

        importss_rlist = [None] * len(importss)
        for index, imp_group in enumerate(importss):
            len_group = len(imp_group)
            importss_inst = [None] * len_group
            for i, imp_obj in enumerate(imp_group):
                importss_inst[i] = values.to_list([
                    import_sym, imp_obj.group, imp_obj.id, imp_obj.int_id,
                    imp_obj.ext_id
                ])
            importss_rlist[index] = values.to_list(importss_inst)
        importss_list = values.to_list(importss_rlist)

        exports_rlist = [None] * len(exports)
        i = 0
        for k, exp_obj in exports.iteritems():
            exports_rlist[i] = values.to_list(
                [export_sym, k, exp_obj.int_id, exp_obj.ext_id])
            i += 1

        exports_list = values.to_list(exports_rlist)

        body_forms_rlist = [None] * len(body_forms)
        for index, ast_form in enumerate(body_forms):
            body_forms_rlist[index] = ast_form.to_sexp()

        linklet_rlist = [linklet_sym, name, importss_list, exports_list
                         ] + body_forms_rlist
        linklet_s_exp = values.to_list(linklet_rlist)

        return linklet_s_exp
    elif isinstance(form, W_LinkletBundle) or isinstance(
            form, W_LinkletDirectory):
        bd_sym = None
        if isinstance(form, W_LinkletBundle):
            bd_sym = mksym(":B:")
        else:
            bd_sym = mksym(":D:")

        mapping = form.get_mapping()
        l = mapping.length()
        keys = [None] * l
        vals = [None] * l

        if isinstance(mapping, equal.W_EqualHashTable):
            i = 0
            for k, v in mapping.hash_items():
                keys[i] = k
                vals[i] = ast_to_sexp(v)
                i += 1

            return values.W_Cons.make(
                bd_sym, equal.W_EqualHashTable(keys, vals, immutable=True))
        elif isinstance(mapping, simple.W_EqImmutableHashTable):
            i = 0
            for k, v in mapping.iteritems():
                keys[i] = k
                vals[i] = ast_to_sexp(v)
                i += 1

            return values.W_Cons.make(
                bd_sym,
                simple.make_simple_immutable_table(
                    simple.W_EqImmutableHashTable, keys, vals))
        else:
            raise SchemeException(
                "Something wrong with the bundle/directory mapping : %s" %
                mapping.tostring())
    else:
        return form.to_sexp()
Exemple #42
0
 def short_key(self):
     key = self.key()
     short_key = key[:1] + key[2:]
     return values.to_list(short_key) if len(short_key) > 1 else key[0]
Exemple #43
0
def do_list(args):
    return values.to_list(args)
Exemple #44
0
def string_to_list(s):
    return values.to_list([values.W_Character(i) for i in s.as_unicode()])
Exemple #45
0
    def fasl_to_sexp_recursive(self, fasl_string, pos):
        from pycket import values as v
        from pycket.values_string import W_String
        from pycket.values_regex import W_Regexp, W_PRegexp, W_ByteRegexp, W_BytePRegexp
        from pycket.vector import W_Vector
        from pycket.values_struct import W_Struct
        from pycket.hash import simple as hash_simple
        from pycket.hash.equal import W_EqualHashTable
        from pycket.prims.numeric import float_bytes_to_real
        from pycket.prims.string import _str2num
        from rpython.rlib.rbigint import rbigint
        from pycket.prims.input_output import build_path, bytes_to_path_element
        from pycket.ast_vs_sexp import to_rpython_list

        typ, pos = self.read_byte_no_eof(fasl_string, pos)

        if typ == FASL_GRAPH_DEF_TYPE:
            position, pos = self.read_fasl_integer(fasl_string, pos)
            val, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            if position >= self.GLOBAL_SHARED_COUNT:
                raise Exception("fasl: bad graph index")
            self.SHARED[position] = val
            return val, pos
        elif typ == FASL_GRAPH_REF_TYPE:
            position, pos = self.read_fasl_integer(fasl_string, pos)
            if position >= self.GLOBAL_SHARED_COUNT:
                raise Exception("fasl: bad graph index")
            return self.SHARED[position], pos
        elif typ == FASL_FALSE_TYPE:
            return v.w_false, pos
        elif typ == FASL_TRUE_TYPE:
            return v.w_true, pos
        elif typ == FASL_NULL_TYPE:
            return v.w_null, pos
        elif typ == FASL_VOID_TYPE:
            return v.w_void, pos
        elif typ == FASL_EOF_TYPE:
            return v.eof_object, pos
        elif typ == FASL_INTEGER_TYPE:
            num, pos = self.read_fasl_integer(fasl_string, pos)
            if isinstance(num, rbigint):
                return v.W_Bignum(num), pos
            return v.W_Fixnum(num), pos
        elif typ == FASL_FLONUM_TYPE:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 8)
            return float_bytes_to_real(list(num_str), v.w_false), pos
        elif typ == FASL_SINGLE_FLONUM_TYPE:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 4)
            real = float_bytes_to_real(list(num_str), v.w_false)
            return real.arith_exact_inexact(), pos
        elif typ == FASL_EXTFLONUM_TYPE:
            bstr_len, pos = self.read_fasl_integer(fasl_string, pos)
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, bstr_len)
            return _str2num(W_String.fromstr_utf8(num_str).as_str_utf8(), 10), pos
        elif typ == FASL_RATIONAL_TYPE:
            num, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            den, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Rational.make(num, den), pos
        elif typ == FASL_COMPLEX_TYPE:
            re, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            im, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Complex.from_real_pair(re, im), pos
        elif typ == FASL_CHAR_TYPE:
            _chr, pos = self.read_fasl_integer(fasl_string, pos)
            return v.W_Character(unichr(_chr)), pos
        elif typ == FASL_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol.make(sym_str), pos
        elif typ == FASL_UNREADABLE_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol.make_unreadable(sym_str), pos
        elif typ == FASL_UNINTERNED_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol(sym_str), pos
        elif typ == FASL_KEYWORD_TYPE:
            key_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Keyword.make(key_str), pos
        elif typ == FASL_STRING_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_String.make(str_str), pos
        elif typ == FASL_IMMUTABLE_STRING_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_String.make(str_str).make_immutable(), pos
        elif typ == FASL_BYTES_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Bytes.from_string(byts, immutable=False), pos
        elif typ == FASL_IMMUTABLE_BYTES_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Bytes.from_string(byts), pos
        elif typ == FASL_PATH_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Path(byts), pos
        elif typ == FASL_RELATIVE_PATH_TYPE:
            wrt_dir = self.current_relative_dir
            p_w_lst, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            p_r_lst, _ = to_rpython_list(p_w_lst)
            rel_elems = [bytes_to_path_element(p) if isinstance(p, v.W_Bytes) else p for p in p_r_lst]
            if wrt_dir:
                return build_path([wrt_dir] + rel_elems), pos
            elif rel_elems == []:
                return build_path([v.W_Symbol.make("same")]), pos
            else:
                return build_path(rel_elems), pos
        elif typ == FASL_PREGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_PRegexp(str_str), pos
        elif typ == FASL_REGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_Regexp(str_str), pos
        elif typ == FASL_BYTE_PREGEXP:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_BytePRegexp(str_str), pos
        elif typ == FASL_BYTE_REGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_ByteRegexp(str_str), pos
        elif typ == FASL_LIST_TYPE:
            list_len, pos = self.read_fasl_integer(fasl_string, pos)
            lst, pos = self.read_multi_into_rpython_list(fasl_string, pos, list_len)
            return v.to_list(lst), pos
        elif typ == FASL_PAIR_TYPE:
            car, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            cdr, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Cons.make(car, cdr), pos
        elif typ == FASL_LIST_STAR_TYPE:
            list_len, pos = self.read_fasl_integer(fasl_string, pos)
            # list_len is the length of the proper part
            lst, pos = self.read_multi_into_rpython_list(fasl_string, pos, list_len)
            # read the last element
            return_list, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            for i in range(list_len-1, -1, -1):
                return_list = v.W_Cons.make(lst[i], return_list)
            return return_list, pos
        elif typ == FASL_VECTOR_TYPE or typ == FASL_IMMUTABLE_VECTOR_TYPE:
            vec_len, pos = self.read_fasl_integer(fasl_string, pos)
            storage, pos = self.read_multi_into_rpython_list(fasl_string, pos, vec_len)
            if typ == FASL_IMMUTABLE_VECTOR_TYPE:
                return W_Vector.fromelements(storage, immutable=True), pos
            return W_Vector.fromelements(storage), pos
        elif typ == FASL_BOX_TYPE:
            element, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_MBox(element), pos
        elif typ == FASL_IMMUTABLE_BOX_TYPE:
            element, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_IBox(element), pos
        elif typ == FASL_PREFAB_TYPE:
            key, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            vals, pos = self.read_multi_into_rpython_list(fasl_string, pos, length)
            return W_Struct.make_prefab(key, vals), pos
        elif typ == FASL_HASH_TYPE:
            variant, pos = self.read_byte_no_eof(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            keys, vals, pos = self.read_multi_double_into_rpython_list(fasl_string, pos, length)
            if variant == FASL_HASH_EQ_VARIANT:
                return hash_simple.make_simple_mutable_table(hash_simple.W_EqMutableHashTable, keys, vals), pos
            elif variant == FASL_HASH_EQV_VARIANT:
                return hash_simple.make_simple_mutable_table(hash_simple.W_EqvMutableHashTable, keys, vals), pos
            else: # variant == FASL_HASH_EQUAL_VARIANT:
                return W_EqualHashTable(keys, vals, immutable=False), pos
        elif typ == FASL_IMMUTABLE_HASH_TYPE:
            variant, pos = self.read_byte_no_eof(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            keys, vals, pos = self.read_multi_double_into_rpython_list(fasl_string, pos, length)
            if variant == FASL_HASH_EQ_VARIANT:
                return hash_simple.make_simple_immutable_table(hash_simple.W_EqImmutableHashTable, keys, vals), pos
            elif variant == FASL_HASH_EQV_VARIANT:
                return hash_simple.make_simple_immutable_table(hash_simple.W_EqvImmutableHashTable, keys, vals), pos
            else: # variant == FASL_HASH_EQUAL_VARIANT:
                return W_EqualHashTable(keys, vals, immutable=True), pos
        elif typ == FASL_SRCLOC:
            # difficult to create an instance of srcloc struct so defer that to the runtime
            source, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            line, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            column, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            position, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            span, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.to_list([v.W_Symbol.make("srcloc"), source, line, column, position, span]), pos
        else:
            if typ >= FASL_SMALL_INTEGER_START:
                return v.W_Fixnum((typ-FASL_SMALL_INTEGER_START)+FASL_LOWEST_SMALL_INTEGER), pos
            else:
                raise Exception("unrecognized fasl tag : %s" % typ)
Exemple #46
0
def inst(linkl, imports=[], target=None):
    if not target:
        target = w_false

    return instantiate_linklet.call_interpret(
        [linkl, to_list(imports), target, w_false], get_testing_config())
Exemple #47
0
def make_known_char_range_list(args):
    all_known = [None]*LEN_CHAR_RANGES
    for i, (start, end, same_props) in enumerate(known_char_ranges):
        all_known[i] = to_list([W_Fixnum(start), W_Fixnum(end), W_Bool.make(same_props)])
    return to_list(all_known)
Exemple #48
0
def vector2list(v):
    es = []
    for i in range(v.len):
        es.append(v.ref(i))
    return values.to_list(es)
Exemple #49
0
    def fasl_to_sexp_recursive(self, fasl_string, pos):
        from pycket import values as v
        from pycket.values_string import W_String
        from pycket.values_regex import W_Regexp, W_PRegexp, W_ByteRegexp, W_BytePRegexp
        from pycket.vector import W_Vector
        from pycket.values_struct import W_Struct
        from pycket.prims.general import srcloc
        from pycket.hash import simple as hash_simple
        from pycket.hash.equal import W_EqualHashTable
        from pycket.prims.numeric import float_bytes_to_real
        from pycket.prims.string import _str2num
        from rpython.rlib.rbigint import rbigint
        from pycket.prims.input_output import build_path, bytes_to_path_element
        from pycket.ast_vs_sexp import to_rpython_list
        from pycket.racket_entry import get_primitive

        typ, pos = self.read_byte_no_eof(fasl_string, pos)

        if typ == FASL_GRAPH_DEF_TYPE:
            position, pos = self.read_fasl_integer(fasl_string, pos)
            val, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            if position >= self.GLOBAL_SHARED_COUNT:
                raise Exception("fasl: bad graph index")
            self.SHARED[position] = val
            return val, pos
        elif typ == FASL_GRAPH_REF_TYPE:
            position, pos = self.read_fasl_integer(fasl_string, pos)
            if position >= self.GLOBAL_SHARED_COUNT:
                raise Exception("fasl: bad graph index")
            return self.SHARED[position], pos
        elif typ == FASL_FALSE_TYPE:
            return v.w_false, pos
        elif typ == FASL_TRUE_TYPE:
            return v.w_true, pos
        elif typ == FASL_NULL_TYPE:
            return v.w_null, pos
        elif typ == FASL_VOID_TYPE:
            return v.w_void, pos
        elif typ == FASL_EOF_TYPE:
            return v.eof_object, pos
        elif typ == FASL_INTEGER_TYPE:
            num, pos = self.read_fasl_integer(fasl_string, pos)
            if isinstance(num, rbigint):
                return v.W_Bignum(num), pos
            return v.W_Fixnum(num), pos
        elif typ == FASL_FLONUM_TYPE:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 8)
            return float_bytes_to_real(list(num_str), v.w_false), pos
        elif typ == FASL_SINGLE_FLONUM_TYPE:
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, 4)
            real = float_bytes_to_real(list(num_str), v.w_false)
            return real.arith_exact_inexact(), pos
        elif typ == FASL_EXTFLONUM_TYPE:
            bstr_len, pos = self.read_fasl_integer(fasl_string, pos)
            num_str, pos = self.read_bytes_exactly(fasl_string, pos, bstr_len)
            return _str2num(W_String.fromstr_utf8(num_str).as_str_utf8(),
                            10), pos
        elif typ == FASL_RATIONAL_TYPE:
            num, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            den, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Rational.make(num, den), pos
        elif typ == FASL_COMPLEX_TYPE:
            re, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            im, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Complex.from_real_pair(re, im), pos
        elif typ == FASL_CHAR_TYPE:
            _chr, pos = self.read_fasl_integer(fasl_string, pos)
            return v.W_Character(unichr(_chr)), pos
        elif typ == FASL_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol.make(sym_str), pos
        elif typ == FASL_UNREADABLE_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol.make_unreadable(sym_str), pos
        elif typ == FASL_UNINTERNED_SYMBOL_TYPE:
            sym_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Symbol(sym_str), pos
        elif typ == FASL_KEYWORD_TYPE:
            key_str, pos = self.read_fasl_string(fasl_string, pos)
            return v.W_Keyword.make(key_str), pos
        elif typ == FASL_STRING_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_String.make(str_str), pos
        elif typ == FASL_IMMUTABLE_STRING_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            return W_String.make(str_str).make_immutable(), pos
        elif typ == FASL_BYTES_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Bytes.from_string(byts, immutable=False), pos
        elif typ == FASL_IMMUTABLE_BYTES_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Bytes.from_string(byts), pos
        elif typ == FASL_PATH_TYPE:
            byts, pos = self.read_fasl_bytes(fasl_string, pos)
            return v.W_Path(byts), pos
        elif typ == FASL_RELATIVE_PATH_TYPE:
            wrt_dir = self.current_relative_dir
            p_w_lst, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            p_r_lst, _ = to_rpython_list(p_w_lst)
            rel_elems = [
                bytes_to_path_element(p) if isinstance(p, v.W_Bytes) else p
                for p in p_r_lst
            ]
            if wrt_dir:
                return build_path([wrt_dir] + rel_elems), pos
            elif rel_elems == []:
                return build_path([v.W_Symbol.make("same")]), pos
            else:
                return build_path(rel_elems), pos
        elif typ == FASL_PREGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            reg_str = W_String.make(str_str)
            pregexp = get_primitive('pregexp')
            pregexp_obj = pregexp.call_interpret([reg_str])
            return pregexp_obj, pos
        elif typ == FASL_REGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            reg_str = W_String.make(str_str)
            regexp = get_primitive('regexp')
            regexp_obj = regexp.call_interpret([reg_str])
            return regexp_obj, pos
        elif typ == FASL_BYTE_PREGEXP:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            reg_bytes = v.W_Bytes.from_string(str_str)
            byte_pregexp = get_primitive('byte-pregexp')
            byte_pregexp_obj = byte_pregexp.call_interpret([reg_bytes])
            return byte_pregexp_obj, pos
        elif typ == FASL_BYTE_REGEXP_TYPE:
            str_str, pos = self.read_fasl_string(fasl_string, pos)
            reg_bytes = v.W_Bytes.from_string(str_str)
            byte_regexp = get_primitive('byte-regexp')
            byte_regexp_obj = byte_regexp.call_interpret([reg_bytes])
            return byte_regexp_obj, pos
        elif typ == FASL_LIST_TYPE:
            list_len, pos = self.read_fasl_integer(fasl_string, pos)
            lst, pos = self.read_multi_into_rpython_list(
                fasl_string, pos, list_len)
            return v.to_list(lst), pos
        elif typ == FASL_PAIR_TYPE:
            car, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            cdr, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_Cons.make(car, cdr), pos
        elif typ == FASL_LIST_STAR_TYPE:
            list_len, pos = self.read_fasl_integer(fasl_string, pos)
            # list_len is the length of the proper part
            lst, pos = self.read_multi_into_rpython_list(
                fasl_string, pos, list_len)
            # read the last element
            return_list, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            for i in range(list_len - 1, -1, -1):
                return_list = v.W_Cons.make(lst[i], return_list)
            return return_list, pos
        elif typ == FASL_VECTOR_TYPE or typ == FASL_IMMUTABLE_VECTOR_TYPE:
            vec_len, pos = self.read_fasl_integer(fasl_string, pos)
            storage, pos = self.read_multi_into_rpython_list(
                fasl_string, pos, vec_len)
            if typ == FASL_IMMUTABLE_VECTOR_TYPE:
                return W_Vector.fromelements(storage, immutable=True), pos
            return W_Vector.fromelements(storage), pos
        elif typ == FASL_BOX_TYPE:
            element, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_MBox(element), pos
        elif typ == FASL_IMMUTABLE_BOX_TYPE:
            element, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return v.W_IBox(element), pos
        elif typ == FASL_PREFAB_TYPE:
            key, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            vals, pos = self.read_multi_into_rpython_list(
                fasl_string, pos, length)
            return W_Struct.make_prefab(key, vals), pos
        elif typ == FASL_HASH_TYPE:
            variant, pos = self.read_byte_no_eof(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            keys, vals, pos = self.read_multi_double_into_rpython_list(
                fasl_string, pos, length)
            if variant == FASL_HASH_EQ_VARIANT:
                return hash_simple.make_simple_mutable_table(
                    hash_simple.W_EqMutableHashTable, keys, vals), pos
            elif variant == FASL_HASH_EQV_VARIANT:
                return hash_simple.make_simple_mutable_table(
                    hash_simple.W_EqvMutableHashTable, keys, vals), pos
            else:  # variant == FASL_HASH_EQUAL_VARIANT:
                return W_EqualHashTable(keys, vals, immutable=False), pos
        elif typ == FASL_IMMUTABLE_HASH_TYPE:
            variant, pos = self.read_byte_no_eof(fasl_string, pos)
            length, pos = self.read_fasl_integer(fasl_string, pos)
            keys, vals, pos = self.read_multi_double_into_rpython_list(
                fasl_string, pos, length)
            if variant == FASL_HASH_EQ_VARIANT:
                return hash_simple.make_simple_immutable_table(
                    hash_simple.W_EqImmutableHashTable, keys, vals), pos
            elif variant == FASL_HASH_EQV_VARIANT:
                return hash_simple.make_simple_immutable_table(
                    hash_simple.W_EqvImmutableHashTable, keys, vals), pos
            else:  # variant == FASL_HASH_EQUAL_VARIANT:
                return W_EqualHashTable(keys, vals, immutable=True), pos
        elif typ == FASL_SRCLOC:
            # difficult to create an instance of srcloc struct so defer that to the runtime
            source, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            line, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            column, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            position, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            span, pos = self.fasl_to_sexp_recursive(fasl_string, pos)
            return W_Struct.make([source, line, column, position, span],
                                 srcloc), pos
        else:
            if typ >= FASL_SMALL_INTEGER_START:
                return v.W_Fixnum((typ - FASL_SMALL_INTEGER_START) +
                                  FASL_LOWEST_SMALL_INTEGER), pos
            else:
                raise Exception("unrecognized fasl tag : %s" % typ)
Exemple #50
0
def string_to_list(s):
    return values.to_list([values.W_Character(i) for i in s.as_unicode()])