Skip to content

Commit d411c4e

Browse files
committed
Remove flisp code from jl_parse_eval_all
This makes the top level parse-lower-eval loop independent of flisp internals.
1 parent 9ac1888 commit d411c4e

File tree

4 files changed

+70
-121
lines changed

4 files changed

+70
-121
lines changed

base/loading.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ actually evaluates `mapexpr(expr)`. If it is omitted, `mapexpr` defaults to [`i
10831083
"""
10841084
function include_string(mapexpr::Function, m::Module, txt_::AbstractString, fname::AbstractString="string")
10851085
txt = String(txt_)
1086-
ccall(:jl_load_rewrite_file_string, Any, (Any, Any, Any, Any),
1086+
ccall(:jl_parse_eval_all, Any, (Any, Any, Any, Any),
10871087
m, txt, String(fname), mapexpr === identity ? nothing : mapexpr)
10881088
end
10891089

src/ast.c

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ typedef enum {
783783
JL_PARSE_TOPLEVEL = 3,
784784
} jl_parse_rule_t;
785785

786+
// Parse string `content` starting at byte offset `start_pos` attributing it to
787+
// `filename`. Return an svec of (parse_result, final_pos)
786788
JL_DLLEXPORT jl_value_t *jl_fl_parse(const char *content, size_t content_len,
787789
const char *filename, size_t filename_len,
788790
int start_pos, jl_parse_rule_t rule)
@@ -856,113 +858,6 @@ JL_DLLEXPORT jl_value_t *jl_parse_input_line(const char *str, size_t len,
856858
return jl_parse_all(str, len, filename, filename_len);
857859
}
858860

859-
// Parse a string `text` at top level, attributing source locations to
860-
// `filename`. Each expression is optionally modified by `mapexpr` (if
861-
// non-NULL) before evaluating in module `inmodule`.
862-
jl_value_t *jl_parse_eval_all(jl_module_t *inmodule, jl_value_t *text,
863-
jl_value_t *filename, jl_value_t *mapexpr)
864-
{
865-
jl_ptls_t ptls = jl_get_ptls_states();
866-
if (ptls->in_pure_callback)
867-
jl_error("cannot use include inside a generated function");
868-
jl_ast_context_t *ctx = jl_ast_ctx_enter();
869-
fl_context_t *fl_ctx = &ctx->fl;
870-
value_t f, ast, expression;
871-
f = cvalue_static_cstrn(fl_ctx, jl_string_data(filename), jl_string_len(filename));
872-
fl_gc_handle(fl_ctx, &f);
873-
{
874-
JL_TIMING(PARSING);
875-
value_t t = cvalue_static_cstrn(fl_ctx, jl_string_data(text),
876-
jl_string_len(text));
877-
fl_gc_handle(fl_ctx, &t);
878-
ast = fl_applyn(fl_ctx, 2, symbol_value(symbol(fl_ctx, "jl-parse-all")), t, f);
879-
fl_free_gc_handles(fl_ctx, 1);
880-
}
881-
fl_free_gc_handles(fl_ctx, 1);
882-
if (ast == fl_ctx->F) {
883-
jl_ast_ctx_leave(ctx);
884-
jl_errorf("could not open file %s", jl_string_data(filename));
885-
}
886-
fl_gc_handle(fl_ctx, &ast);
887-
fl_gc_handle(fl_ctx, &expression);
888-
889-
int last_lineno = jl_lineno;
890-
const char *last_filename = jl_filename;
891-
size_t last_age = jl_get_ptls_states()->world_age;
892-
int lineno = 0;
893-
jl_lineno = 0;
894-
jl_filename = jl_string_data(filename);
895-
jl_module_t *old_module = ctx->module;
896-
ctx->module = inmodule;
897-
jl_value_t *form = NULL;
898-
jl_value_t *result = jl_nothing;
899-
int err = 0;
900-
JL_GC_PUSH2(&form, &result);
901-
JL_TRY {
902-
assert(iscons(ast) && car_(ast) == symbol(fl_ctx, "toplevel"));
903-
ast = cdr_(ast);
904-
while (iscons(ast)) {
905-
expression = car_(ast);
906-
{
907-
JL_TIMING(LOWERING);
908-
if (fl_ctx->T == fl_applyn(fl_ctx, 1, symbol_value(symbol(fl_ctx, "contains-macrocall")), expression)) {
909-
form = scm_to_julia(fl_ctx, expression, inmodule);
910-
if (mapexpr)
911-
form = jl_call1(mapexpr, form);
912-
form = jl_expand_macros(form, inmodule, NULL, 0);
913-
expression = julia_to_scm(fl_ctx, form);
914-
}
915-
else if (mapexpr) {
916-
form = scm_to_julia(fl_ctx, expression, inmodule);
917-
form = jl_call1(mapexpr, form);
918-
expression = julia_to_scm(fl_ctx, form);
919-
}
920-
// expand non-final expressions in statement position (value unused)
921-
expression =
922-
fl_applyn(fl_ctx, 4,
923-
symbol_value(symbol(fl_ctx, "jl-expand-to-thunk-warn")),
924-
expression, symbol(fl_ctx, jl_string_data(filename)),
925-
fixnum(lineno), iscons(cdr_(ast)) ? fl_ctx->T : fl_ctx->F);
926-
}
927-
jl_get_ptls_states()->world_age = jl_world_counter;
928-
form = scm_to_julia(fl_ctx, expression, inmodule);
929-
JL_SIGATOMIC_END();
930-
jl_get_ptls_states()->world_age = jl_world_counter;
931-
if (jl_is_linenode(form)) {
932-
lineno = jl_linenode_line(form);
933-
jl_lineno = lineno;
934-
}
935-
else {
936-
result = jl_toplevel_eval_flex(inmodule, form, 1, 1);
937-
}
938-
JL_SIGATOMIC_BEGIN();
939-
ast = cdr_(ast);
940-
}
941-
}
942-
JL_CATCH {
943-
form = filename;
944-
result = jl_box_long(jl_lineno);
945-
err = 1;
946-
goto finally; // skip jl_restore_excstack
947-
}
948-
finally:
949-
jl_get_ptls_states()->world_age = last_age;
950-
jl_lineno = last_lineno;
951-
jl_filename = last_filename;
952-
fl_free_gc_handles(fl_ctx, 2);
953-
ctx->module = old_module;
954-
jl_ast_ctx_leave(ctx);
955-
if (err) {
956-
if (jl_loaderror_type == NULL)
957-
jl_rethrow();
958-
else
959-
jl_rethrow_other(jl_new_struct(jl_loaderror_type, form, result,
960-
jl_current_exception()));
961-
}
962-
JL_GC_POP();
963-
return result;
964-
}
965-
966861
// returns either an expression or a thunk
967862
jl_value_t *jl_call_scm_on_ast(const char *funcname, jl_value_t *expr, jl_module_t *inmodule)
968863
{

src/julia_internal.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,6 @@ jl_array_t *jl_get_loaded_modules(void);
473473
jl_value_t *jl_toplevel_eval_flex(jl_module_t *m, jl_value_t *e, int fast, int expanded);
474474

475475
jl_value_t *jl_eval_global_var(jl_module_t *m JL_PROPAGATES_ROOT, jl_sym_t *e);
476-
jl_value_t *jl_parse_eval_all(jl_module_t *inmodule, jl_value_t *text,
477-
jl_value_t *filename, jl_value_t *mapexpr);
478476
jl_value_t *jl_interpret_toplevel_thunk(jl_module_t *m, jl_code_info_t *src);
479477
jl_value_t *jl_interpret_toplevel_expr_in(jl_module_t *m, jl_value_t *e,
480478
jl_code_info_t *src,

src/toplevel.c

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -869,19 +869,75 @@ JL_DLLEXPORT jl_value_t *jl_infer_thunk(jl_code_info_t *thk, jl_module_t *m)
869869
//------------------------------------------------------------------------------
870870
// Code loading: combined parse+eval for include() and include_string()
871871

872-
// Parse julia code from the string `text`, attributing it to `filename`. Eval
873-
// the resulting statements into `module` after applying `mapexpr` to each one
874-
// (if not one of NULL or nothing).
875-
JL_DLLEXPORT jl_value_t *jl_load_rewrite_file_string(jl_module_t *module,
876-
jl_value_t *text,
877-
jl_value_t *filename,
878-
jl_value_t *mapexpr)
872+
// Parse julia code from the string `text` at top level, attributing it to
873+
// `filename`. Each resulting top level expression is optionally transformed by
874+
// `mapexpr` (if not nothing) before being lowered and evaluated in module
875+
// `module`.
876+
JL_DLLEXPORT jl_value_t *jl_parse_eval_all(jl_module_t *module, jl_value_t *text,
877+
jl_value_t *filename, jl_value_t *mapexpr)
879878
{
880879
if (!jl_is_string(text) || !jl_is_string(filename)) {
881880
jl_errorf("Expected `String`s for `text` and `filename`");
882881
}
883-
return jl_parse_eval_all(module, text, filename,
884-
mapexpr == jl_nothing ? NULL : mapexpr);
882+
jl_ptls_t ptls = jl_get_ptls_states();
883+
if (ptls->in_pure_callback)
884+
jl_error("cannot use include inside a generated function");
885+
886+
jl_value_t *result = jl_nothing;
887+
jl_value_t *ast = NULL;
888+
jl_value_t *expression = NULL;
889+
JL_GC_PUSH3(&ast, &result, &expression);
890+
891+
ast = jl_parse_all(jl_string_data(text), jl_string_len(text),
892+
jl_string_data(filename), jl_string_len(filename));
893+
if (!jl_is_expr(ast) || ((jl_expr_t*)ast)->head != toplevel_sym) {
894+
jl_errorf("jl_parse_all() must generate a top level expression");
895+
}
896+
897+
int last_lineno = jl_lineno;
898+
const char *last_filename = jl_filename;
899+
size_t last_age = jl_get_ptls_states()->world_age;
900+
int lineno = 0;
901+
jl_lineno = 0;
902+
jl_filename = jl_string_data(filename);
903+
int err = 0;
904+
905+
JL_TRY {
906+
for (size_t i = 0; i < jl_expr_nargs(ast); i++) {
907+
expression = jl_exprarg(ast, i);
908+
if (jl_is_linenode(expression)) {
909+
// filename is already set above.
910+
lineno = jl_linenode_line(expression);
911+
jl_lineno = lineno;
912+
continue;
913+
}
914+
if (mapexpr != jl_nothing) {
915+
expression = jl_call1(mapexpr, expression);
916+
}
917+
expression = jl_expand_with_loc_warn(expression, module,
918+
jl_string_data(filename), lineno);
919+
jl_get_ptls_states()->world_age = jl_world_counter;
920+
result = jl_toplevel_eval_flex(module, expression, 1, 1);
921+
}
922+
}
923+
JL_CATCH {
924+
result = jl_box_long(jl_lineno); // (ab)use result to root error line
925+
err = 1;
926+
goto finally; // skip jl_restore_excstack
927+
}
928+
finally:
929+
jl_get_ptls_states()->world_age = last_age;
930+
jl_lineno = last_lineno;
931+
jl_filename = last_filename;
932+
if (err) {
933+
if (jl_loaderror_type == NULL)
934+
jl_rethrow();
935+
else
936+
jl_rethrow_other(jl_new_struct(jl_loaderror_type, filename, result,
937+
jl_current_exception()));
938+
}
939+
JL_GC_POP();
940+
return result;
885941
}
886942

887943
// Synchronously read content of entire file into a julia String
@@ -910,7 +966,7 @@ JL_DLLEXPORT jl_value_t *jl_load_rewrite(jl_module_t *module,
910966
{
911967
jl_value_t *text = jl_file_content_as_string(filename);
912968
JL_GC_PUSH1(&text);
913-
jl_value_t *result = jl_load_rewrite_file_string(module, text, filename, mapexpr);
969+
jl_value_t *result = jl_parse_eval_all(module, text, filename, mapexpr);
914970
JL_GC_POP();
915971
return result;
916972
}
@@ -938,7 +994,7 @@ JL_DLLEXPORT jl_value_t *jl_load_file_string(const char *text, size_t len,
938994
JL_GC_PUSH2(&text_, &filename_);
939995
text_ = jl_pchar_to_string(text, len);
940996
filename_ = jl_cstr_to_string(filename);
941-
jl_value_t *result = jl_load_rewrite_file_string(module, text_, filename_, jl_nothing);
997+
jl_value_t *result = jl_parse_eval_all(module, text_, filename_, jl_nothing);
942998
JL_GC_POP();
943999
return result;
9441000
}

0 commit comments

Comments
 (0)