def sample_sexp(): from pycket.expand import readfile_rpython, getkey from pycket import pycket_json data = readfile_rpython('sample-module.json') json = pycket_json.loads(data) ast = JsonLoader().to_ast(json) #module return ast.to_sexp()
def main(argv): assert len(argv) > 1 data = readfile_rpython(argv[1]) #data = readfile(argv[1]) json = loads(data) print json print json.tostring() return 0
def load_linklet(json_file_name, set_version=False): from pycket.expand import readfile_rpython, getkey, JsonLoader from pycket.util import console_log from pycket.env import w_version """ Expands and loads a linklet from a JSON file""" with PerfRegion("json-load"): data = readfile_rpython(json_file_name) json = pycket_json.loads(data) console_log("Finished reading JSON from %s" % json_file_name, 2) assert json.is_object json_python_dict = json.value_object() assert "linklet" in json_python_dict linklet_dict = getkey(json_python_dict, "linklet", type='o') assert "exports" in linklet_dict and "body" in linklet_dict # list of JsonObject exports_list = getkey(linklet_dict, "exports", type='a') if set_version: from pycket.util import console_log conf = getkey(linklet_dict, "config", type='o') ver = conf['version'].value_string() console_log("Setting the version to %s" % ver) w_version.set_version(ver) exports = {} for exp in exports_list: if exp.is_array: arr = exp.value_array() internal_str = arr[0].value_object()['quote'].value_object( )['toplevel'].value_string() internal_name = W_Symbol.make(internal_str) external_name = W_Symbol.make( arr[1].value_object()['quote'].value_object() ['toplevel'].value_string()) w_internal_name = Gensym.gensym(internal_str) exports[internal_name] = Export(w_internal_name, external_name) else: exp_str = exp.value_object()['quote'].value_object( )['toplevel'].value_string() exp_sym = W_Symbol.make(exp_str) w_exp_sym = Gensym.gensym(exp_str) exports[exp_sym] = Export(w_exp_sym, exp_sym) imports_list = getkey(linklet_dict, "importss", type='a', default=[]) importss = [None] * len(imports_list) # list of dict if "importss" in linklet_dict: for index, imports in enumerate(imports_list): arr = imports.value_array() # bootstrap linklets have no imports at all # this is only for debugging purposes instance_imports = [] for id_str in arr: imp_str = id_str.value_object()['quote'].value_object( )['toplevel'].value_string() imp_sym = W_Symbol.make(imp_str) w_imp_sym = Gensym.gensym(imp_str) instance_imports.append( Import(W_Fixnum(index), imp_sym, w_imp_sym, w_imp_sym)) importss[index] = instance_imports console_log("Converting linklet forms to AST ...", 2) loader = JsonLoader() with PerfRegion("json-to-ast"): all_forms = [] for body_form in getkey(linklet_dict, "body", type='a'): form_2 = loader.to_ast(body_form) form_1 = Context.normalize_term(form_2) form = assign_convert(form_1) all_forms.append(form) # for each exported defined id, we need to add a # variable-set! for the exported var with the defined id for exp_sym, exp_obj in exports.iteritems(): rator = ModuleVar(var_set_sym, "#%kernel", var_set_sym, None) exp_var = LinkletVar(exp_obj.int_id) top_var = ToplevelVar(exp_sym) mode = Quote(values.w_false) # FIXME: possible optimization rands = [exp_var, top_var, mode] all_forms.append(App.make(rator, rands)) linkl = W_Linklet(W_Symbol.make(json_file_name), importss, exports, all_forms) console_log("Finished converting linklet forms to AST ...", 2) config = {} config_obj = getkey(linklet_dict, "config", type='o') if config_obj is not None: for k, v in config_obj.iteritems(): config[k] = v.value_string() return linkl, config
def load_linklet(json_file_name, set_version=False, generate_zo=False): from pycket.expand import readfile_rpython, getkey, JsonLoader from pycket.util import console_log from pycket.env import w_version import os if generate_zo: try: os.remove("expander.zo") except OSError: pass if set_version and os.path.exists("expander.zo"): console_log("Loading the expander linklet from expander.zo") # We're loading the expander, so try fasl->sexp from pycket.racket_entry import get_primitive from pycket.prims.input_output import open_input_file with PerfRegion("expander-linklet"): fasl_to_s_exp = get_primitive("fasl->s-exp") in_file = open_input_file.w_prim.call_interpret( [W_Path("expander.zo")]) try: expander_zo_sexp = fasl_to_s_exp.call_interpret( [in_file, w_true]) version = expander_zo_sexp.car() expander_sexp = expander_zo_sexp.cdr() expander_linkl = deserialize_loop(expander_sexp) w_version.set_version(version.tostring()) console_log("Setting the version to %s" % version.tostring()) return expander_linkl, None except (SchemeException, OSError): console_log("Couldn't read from expander.zo") """ Expands and loads a linklet from a JSON file""" with PerfRegion("json-load"): data = readfile_rpython(json_file_name) json = pycket_json.loads(data) console_log("Finished reading JSON from %s" % json_file_name, 2) assert json.is_object json_python_dict = json.value_object() assert "linklet" in json_python_dict linklet_dict = getkey(json_python_dict, "linklet", type='o') assert "exports" in linklet_dict and "body" in linklet_dict # list of JsonObject exports_list = getkey(linklet_dict, "exports", type='a') if set_version: from pycket.util import console_log conf = getkey(linklet_dict, "config", type='o') ver = conf['version'].value_string() console_log("Setting the version to %s" % ver) w_version.set_version(ver) exports = {} for exp in exports_list: if exp.is_array: arr = exp.value_array() internal_str = arr[0].value_object()['quote'].value_object( )['toplevel'].value_string() internal_name = W_Symbol.make(internal_str) external_name = W_Symbol.make( arr[1].value_object()['quote'].value_object() ['toplevel'].value_string()) w_internal_name = Gensym.gensym(internal_str) exports[internal_name] = Export(w_internal_name, external_name) else: exp_str = exp.value_object()['quote'].value_object( )['toplevel'].value_string() exp_sym = W_Symbol.make(exp_str) w_exp_sym = Gensym.gensym(exp_str) exports[exp_sym] = Export(w_exp_sym, exp_sym) imports_list = getkey(linklet_dict, "importss", type='a', default=[]) importss = [None] * len(imports_list) # list of dict if "importss" in linklet_dict: for index, imports in enumerate(imports_list): arr = imports.value_array() # bootstrap linklets have no imports at all # this is only for debugging purposes instance_imports = [] for id_str in arr: imp_str = id_str.value_object()['quote'].value_object( )['toplevel'].value_string() imp_sym = W_Symbol.make(imp_str) w_imp_sym = Gensym.gensym(imp_str) instance_imports.append( Import(W_Fixnum(index), imp_sym, w_imp_sym, w_imp_sym)) importss[index] = instance_imports console_log("Converting linklet forms to AST ...", 2) loader = JsonLoader() with PerfRegion("json-to-ast"): all_forms = [] for body_form in getkey(linklet_dict, "body", type='a'): form_2 = loader.to_ast(body_form) form_1 = Context.normalize_term(form_2) # if form_1.tostring() != form_2.tostring(): # import pdb;pdb.set_trace() form = assign_convert(form_1) all_forms.append(form) # for each exported defined id, we need to add a # variable-set! for the exported var with the defined id for exp_sym, exp_obj in exports.iteritems(): rator = ModuleVar(var_set_sym, "#%kernel", var_set_sym, None) exp_var = LinkletStaticVar(exp_obj.int_id) top_var = ToplevelVar(exp_sym) mode = Quote(values.w_false) # FIXME: possible optimization rands = [exp_var, top_var, mode] all_forms.append(App.make(rator, rands)) linkl = W_Linklet(W_Symbol.make(json_file_name), importss, exports, all_forms) console_log("Finished converting linklet forms to AST ...", 2) config = {} config_obj = getkey(linklet_dict, "config", type='o') if config_obj is not None: for k, v in config_obj.iteritems(): config[k] = v.value_string() if set_version and not os.path.exists("expander.zo"): console_log("Serializing the expander linklet into expander.zo") from pycket.racket_entry import get_primitive from pycket.prims.input_output import open_output_file s_exp_to_fasl = get_primitive("s-exp->fasl") expander_sexp = ast_to_sexp(linkl) version = W_Symbol.make(w_version.get_version()) expander_zo_sexp = W_Cons.make(version, expander_sexp) try: out_file = open_output_file.w_prim.call_interpret( [W_Path("expander.zo")]) s_exp_to_fasl.call_interpret( [expander_zo_sexp, out_file, values.w_false]) except (SchemeException, OSError): console_log("Couldn't write the expander.zo") return linkl, config
def load_linklet(json_file_name, set_version=False): from pycket.expand import readfile_rpython, getkey, JsonLoader from pycket.util import console_log from pycket.env import w_version """ Expands and loads a linklet from a JSON file""" with PerfRegion("json-load"): data = readfile_rpython(json_file_name) json = pycket_json.loads(data) console_log("Finished reading JSON from %s" % json_file_name, 2) assert json.is_object json_python_dict = json.value_object() assert "linklet" in json_python_dict linklet_dict = getkey(json_python_dict, "linklet", type='o') assert "exports" in linklet_dict and "body" in linklet_dict # list of JsonObject exports_list = getkey(linklet_dict, "exports", type='a') if set_version: from pycket.util import console_log conf = getkey(linklet_dict, "config", type='o') ver = conf['version'].value_string() console_log("Setting the version to %s" % ver) w_version.set_version(ver) exports = {} for exp in exports_list: if exp.is_array: arr = exp.value_array() internal_str = arr[0].value_object()['quote'].value_object()['toplevel'].value_string() internal_name = W_Symbol.make(internal_str) external_name = W_Symbol.make(arr[1].value_object()['quote'].value_object()['toplevel'].value_string()) w_internal_name = Gensym.gensym(internal_str) exports[internal_name] = Export(w_internal_name, external_name) else: exp_str = exp.value_object()['quote'].value_object()['toplevel'].value_string() exp_sym = W_Symbol.make(exp_str) w_exp_sym = Gensym.gensym(exp_str) exports[exp_sym] = Export(w_exp_sym, exp_sym) imports_list = getkey(linklet_dict, "importss", type='a', default=[]) importss = [None]*len(imports_list) # list of dict if "importss" in linklet_dict: for index, imports in enumerate(imports_list): arr = imports.value_array() # bootstrap linklets have no imports at all # this is only for debugging purposes instance_imports = [] for id_str in arr: imp_str = id_str.value_object()['quote'].value_object()['toplevel'].value_string() imp_sym = W_Symbol.make(imp_str) w_imp_sym = Gensym.gensym(imp_str) instance_imports.append(Import(W_Fixnum(index), imp_sym, w_imp_sym, w_imp_sym)) importss[index] = instance_imports console_log("Converting linklet forms to AST ...", 2) loader = JsonLoader() with PerfRegion("json-to-ast"): all_forms = [] for body_form in getkey(linklet_dict, "body", type='a'): form_2 = loader.to_ast(body_form) form_1 = Context.normalize_term(form_2) form = assign_convert(form_1) all_forms.append(form) # for each exported defined id, we need to add a # variable-set! for the exported var with the defined id for exp_sym, exp_obj in exports.iteritems(): rator = ModuleVar(var_set_sym, "#%kernel", var_set_sym, None) exp_var = LinkletVar(exp_obj.int_id) top_var = ToplevelVar(exp_sym) mode = Quote(values.w_false) # FIXME: possible optimization rands = [exp_var, top_var, mode] all_forms.append(App.make(rator,rands)) linkl = W_Linklet(W_Symbol.make(json_file_name), importss, exports, all_forms) console_log("Finished converting linklet forms to AST ...", 2) config = {} config_obj = getkey(linklet_dict, "config", type='o') if config_obj is not None: for k, v in config_obj.iteritems(): config[k] = v.value_string() return linkl, config