From: Mark Thom Date: Tue, 2 Feb 2021 22:43:52 +0000 (-0700) Subject: export loader's public predicates from builtins, handle module resolution from DCGs X-Git-Tag: v0.9.0~150^2~67^2~4 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=4e29099ed91195a8ce848a102f6568152a33d1a9;p=scryer-prolog.git export loader's public predicates from builtins, handle module resolution from DCGs --- diff --git a/src/lib/dcgs.pl b/src/lib/dcgs.pl index 6918342e..f9410d99 100644 --- a/src/lib/dcgs.pl +++ b/src/lib/dcgs.pl @@ -18,9 +18,12 @@ phrase(GRBody, S0) :- phrase(GRBody, S0, S) :- ( var(GRBody) -> throw(error(instantiation_error, phrase/3)) - ; strip_module(GRBody, _, GRBody0), - dcg_constr(GRBody0) -> - phrase_(GRBody0, S0, S) + ; strip_module(GRBody, Module, GRBody0), + dcg_constr(GRBody0), + ( var(Module) -> + phrase_(GRBody0, S0, S) + ; phrase_(Module:GRBody0, S0, S) + ) ; functor(GRBody, _, _) -> call(GRBody, S0, S) ; throw(error(type_error(callable, GRBody), phrase/3)) @@ -30,22 +33,36 @@ phrase_([], S, S). phrase_(!, S, S). phrase_((A, B), S0, S) :- phrase(A, S0, S1), phrase(B, S1, S). +phrase_(M:(A, B), S0, S) :- + phrase(M:A, S0, S1), phrase(M:B, S1, S). phrase_((A -> B ; C), S0, S) :- !, ( phrase(A, S0, S1) -> phrase(B, S1, S) ; phrase(C, S0, S) ). +phrase_(M:(A -> B ; C), S0, S) :- + !, + ( phrase(M:A, S0, S1) -> + phrase(M:B, S1, S) + ; phrase(M:C, S0, S) + ). phrase_((A ; B), S0, S) :- ( phrase(A, S0, S) ; phrase(B, S0, S) ). +phrase_(M:(A ; B), S0, S) :- + ( phrase(M:A, S0, S) ; phrase(M:B, S0, S) ). phrase_((A | B), S0, S) :- ( phrase(A, S0, S) ; phrase(B, S0, S) ). +phrase_(M:(A | B), S0, S) :- + ( phrase(M:A, S0, S) ; phrase(M:B, S0, S) ). phrase_({G}, S0, S) :- ( call(G), S0 = S ). phrase_(call(G), S0, S) :- call(G, S0, S). phrase_((A -> B), S0, S) :- phrase((A -> B ; fail), S0, S). +phrase_(M:(A -> B), S0, S) :- + phrase((M:A -> M:B ; fail), S0, S). phrase_(phrase(NonTerminal), S0, S) :- phrase(NonTerminal, S0, S). phrase_([T|Ts], S0, S) :- diff --git a/src/loader.pl b/src/loader.pl index 972fe367..448960b9 100644 --- a/src/loader.pl +++ b/src/loader.pl @@ -30,7 +30,7 @@ expand_term(Term, ExpandedTerm) :- error:instantiation_error(term_expansion/2) ; ExpandedTerm0 = [_|_] -> term_expansion_list(ExpandedTerm0, ExpandedTerm, []) - ; expand_term(ExpandedTerm0, ExpandedTerm) % term_expansion(ExpandedTerm0, ExpandedTerm) + ; expand_term(ExpandedTerm0, ExpandedTerm) ) ; Term = ExpandedTerm ). @@ -38,7 +38,7 @@ expand_term(Term, ExpandedTerm) :- term_expansion_list([], ExpandedTerms, ExpandedTerms). term_expansion_list([Term|Terms], ExpandedTermsHead, ExpandedTermsTail) :- - expand_term(Term, ExpandedTerm0), % term_expansion(Term, ExpandedTerm0), + expand_term(Term, ExpandedTerm0), ( var(ExpandedTerm0) -> error:instantiation_error(term_expansion/2) ; ExpandedTerm0 = [_|_] -> diff --git a/src/machine/loader.rs b/src/machine/loader.rs index 286fa167..d29fe3dc 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -1733,11 +1733,12 @@ fn load_module( code_dir: &mut CodeDir, op_dir: &mut OpDir, meta_predicate_dir: &mut MetaPredicateDir, + compilation_target: &CompilationTarget, module: &Module, ) { import_module_exports( &mut RetractionInfo::new(0), - &CompilationTarget::User, + &compilation_target, module, code_dir, op_dir, diff --git a/src/machine/mod.rs b/src/machine/mod.rs index 51af9c13..9a75837c 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -230,6 +230,7 @@ impl Machine { &mut self.indices.code_dir, &mut self.indices.op_dir, &mut self.indices.meta_predicates, + &CompilationTarget::User, toplevel, ); } else { @@ -337,6 +338,7 @@ impl Machine { &mut wam.indices.code_dir, &mut wam.indices.op_dir, &mut wam.indices.meta_predicates, + &CompilationTarget::User, builtins, ); } else { @@ -354,13 +356,32 @@ impl Machine { ), ).unwrap(); - if let Some(loader) = wam.indices.modules.get(&clause_name!("loader")) { + if let Some(loader) = wam.indices.modules.swap_remove(&clause_name!("loader")) { + if let Some(builtins) = wam.indices.modules.get_mut(&clause_name!("builtins")) { + // Import loader's exports into the builtins module so they will be + // implicitly included every further module. + load_module( + &mut builtins.code_dir, + &mut builtins.op_dir, + &mut builtins.meta_predicates, + &CompilationTarget::Module(clause_name!("builtins")), + &loader, + ); + + for export in &loader.module_decl.exports { + builtins.module_decl.exports.push(export.clone()); + } + } + load_module( &mut wam.indices.code_dir, &mut wam.indices.op_dir, &mut wam.indices.meta_predicates, - loader, + &CompilationTarget::User, + &loader, ); + + wam.indices.modules.insert(clause_name!("loader"), loader); } else { unreachable!() }