From: Mark Thom Date: Thu, 5 Jul 2018 05:25:55 +0000 (-0600) Subject: include builtins implicitly in every module. X-Git-Tag: v0.8.110~448 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=073a8888f3b81e41f13a33f79d84bb5ab8ea5215;p=scryer-prolog.git include builtins implicitly in every module. --- diff --git a/src/main.rs b/src/main.rs index c4c749fe..ddc82b10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ fn prolog_repl() { match read() { Input::Line(line) => parse_and_compile_line(&mut wam, line.as_str()), Input::Batch(batch) => - match compile_listing(&mut wam, batch.as_str()) { + match compile_user_module(&mut wam, batch.as_str()) { EvalSession::Error(e) => println!("{}", e), _ => {} }, diff --git a/src/prolog/compile.rs b/src/prolog/compile.rs index 50716f5c..35b0c697 100644 --- a/src/prolog/compile.rs +++ b/src/prolog/compile.rs @@ -180,7 +180,7 @@ impl<'a> ListingCompiler<'a> { compile_appendix(&mut decl_code, Vec::from(queue))?; - let idx = code_dir.entry((name, arity)).or_insert(CodeIndex::default()); + let idx = code_dir.entry((name, arity)).or_insert(CodeIndex::default()); set_code_index!(idx, IndexPtr::Index(p), self.get_module_name()); code.extend(decl_code.into_iter()); @@ -193,7 +193,7 @@ impl<'a> ListingCompiler<'a> { let code_dir = mem::replace(indices.code_dir, HashMap::new()); let op_dir = mem::replace(indices.op_dir, HashMap::new()); - if let Some(mut module) = self.module { + if let Some(mut module) = self.module { module.code_dir.extend(as_module_code_dir(code_dir)); module.op_dir.extend(op_dir.into_iter()); @@ -213,8 +213,6 @@ fn use_module(module: &mut Option, submodule: &Module, indices: &mut Mac if let &mut Some(ref mut module) = module { module.use_module(submodule); } - - // self.wam.use_module_in_toplevel(name); } fn use_qualified_module(module: &mut Option, submodule: &Module, exports: &Vec, @@ -225,13 +223,11 @@ fn use_qualified_module(module: &mut Option, submodule: &Module, exports if let &mut Some(ref mut module) = module { module.use_qualified_module(submodule, exports); } - - // self.wam.use_qualified_module_in_toplevel(name, exports); } -pub fn compile_listing(wam: &mut Machine, src_str: &str) -> EvalSession { - let mut indices = machine_code_index!(&mut CodeDir::new(), &mut default_op_dir()); - +pub +fn compile_listing(wam: &mut Machine, src_str: &str, mut indices: MachineCodeIndex) -> EvalSession +{ let mut worker = TopLevelBatchWorker::new(src_str.as_bytes(), wam.atom_tbl()); let mut compiler = ListingCompiler::new(wam); @@ -266,3 +262,15 @@ pub fn compile_listing(wam: &mut Machine, src_str: &str) -> EvalSession { EvalSession::EntrySuccess } + +pub fn compile_user_module(wam: &mut Machine, src_str: &str) -> EvalSession { + let mut indices = machine_code_index!(&mut CodeDir::new(), &mut default_op_dir()); + + if let Some(ref builtins) = wam.modules.get(&clause_name!("builtins")) { + indices.use_module(builtins); + } else { + return EvalSession::from(SessionError::ModuleNotFound); + } + + compile_listing(wam, src_str, indices) +} diff --git a/src/prolog/lib/control.pl b/src/prolog/lib/control.pl index 4f641e59..94090a05 100644 --- a/src/prolog/lib/control.pl +++ b/src/prolog/lib/control.pl @@ -1,5 +1,3 @@ -:- use_module(library(builtins)). - :- module(control, [(\=)/2, (\+)/1, between/3, once/1, repeat/0]). :- op(900, fy, \+). diff --git a/src/prolog/lib/lists.pl b/src/prolog/lib/lists.pl index 05725838..1c5317da 100644 --- a/src/prolog/lib/lists.pl +++ b/src/prolog/lib/lists.pl @@ -1,5 +1,3 @@ -:- use_module(library(builtins)). - :- module(lists, [member/2, select/3, append/3, memberchk/2, reverse/2, length/2, maplist/2, maplist/3, maplist/4, maplist/5, maplist/6, maplist/7, diff --git a/src/prolog/lib/queues.pl b/src/prolog/lib/queues.pl index f1faf80b..1cc1e6c8 100644 --- a/src/prolog/lib/queues.pl +++ b/src/prolog/lib/queues.pl @@ -1,5 +1,3 @@ -:- use_module(library(builtins)). - :- module(queues, [queue/1, queue/2, queue_head/3, queue_head_list/3, queue_last/3, queue_last_list/3, list_queue/2, queue_length/2]). diff --git a/src/prolog/machine/mod.rs b/src/prolog/machine/mod.rs index cac185c9..e42981f0 100644 --- a/src/prolog/machine/mod.rs +++ b/src/prolog/machine/mod.rs @@ -111,11 +111,12 @@ impl Machine { cached_query: None }; - compile_listing(&mut wam, BUILTINS); + let indices = machine_code_index!(&mut CodeDir::new(), &mut default_op_dir()); + compile_listing(&mut wam, BUILTINS, indices); - compile_listing(&mut wam, LISTS); - compile_listing(&mut wam, CONTROL); - compile_listing(&mut wam, QUEUES); + compile_user_module(&mut wam, LISTS); + compile_user_module(&mut wam, CONTROL); + compile_user_module(&mut wam, QUEUES); wam.use_module_in_toplevel(clause_name!("builtins"));