split into lib and bin

* makes most pub things in src/ pub(crate) as not to expose things accidentally
  * only those things needed by src/bin/scryer-prolog.rs and tests/scryer.rs
    should be pub
* split src/main.rs into src/lib.rs and src/bin/scryer-prolog.rs
* add tests folder and run most of the files in src/tests with cargo test
  added bytes method to Stream in src/machine/streams.rs to check if stdout is as expected
This commit is contained in:
Skgland
2021-02-25 23:24:20 +01:00
parent f935060b2b
commit 2f428b7261
35 changed files with 981 additions and 964 deletions

View File

@@ -13,7 +13,7 @@ use std::rc::Rc;
use std::vec::Vec;
#[derive(Debug, Clone)]
pub enum TermRef<'a> {
pub(crate) enum TermRef<'a> {
AnonVar(Level),
Cons(Level, &'a Cell<RegType>, &'a Term, &'a Term),
Constant(Level, &'a Cell<RegType>, &'a Constant),
@@ -23,7 +23,7 @@ pub enum TermRef<'a> {
}
impl<'a> TermRef<'a> {
pub fn level(self) -> Level {
pub(crate) fn level(self) -> Level {
match self {
TermRef::AnonVar(lvl)
| TermRef::Cons(lvl, ..)
@@ -36,7 +36,7 @@ impl<'a> TermRef<'a> {
}
#[derive(Debug)]
pub enum TermIterState<'a> {
pub(crate) enum TermIterState<'a> {
AnonVar(Level),
Constant(Level, &'a Cell<RegType>, &'a Constant),
Clause(
@@ -97,7 +97,7 @@ fn is_partial_string<'a>(head: &'a Term, mut tail: &'a Term) -> Option<(String,
}
impl<'a> TermIterState<'a> {
pub fn subterm_to_state(lvl: Level, term: &'a Term) -> TermIterState<'a> {
pub(crate) fn subterm_to_state(lvl: Level, term: &'a Term) -> TermIterState<'a> {
match term {
&Term::AnonVar => TermIterState::AnonVar(lvl),
&Term::Clause(ref cell, ref name, ref subterms, ref spec) => {
@@ -119,7 +119,7 @@ impl<'a> TermIterState<'a> {
}
#[derive(Debug)]
pub struct QueryIterator<'a> {
pub(crate) struct QueryIterator<'a> {
state_stack: Vec<TermIterState<'a>>,
}
@@ -286,7 +286,7 @@ impl<'a> Iterator for QueryIterator<'a> {
}
#[derive(Debug)]
pub struct FactIterator<'a> {
pub(crate) struct FactIterator<'a> {
state_queue: VecDeque<TermIterState<'a>>,
iterable_root: bool,
}
@@ -297,7 +297,7 @@ impl<'a> FactIterator<'a> {
.push_back(TermIterState::subterm_to_state(lvl, term));
}
pub fn from_rule_head_clause(terms: &'a Vec<Box<Term>>) -> Self {
pub(crate) fn from_rule_head_clause(terms: &'a Vec<Box<Term>>) -> Self {
let state_queue = terms
.iter()
.map(|bt| TermIterState::subterm_to_state(Level::Shallow, bt.as_ref()))
@@ -386,26 +386,26 @@ impl<'a> Iterator for FactIterator<'a> {
}
}
pub fn post_order_iter(term: &Term) -> QueryIterator {
pub(crate) fn post_order_iter(term: &Term) -> QueryIterator {
QueryIterator::from_term(term)
}
pub fn breadth_first_iter(term: &Term, iterable_root: bool) -> FactIterator {
pub(crate) fn breadth_first_iter(term: &Term, iterable_root: bool) -> FactIterator {
FactIterator::new(term, iterable_root)
}
#[derive(Debug)]
pub enum ChunkedTerm<'a> {
pub(crate) enum ChunkedTerm<'a> {
HeadClause(ClauseName, &'a Vec<Box<Term>>),
BodyTerm(&'a QueryTerm),
}
pub fn query_term_post_order_iter<'a>(query_term: &'a QueryTerm) -> QueryIterator<'a> {
pub(crate) fn query_term_post_order_iter<'a>(query_term: &'a QueryTerm) -> QueryIterator<'a> {
QueryIterator::new(query_term)
}
impl<'a> ChunkedTerm<'a> {
pub fn post_order_iter(&self) -> QueryIterator<'a> {
pub(crate) fn post_order_iter(&self) -> QueryIterator<'a> {
match self {
&ChunkedTerm::BodyTerm(ref qt) => QueryIterator::new(qt),
&ChunkedTerm::HeadClause(_, terms) => QueryIterator::from_rule_head_clause(terms),
@@ -425,8 +425,8 @@ fn contains_cut_var<'a, Iter: Iterator<Item = &'a Term>>(terms: Iter) -> bool {
false
}
pub struct ChunkedIterator<'a> {
pub chunk_num: usize,
pub(crate) struct ChunkedIterator<'a> {
pub(crate) chunk_num: usize,
iter: Box<dyn Iterator<Item = ChunkedTerm<'a>> + 'a>,
deep_cut_encountered: bool,
cut_var_in_head: bool,
@@ -448,7 +448,7 @@ type ChunkedIteratorItem<'a> = (usize, usize, Vec<ChunkedTerm<'a>>);
type RuleBodyIteratorItem<'a> = (usize, usize, Vec<&'a QueryTerm>);
impl<'a> ChunkedIterator<'a> {
pub fn rule_body_iter(self) -> Box<dyn Iterator<Item = RuleBodyIteratorItem<'a>> + 'a> {
pub(crate) fn rule_body_iter(self) -> Box<dyn Iterator<Item = RuleBodyIteratorItem<'a>> + 'a> {
Box::new(self.filter_map(|(cn, lt_arity, terms)| {
let filtered_terms: Vec<_> = terms
.into_iter()
@@ -466,7 +466,7 @@ impl<'a> ChunkedIterator<'a> {
}))
}
/*
pub fn from_term_sequence(terms: &'a [QueryTerm]) -> Self {
pub(crate) fn from_term_sequence(terms: &'a [QueryTerm]) -> Self {
ChunkedIterator {
chunk_num: 0,
iter: Box::new(terms.iter().map(|t| ChunkedTerm::BodyTerm(t))),
@@ -475,7 +475,7 @@ impl<'a> ChunkedIterator<'a> {
}
}
*/
pub fn from_rule_body(p1: &'a QueryTerm, clauses: &'a Vec<QueryTerm>) -> Self {
pub(crate) fn from_rule_body(p1: &'a QueryTerm, clauses: &'a Vec<QueryTerm>) -> Self {
let inner_iter = Box::new(once(ChunkedTerm::BodyTerm(p1)));
let iter = inner_iter.chain(clauses.iter().map(|t| ChunkedTerm::BodyTerm(t)));
@@ -487,7 +487,7 @@ impl<'a> ChunkedIterator<'a> {
}
}
pub fn from_rule(rule: &'a Rule) -> Self {
pub(crate) fn from_rule(rule: &'a Rule) -> Self {
let &Rule {
head: (ref name, ref args, ref p1),
ref clauses,
@@ -505,7 +505,7 @@ impl<'a> ChunkedIterator<'a> {
}
}
pub fn encountered_deep_cut(&self) -> bool {
pub(crate) fn encountered_deep_cut(&self) -> bool {
self.deep_cut_encountered
}