def write_linklet_bundle_hash(ht, out_port, env, cont): from pycket.util import console_log from pycket.racket_entry import get_primitive console_log("BUNDLE AST TO BE SERIALIZED: %s" % ht.tostring(), 7) with PerfRegion("ast->sexp"): l = ht.length() keys = [None] * l vals = [None] * l i = 0 for k, v in ht.iteritems(): keys[i] = k vals[i] = ast_to_sexp(v) i += 1 bundle_s_exp = make_simple_immutable_table(W_EqImmutableHashTable, keys, vals) console_log("WRITING BUNDLE SEXP : %s" % bundle_s_exp.tostring(), 7) s_exp_to_fasl = get_primitive("s-exp->fasl") with PerfRegionCPS("s-exp->fasl"): return s_exp_to_fasl.call([bundle_s_exp, out_port, values.w_false], env, finish_perf_region_cont( "s-exp->fasl", env, cont))
def hasheqv(args): if len(args) % 2 != 0: raise SchemeException( "hasheqv: key does not have a corresponding value") keys = [args[i] for i in range(0, len(args), 2)] vals = [args[i] for i in range(1, len(args), 2)] return make_simple_immutable_table(W_EqvImmutableHashTable, keys, vals)
def write_linklet_bundle_hash(ht, out_port, env, cont): from pycket.util import console_log from pycket.racket_entry import get_primitive console_log("BUNDLE AST TO BE SERIALIZED: %s" % ht.tostring(), 7) with PerfRegion("ast->sexp"): l = ht.length() keys = [None]*l vals = [None]*l i = 0 for k, v in ht.iteritems(): keys[i] = k vals[i] = ast_to_sexp(v) i += 1 bundle_s_exp = make_simple_immutable_table(W_EqImmutableHashTable, keys, vals) console_log("WRITING BUNDLE SEXP : %s" % bundle_s_exp.tostring(), 7) s_exp_to_fasl = get_primitive("s-exp->fasl") with PerfRegionCPS("s-exp->fasl"): return s_exp_to_fasl.call([bundle_s_exp, out_port, values.w_false], env, finish_perf_region_cont("s-exp->fasl", env, cont))
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)
def hasheqv(args): if len(args) % 2 != 0: raise SchemeException("hasheqv: key does not have a corresponding value") keys = [args[i] for i in range(0, len(args), 2)] vals = [args[i] for i in range(1, len(args), 2)] return make_simple_immutable_table(W_EqvImmutableHashTable, keys, vals)
def deserialize_loop(sexp): from pycket.prims.linklet import W_Linklet, W_LinkletBundle, W_LinkletDirectory from pycket.env import w_global_config if isinstance(sexp, values.W_Cons): c = sexp.car() if c is dir_sym: dir_map = sexp.cdr() return W_LinkletDirectory(deserialize_loop(dir_map)) elif c is bundle_sym: bundle_map = sexp.cdr() return W_LinkletBundle(deserialize_loop(bundle_map)) elif looks_like_linklet(sexp): # Unify this with compile_linklet if isinstance(sexp.cdr().car(), values.W_List): w_name = mksym("anonymous") w_importss = sexp.cdr().car() w_exports = sexp.cdr().cdr().car() w_body = sexp.cdr().cdr().cdr() else: w_name = sexp.cdr().car() w_importss = sexp.cdr().cdr().car() w_exports = sexp.cdr().cdr().cdr().car() w_body = sexp.cdr().cdr().cdr().cdr() importss_list = deserialize_importss(w_importss) # Process the exports exports = deserialize_exports(w_exports) # Process the body with PerfRegion("compile-sexp-to-ast"): body_forms = process_w_body_sexp(w_body, importss_list, exports, from_zo=True) return W_Linklet(w_name, importss_list, exports, body_forms) else: # get the length ls = sexp length = 0 is_improper = False while ls is not values.w_null: if isinstance(ls, values.W_Cons): length += 1 ls = ls.cdr() else: is_improper = True ls = values.w_null # allocate an r_list (to avoid reversing w_list) if is_improper: sexp_ls = [None] * (length + 1) else: sexp_ls = [None] * length # second pass, get the elements ls = sexp for i in range(length - 1, -1, -1): sexp_ls[i] = ls.car() ls = ls.cdr() if is_improper: sexp_ls[length] = ls # make the new list new = values.w_null for s in sexp_ls: new = values.W_Cons.make(deserialize_loop(s), new) return new elif isinstance(sexp, simple.W_EqImmutableHashTable): l = sexp.length() keys = [None] * l vals = [None] * l i = 0 for k, v in sexp.iteritems(): keys[i] = k vals[i] = deserialize_loop(v) i += 1 return simple.make_simple_immutable_table( simple.W_EqImmutableHashTable, keys, vals) elif isinstance(sexp, equal.W_EqualHashTable): l = sexp.length() keys = [None] * l vals = [None] * l i = 0 for k, v in sexp.hash_items(): keys[i] = k vals[i] = deserialize_loop(v) i += 1 return equal.W_EqualHashTable(keys, vals, immutable=True) elif isinstance(sexp, vector.W_Vector): new = [None] * sexp.length() items = sexp.get_strategy().ref_all(sexp) for index, obj in enumerate(items): new[index] = deserialize_loop(obj) return vector.W_Vector.fromelements(new, sexp.immutable()) else: return sexp
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()
def deserialize_loop(sexp): from pycket.prims.linklet import W_Linklet, W_LinkletBundle, W_LinkletDirectory from pycket.env import w_global_config if isinstance(sexp, values.W_Cons): c = sexp.car() if c is dir_sym: dir_map = sexp.cdr() return W_LinkletDirectory(deserialize_loop(dir_map)) elif c is bundle_sym: bundle_map = sexp.cdr() return W_LinkletBundle(deserialize_loop(bundle_map)) elif looks_like_linklet(sexp): # Unify this with compile_linklet if isinstance(sexp.cdr().car(), values.W_List): w_name = mksym("anonymous") w_importss = sexp.cdr().car() w_exports = sexp.cdr().cdr().car() w_body = sexp.cdr().cdr().cdr() else: w_name = sexp.cdr().car() w_importss = sexp.cdr().cdr().car() w_exports = sexp.cdr().cdr().cdr().car() w_body = sexp.cdr().cdr().cdr().cdr() importss_list = deserialize_importss(w_importss) # Process the exports exports = deserialize_exports(w_exports) # Process the body with PerfRegion("compile-sexp-to-ast"): body_forms = process_w_body_sexp(w_body, importss_list, exports, from_zo=True) return W_Linklet(w_name, importss_list, exports, body_forms) else: # get the length ls = sexp length = 0 is_improper = False while ls is not values.w_null: if isinstance(ls, values.W_Cons): length += 1 ls = ls.cdr() else: is_improper = True ls = values.w_null # allocate an r_list (to avoid reversing w_list) if is_improper: sexp_ls = [None]*(length+1) else: sexp_ls = [None]*length # second pass, get the elements ls = sexp for i in range(length-1, -1, -1): sexp_ls[i] = ls.car() ls = ls.cdr() if is_improper: sexp_ls[length] = ls # make the new list new = values.w_null for s in sexp_ls: new = values.W_Cons.make(deserialize_loop(s), new) return new elif isinstance(sexp, simple.W_EqImmutableHashTable): l = sexp.length() keys = [None]*l vals = [None]*l i = 0 for k, v in sexp.iteritems(): keys[i] = k vals[i] = deserialize_loop(v) i += 1 return simple.make_simple_immutable_table(simple.W_EqImmutableHashTable, keys, vals) elif isinstance(sexp, equal.W_EqualHashTable): l = sexp.length() keys = [None]*l vals = [None]*l i = 0 for k, v in sexp.hash_items(): keys[i] = k vals[i] = deserialize_loop(v) i += 1 return equal.W_EqualHashTable(keys, vals, immutable=True) elif isinstance(sexp, vector.W_Vector): new = [None]*sexp.length() items = sexp.get_strategy().ref_all(sexp) for index, obj in enumerate(items): new[index] = deserialize_loop(obj) return vector.W_Vector.fromelements(new, sexp.immutable()) else: return sexp
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()
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)
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 "hasheq-keys" in obj and "hasheq-vals" in obj: keys = [to_value(i) for i in obj["hasheq-keys"].value_array()] vals = [to_value(i) for i in obj["hasheq-vals"].value_array()] return make_simple_immutable_table(W_EqImmutableHashTable, keys, vals) if "hasheqv-keys" in obj and "hasheqv-vals" in obj: keys = [to_value(i) for i in obj["hasheqv-keys"].value_array()] vals = [to_value(i) for i in obj["hasheqv-vals"].value_array()] return make_simple_immutable_table(W_EqvImmutableHashTable, keys, vals) 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()