[package]
name = "scryer-prolog"
-version = "0.8.94"
+version = "0.8.95"
+build = "build.rs"
repository = "https://github.com/mthom/scryer-prolog"
description = "A modern Prolog implementation written mostly in Rust."
license = "BSD-3-Clause"
+[build-dependencies]
+indexmap = "1.0.2"
+
[dependencies]
cfg-if = "0.1.7"
dirs = "2.0.2"
--- /dev/null
+extern crate indexmap;
+
+use indexmap::IndexSet;
+
+use std::fs::{File, read_dir};
+use std::io::Write;
+use std::path::Path;
+
+fn main()
+{
+ let dest_path = Path::new("./src/prolog/machine/libraries.rs");
+
+ let mut libraries = File::create(&dest_path).unwrap();
+ let mut library_index = IndexSet::new();
+
+ let paths = read_dir("./src/prolog/lib").unwrap();
+
+ for item in paths {
+ let item = item.unwrap().path();
+
+ if item.is_file() {
+ if let Some(ext) = item.extension() {
+ if ext == "pl" {
+ let file_stem = item.file_stem().unwrap();
+ let file_str = file_stem.to_string_lossy().to_uppercase();
+
+ let include_line = format!("static {}: &str = include_str!(\"{}/{}.pl\");\n",
+ file_str, "../lib", file_stem.to_string_lossy());
+
+ libraries.write_all(include_line.as_bytes()).unwrap();
+ library_index.insert(file_stem.to_string_lossy().to_string());
+ }
+ }
+ }
+ }
+
+ libraries.write_all(b"\nref_thread_local! {
+ pub static managed LIBRARIES: IndexMap<&'static str, &'static str> = {
+ let mut m = IndexMap::new();\n").unwrap();
+
+ for item in library_index {
+ let line = format!("\n m.insert(\"{}\", {});", item, item.to_uppercase());
+ libraries.write_all(line.as_bytes()).unwrap();
+ }
+
+ libraries.write_all(b"\n\n m\n };
+}").unwrap();
+}
:- module(domain, [domain/2]).
-:- use_module('src/prolog/lib/atts').
-:- use_module('src/prolog/lib/ordsets', [
+:- use_module(library(atts)).
+:- use_module(library(ordsets), [
ord_intersection/3,
ord_intersect/2,
list_to_ord_set/2
-:- use_module('src/prolog/lib/dcgs').
-:- use_module('src/prolog/lib/reif').
+:- use_module(library(dcgs)).
+:- use_module(library(reif)).
animals([animal(dog, [is_true('has fur'), is_true('says woof')]),
animal(cat, [is_true('has fur'), is_true('says meow')]),
:- module(zdd, [variables_set_zdd/2]).
-:- use_module('src/prolog/lib/atts').
-:- use_module('src/prolog/lib/dcgs').
-:- use_module('src/prolog/lib/lists').
+:- use_module(library(atts)).
+:- use_module(library(dcgs)).
+:- use_module(library(lists)).
:- attribute zdd_vs/2.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
-:- use_module('src/prolog/lib/dcgs').
-:- use_module('src/prolog/lib/dif').
-:- use_module('src/prolog/lib/lists').
+:- use_module(library(dcgs)).
+:- use_module(library(dif)).
+:- use_module(library(lists)).
pl_resolution(Clauses0, Chain) :-
maplist(sort, Clauses0, Clauses), % remove duplicates
del_max_assoc/4 % +Assoc0, ?Key, ?Value, ?Assoc
]).
-:- use_module('src/prolog/lib/lists.pl').
+:- use_module(library(lists)).
/** <module> Binary associations
'$add_to_list'/3, '$del_attr'/3, '$del_attr_step'/3,
'$del_attr_buried'/4]).
-:- use_module('src/prolog/lib/dcgs.pl').
-:- use_module('src/prolog/lib/terms.pl').
+:- use_module(library(dcgs)).
+:- use_module(library(terms)).
:- op(1199, fx, attribute).
%% TODO: numlist/5.
-:- use_module('src/prolog/lib/lists.pl', [length/2]).
-:- use_module('src/prolog/lib/error.pl').
+:- use_module(library(lists), [length/2]).
+:- use_module(library(error)).
between(Lower, Upper, X) :-
must_be(integer, Lower),
:- module(dcgs, [phrase/2, phrase/3]).
-:- use_module('src/prolog/lib/lists.pl', [append/3]).
-:- use_module('src/prolog/lib/terms.pl').
+:- use_module(library(lists), [append/3]).
+:- use_module(library(terms)).
phrase(G, G) :-
nonvar(G), G = [_|_], !.
:- module(diag, [wam_instructions/2]).
-:- use_module('src/prolog/lib/error').
+:- use_module(library(error)).
wam_instructions(Clause, Listing) :-
( nonvar(Clause) ->
:- module(dif, [dif/2]).
-:- use_module('src/prolog/lib/atts.pl').
-:- use_module('src/prolog/lib/lists.pl', [append/3]).
+:- use_module(library(atts)).
+:- use_module(library(lists), [append/3]).
:- attribute dif/1.
:- module(freeze, [freeze/2]).
-:- use_module('src/prolog/lib/atts.pl').
+:- use_module(library(atts)).
:- attribute frozen/1.
ord_intersection/2 % +PowerSet, -Intersection
]).
-:- use_module('src/prolog/lib/lists.pl').
+:- use_module(library(lists)).
/** <module> Ordered set manipulation
Ordered sets are lists with unique elements sorted to the standard order
memberd_t/3, tfilter/3, tmember/2, tmember_t/3,
tpartition/4]).
-:- use_module('src/prolog/lib/dif.pl').
+:- use_module(library(dif)).
if_(If_1, Then_0, Else_0) :-
call(If_1, T),
:- module(terms, [numbervars/3]).
-:- use_module('src/prolog/lib/error.pl').
+:- use_module(library(error)).
numbervars(Term, N0, N) :-
catch(internal_numbervars(Term, N0, N), error(E,Ctx),
use indexmap::{IndexMap, IndexSet};
+use ref_thread_local::RefThreadLocal;
+
use std::cell::Cell;
use std::collections::VecDeque;
use std::fs::File;
Ok(path)
}
-fn load_module_from_file(wam: &mut Machine, filename: &str) -> Result<ClauseName, SessionError> {
- let path = fix_filename(wam.indices.atom_tbl.clone(), filename)?;
-
- let file_handle = File::open(&path).or_else(|_| {
- let filename = clause_name!(path.to_string_lossy().to_string(), wam.indices.atom_tbl);
- Err(SessionError::InvalidFileName(filename))
- })?;
-
- let file_src = parsing_stream(file_handle);
-
+fn load_module<R: Read>(wam: &mut Machine, name: &str, stream: ParsingStream<R>)
+ -> Result<ClauseName, SessionError>
+{
// follow the operation of compile_user_module, but before
// compiling, check that a module is declared in the file. if not,
// throw an exception.
setup_indices(wam, clause_name!("builtins"), &mut indices)?;
let mut compiler = ListingCompiler::new(&wam.code_repo);
- let results = compiler.gather_items(wam, file_src, &mut indices)?;
+ let results = compiler.gather_items(wam, stream, &mut indices)?;
let module_name = if let Some(ref module) = &compiler.module {
module.module_decl.name.clone()
} else {
- let module_name = path.to_string_lossy().to_string();
- let module_name = clause_name!(module_name, wam.indices.atom_tbl);
-
+ let module_name = clause_name!(name.to_string(), wam.indices.atom_tbl);
return Err(SessionError::NoModuleDeclaration(module_name));
};
match compile_work_impl(&mut compiler, wam, indices, results) {
EvalSession::Error(e) => Err(e),
_ => Ok(module_name),
- }
+ }
+}
+
+fn load_module_from_file(wam: &mut Machine, filename: &str) -> Result<ClauseName, SessionError>
+{
+ let path = fix_filename(wam.indices.atom_tbl.clone(), filename)?;
+
+ let file_handle = File::open(&path).or_else(|_| {
+ let filename = clause_name!(path.to_string_lossy().to_string(), wam.indices.atom_tbl);
+ Err(SessionError::InvalidFileName(filename))
+ })?;
+
+ let file_stem = path.file_stem().unwrap().to_string_lossy();
+ load_module(wam, &file_stem, parsing_stream(file_handle))
}
pub type PredicateCompileQueue = (Predicate, VecDeque<TopLevel>);
Ok(())
}
+fn load_library(wam: &mut Machine, name: ClauseName) -> Result<ClauseName, SessionError> {
+ match LIBRARIES.borrow().get(name.as_str()) {
+ Some(code) => load_module(wam, name.as_str(), parsing_stream(code.as_bytes())),
+ None => Err(SessionError::ModuleNotFound)
+ }
+}
+
impl ListingCompiler {
#[inline]
pub fn new(code_repo: &CodeRepo) -> Self {
op_decl.submit(self.get_module_name(), spec, &mut indices.op_dir)
}
Declaration::UseModule(ModuleSource::Library(name)) => {
+ let name = if !wam.indices.modules.contains_key(&name) {
+ load_library(wam, name)?
+ } else {
+ name
+ };
+
self.use_module(name, &mut wam.code_repo, flags, &mut wam.indices, indices)
}
- Declaration::UseQualifiedModule(ModuleSource::Library(name), exports) => self
- .use_qualified_module(
+ Declaration::UseQualifiedModule(ModuleSource::Library(name), exports) => {
+ let name = if !wam.indices.modules.contains_key(&name) {
+ load_library(wam, name)?
+ } else {
+ name
+ };
+
+ self.use_qualified_module(
name,
&mut wam.code_repo,
flags,
&exports,
&mut wam.indices,
- indices,
- ),
+ indices
+ )
+ },
Declaration::Module(module_decl) => {
if self.module.is_none() {
let module_name = module_decl.name.clone();
}
}
-static BUILTINS: &str = include_str!("../lib/builtins.pl");
-static ERROR: &str = include_str!("../lib/error.pl");
-static LISTS: &str = include_str!("../lib/lists.pl");
-static NON_ISO: &str = include_str!("../lib/non_iso.pl");
+include!("libraries.rs");
+
static TOPLEVEL: &str = include_str!("../toplevel.pl");
impl Machine {
self.throw_session_error(err, (clause_name!("repl"), 0));
return;
}
- EvalSession::QueryFailure => {
+ EvalSession::QueryFailure =>
if self.machine_st.ball.stub.len() > 0 {
return self.propagate_exception_to_toplevel(snapshot);
} else {
println!("false.");
- }
- }
- _ => {}
+ },
+ _ => println!("true.")
}
self.machine_st.absorb_snapshot(snapshot);
if PROMPT { "?- " } else { "" }
}
}
-
+
pub struct ReadlineStream {
rl: Editor<()>,
pending_input: String,
impl ReadlineStream {
fn input_stream(pending_input: String) -> Self {
let mut rl = Editor::<()>::new();
-
rl.bind_sequence(KeyPress::Tab, Cmd::Insert(1, "\t".to_string()));
-
ReadlineStream { rl, pending_input }
}
}
}
- self.pending_input += "\n";
+ self.pending_input += "\n";
Ok(self.write_to_buf(buf))
}
Err(ReadlineError::Eof) =>
- Ok(self.write_to_buf(buf)),
+ Ok(self.write_to_buf(buf)),
Err(e) =>
Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, e))
}