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

@@ -3,58 +3,44 @@ use crate::instructions::*;
use crate::machine::machine_indices::*;
#[derive(Debug)]
pub struct CodeRepo {
pub(crate) struct CodeRepo {
pub(super) code: Code,
}
impl CodeRepo {
#[inline]
pub(super)
fn new() -> Self {
CodeRepo {
code: Code::new(),
}
pub(super) fn new() -> Self {
CodeRepo { code: Code::new() }
}
#[inline]
pub(super)
fn lookup_local_instr<'a>(
&'a self,
p: LocalCodePtr,
) -> RefOrOwned<'a, Line> {
pub(super) fn lookup_local_instr<'a>(&'a self, p: LocalCodePtr) -> RefOrOwned<'a, Line> {
match p {
LocalCodePtr::Halt => {
// exit with the interrupt exit code.
std::process::exit(1);
}
LocalCodePtr::DirEntry(p) => {
RefOrOwned::Borrowed(&self.code[p as usize])
}
LocalCodePtr::IndexingBuf(p, o, i) => {
match &self.code[p] {
&Line::IndexingCode(ref indexing_lines) => {
match &indexing_lines[o] {
&IndexingLine::IndexedChoice(ref indexed_choice_instrs) => {
RefOrOwned::Owned(Line::IndexedChoice(indexed_choice_instrs[i]))
}
&IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => {
RefOrOwned::Owned(Line::DynamicIndexedChoice(indexed_choice_instrs[i]))
}
_ => {
unreachable!()
}
}
LocalCodePtr::DirEntry(p) => RefOrOwned::Borrowed(&self.code[p as usize]),
LocalCodePtr::IndexingBuf(p, o, i) => match &self.code[p] {
&Line::IndexingCode(ref indexing_lines) => match &indexing_lines[o] {
&IndexingLine::IndexedChoice(ref indexed_choice_instrs) => {
RefOrOwned::Owned(Line::IndexedChoice(indexed_choice_instrs[i]))
}
&IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => {
RefOrOwned::Owned(Line::DynamicIndexedChoice(indexed_choice_instrs[i]))
}
_ => {
unreachable!()
}
},
_ => {
unreachable!()
}
}
},
}
}
pub(super)
fn lookup_instr<'a>(
pub(super) fn lookup_instr<'a>(
&'a self,
last_call: bool,
p: &CodePtr,
@@ -63,9 +49,7 @@ impl CodeRepo {
&CodePtr::Local(local) => {
return Some(self.lookup_local_instr(local));
}
&CodePtr::REPL(..) => {
None
}
&CodePtr::REPL(..) => None,
&CodePtr::BuiltInClause(ref built_in, _) => {
let call_clause = call_clause!(
ClauseType::BuiltIn(built_in.clone()),
@@ -77,26 +61,26 @@ impl CodeRepo {
Some(RefOrOwned::Owned(call_clause))
}
&CodePtr::CallN(arity, _, last_call) => {
let call_clause = call_clause!(
ClauseType::CallN,
arity,
0,
last_call
);
let call_clause = call_clause!(ClauseType::CallN, arity, 0, last_call);
Some(RefOrOwned::Owned(call_clause))
}
&CodePtr::VerifyAttrInterrupt(p) => {
Some(RefOrOwned::Borrowed(&self.code[p]))
}
&CodePtr::VerifyAttrInterrupt(p) => Some(RefOrOwned::Borrowed(&self.code[p])),
}
}
pub(super)
fn find_living_dynamic_else(&self, mut p: usize, cc: usize) -> Option<(usize, usize)> {
pub(super) fn find_living_dynamic_else(
&self,
mut p: usize,
cc: usize,
) -> Option<(usize, usize)> {
loop {
match &self.code[p] {
&Line::Choice(ChoiceInstruction::DynamicElse(birth, death, NextOrFail::Next(i))) => {
&Line::Choice(ChoiceInstruction::DynamicElse(
birth,
death,
NextOrFail::Next(i),
)) => {
if birth < cc && Death::Finite(cc) <= death {
return Some((p, i));
} else if i > 0 {
@@ -105,7 +89,11 @@ impl CodeRepo {
return None;
}
}
&Line::Choice(ChoiceInstruction::DynamicElse(birth, death, NextOrFail::Fail(_))) => {
&Line::Choice(ChoiceInstruction::DynamicElse(
birth,
death,
NextOrFail::Fail(_),
)) => {
if birth < cc && Death::Finite(cc) <= death {
return Some((p, 0));
} else {
@@ -128,8 +116,8 @@ impl CodeRepo {
&Line::Choice(ChoiceInstruction::DynamicInternalElse(
birth,
death,
NextOrFail::Fail(_)),
) => {
NextOrFail::Fail(_),
)) => {
if birth < cc && Death::Finite(cc) <= death {
return Some((p, 0));
} else {
@@ -146,8 +134,11 @@ impl CodeRepo {
}
}
pub(super)
fn find_living_dynamic(&self, p: LocalCodePtr, cc: usize) -> Option<(usize, usize, usize, bool)> {
pub(super) fn find_living_dynamic(
&self,
p: LocalCodePtr,
cc: usize,
) -> Option<(usize, usize, usize, bool)> {
let (p, oi, mut ii) = match p {
LocalCodePtr::IndexingBuf(p, oi, ii) => (p, oi, ii),
_ => unreachable!(),
@@ -158,27 +149,27 @@ impl CodeRepo {
IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => {
indexed_choice_instrs
}
_ => unreachable!()
}
_ => unreachable!()
_ => unreachable!(),
},
_ => unreachable!(),
};
loop {
match &indexed_choice_instrs.get(ii) {
Some(&offset) => {
match &self.code[p + offset - 1] {
&Line::Choice(ChoiceInstruction::DynamicInternalElse(
birth, death, next_or_fail,
)) => {
if birth < cc && Death::Finite(cc) <= death {
return Some((offset, oi, ii, next_or_fail.is_next()));
} else {
ii += 1;
}
Some(&offset) => match &self.code[p + offset - 1] {
&Line::Choice(ChoiceInstruction::DynamicInternalElse(
birth,
death,
next_or_fail,
)) => {
if birth < cc && Death::Finite(cc) <= death {
return Some((offset, oi, ii, next_or_fail.is_next()));
} else {
ii += 1;
}
_ => unreachable!(),
}
}
_ => unreachable!(),
},
None => return None,
}
}