]> Repositorios git - scryer-prolog.git/commitdiff
export loader's public predicates from builtins, handle module resolution from DCGs
authorMark Thom <[email protected]>
Tue, 2 Feb 2021 22:43:52 +0000 (15:43 -0700)
committerMark Thom <[email protected]>
Tue, 2 Feb 2021 22:43:52 +0000 (15:43 -0700)
src/lib/dcgs.pl
src/loader.pl
src/machine/loader.rs
src/machine/mod.rs

index 6918342e83a1f9dbf7b80f3bd3dacc8ddbaaa556..f9410d99d3a3204a45eccf6fdcde02308b6143fe 100644 (file)
@@ -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) :-
index 972fe367dfdf9017fde49329149752d51ef7cfb3..448960b9201bdd77bfe1ee13af258a73ba894326 100644 (file)
@@ -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 = [_|_] ->
index 286fa16723ab0d976d39a740cf0a5a4ddab9154b..d29fe3dc2df3eef23811e309c247fec39703f1d5 100644 (file)
@@ -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,
index 51af9c13dd5a9f63d524bd1a164a4b830a6c8f43..9a75837c570b79af1529e5dc4eac31ff26580d24 100644 (file)
@@ -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!()
         }