// load toplevel expressions, from (file ...) void jl_load_file_expr(char *fname, jl_value_t *ast) { jl_array_t *b = ((jl_expr_t*)ast)->args; size_t i; volatile size_t lineno=0; if (((jl_expr_t*)ast)->head == jl_continue_sym) { jl_errorf("syntax error: %s", jl_string_data(jl_exprarg(ast,0))); } JL_TRY { jl_register_toplevel_eh(); // handle syntax error if (((jl_expr_t*)ast)->head == error_sym) { jl_interpret_toplevel_expr(ast); } for(i=0; i < b->length; i++) { // process toplevel form jl_value_t *form = jl_cellref(b, i); if (jl_is_linenode(form)) { lineno = jl_linenode_line(form); } else { (void)jl_toplevel_eval_flex(form, 0, &lineno); } } } JL_CATCH { jl_value_t *fn=NULL, *ln=NULL; JL_GC_PUSH(&fn, &ln); fn = jl_pchar_to_string(fname, strlen(fname)); ln = jl_box_long(lineno); jl_raise(jl_new_struct(jl_loaderror_type, fn, ln, jl_exception_in_transit)); } }
// repeatedly call jl_parse_next and eval everything void jl_parse_eval_all(char *fname) { //ios_printf(ios_stderr, "***** loading %s\n", fname); int lineno=0; jl_value_t *fn=NULL, *ln=NULL, *form=NULL; JL_GC_PUSH(&fn, &ln, &form); JL_TRY { jl_register_toplevel_eh(); // handle syntax error while (1) { form = jl_parse_next(&lineno); if (form == NULL) break; if (jl_is_expr(form)) { if (((jl_expr_t*)form)->head == jl_continue_sym) { jl_errorf("syntax error: %s", jl_string_data(jl_exprarg(form,0))); } if (((jl_expr_t*)form)->head == error_sym) { jl_interpret_toplevel_expr(form); } } (void)jl_toplevel_eval_flex(form, 1, &lineno); } } JL_CATCH { jl_stop_parsing(); fn = jl_pchar_to_string(fname, strlen(fname)); ln = jl_box_long(lineno); jl_raise(jl_new_struct(jl_loaderror_type, fn, ln, jl_exception_in_transit)); } jl_stop_parsing(); JL_GC_POP(); }
value_t fl_invoke_julia_macro(value_t *args, uint32_t nargs) { if (nargs < 1) argcount("invoke-julia-macro", nargs, 1); jl_function_t *f = NULL; jl_value_t **margs = alloca(nargs * sizeof(jl_value_t*)); int i; for(i=0; i < nargs; i++) margs[i] = NULL; JL_GC_PUSHARGS(margs, nargs); for(i=1; i < nargs; i++) margs[i] = scm_to_julia(args[i]); jl_value_t *result; JL_TRY { jl_register_toplevel_eh(); margs[0] = scm_to_julia(args[0]); f = (jl_function_t*)jl_toplevel_eval(margs[0]); result = jl_apply(f, &margs[1], nargs-1); } JL_CATCH { JL_GC_POP(); value_t opaque = cvalue(jvtype, sizeof(void*)); *(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = jl_exception_in_transit; return fl_list2(symbol("error"), opaque); } // protect result from GC, otherwise it could be freed during future // macro expansions, since it will be referenced only from scheme and // not julia. // all calls to invoke-julia-macro happen under a single call to jl_expand, // so the preserved value stack is popped there. jl_gc_preserve(result); value_t scm = julia_to_scm(result); fl_gc_handle(&scm); value_t scmresult; jl_module_t *defmod = f->linfo->module; if (defmod == jl_current_module) { scmresult = fl_cons(scm, FL_F); } else { value_t opaque = cvalue(jvtype, sizeof(void*)); *(jl_value_t**)cv_data((cvalue_t*)ptr(opaque)) = (jl_value_t*)defmod; scmresult = fl_cons(scm, opaque); } fl_free_gc_handles(1); JL_GC_POP(); return scmresult; }
// load toplevel expressions, from (file ...) void jl_load_file_expr(char *fname, jl_value_t *ast) { jl_array_t *b = ((jl_expr_t*)ast)->args; size_t i; volatile size_t lineno=0; if (((jl_expr_t*)ast)->head == jl_continue_sym) { jl_errorf("syntax error: %s", jl_string_data(jl_exprarg(ast,0))); } char oldcwd[512]; char newcwd[512]; get_cwd(oldcwd, sizeof(oldcwd)); char *sep = strrchr(fname, PATHSEP); if (sep) { size_t n = (sep - fname)+1; if (n > sizeof(newcwd)-1) n = sizeof(newcwd)-1; strncpy(newcwd, fname, n); newcwd[n] = '\0'; set_cwd(newcwd); } JL_TRY { jl_register_toplevel_eh(); // handle syntax error if (((jl_expr_t*)ast)->head == error_sym) { jl_interpret_toplevel_expr(ast); } for(i=0; i < b->length; i++) { // process toplevel form jl_value_t *form = jl_cellref(b, i); if (jl_is_linenode(form)) { lineno = jl_linenode_line(form); } else { (void)jl_toplevel_eval_flex(form, 0, &lineno); } } } JL_CATCH { if (sep) set_cwd(oldcwd); jl_value_t *fn=NULL, *ln=NULL; JL_GC_PUSH(&fn, &ln); fn = jl_pchar_to_string(fname, strlen(fname)); ln = jl_box_long(lineno); jl_raise(jl_new_struct(jl_loaderror_type, fn, ln, jl_exception_in_transit)); } if (sep) set_cwd(oldcwd); }
static int exec_program(void) { int err = 0; again: ; JL_TRY { jl_register_toplevel_eh(); if (err) { //jl_lisp_prompt(); //return 1; jl_value_t *errs = jl_stderr_obj(); jl_value_t *e = jl_exception_in_transit; if (errs != NULL) { jl_show(jl_stderr_obj(), e); } else { while (1) { if (jl_typeof(e) == (jl_type_t*)jl_loaderror_type) { e = jl_fieldref(e, 2); // TODO: show file and line } else if (jl_typeof(e) == (jl_type_t*)jl_backtrace_type) { e = jl_fieldref(e, 0); } else break; } if (jl_typeof(e) == (jl_type_t*)jl_errorexception_type) { ios_printf(ios_stderr, "error during bootstrap: %s\n", jl_string_data(jl_fieldref(e,0))); } else { ios_printf(ios_stderr, "error during bootstrap\n"); } } ios_printf(ios_stderr, "\n"); JL_EH_POP(); return 1; } jl_load(program); } JL_CATCH { err = 1; goto again; } return 0; }
DLLEXPORT void jl_eval_user_input(jl_value_t *ast, int show_value) { if (jl_have_event_loop) { // with multi.j loaded the command line input callback can return // before the command finishes running, so we have to // disable rl to prevent the prompt from reappearing too soon. repl_callback_disable(); } JL_GC_PUSH(&ast); assert(ast != NULL); int iserr = 0; again: ; JL_TRY { jl_register_toplevel_eh(); if (have_color) { ios_printf(ios_stdout, jl_color_normal); } if (iserr) { jl_show(jl_exception_in_transit); ios_printf(ios_stdout, "\n"); JL_EH_POP(); break; // leave JL_TRY } jl_value_t *value = jl_toplevel_eval(ast); jl_set_global(jl_system_module, jl_symbol("ans"), value); if (value != (jl_value_t*)jl_nothing && show_value) { if (have_color) { ios_printf(ios_stdout, jl_answer_color()); } repl_show_value(value); ios_printf(ios_stdout, "\n"); } } JL_CATCH { iserr = 1; goto again; } ios_printf(ios_stdout, "\n"); JL_GC_POP(); repl_callback_enable(); }
static int exec_program(void) { int err = 0; again: ; JL_TRY { jl_register_toplevel_eh(); if (err) { jl_show(jl_exception_in_transit); ios_printf(ios_stdout, "\n"); JL_EH_POP(); return 1; } jl_load(program); } JL_CATCH { err = 1; goto again; } return 0; }