Skip to content

Commit

Permalink
split into lib and bin
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Skgland committed Feb 28, 2021
1 parent f935060 commit 2f428b7
Show file tree
Hide file tree
Showing 35 changed files with 981 additions and 964 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
libraries
.write_all(
b"ref_thread_local::ref_thread_local! {
pub static managed LIBRARIES: IndexMap<&'static str, &'static str> = {
pub(crate) static managed LIBRARIES: IndexMap<&'static str, &'static str> = {
let mut m = IndexMap::new();\n",
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::targets::*;
use std::cell::Cell;
use std::rc::Rc;

pub trait Allocator<'a> {
pub(crate) trait Allocator<'a> {
fn new() -> Self;

fn mark_anon_var<Target>(&mut self, _: Level, _: GenContext, _: &mut Vec<Target>)
Expand Down
22 changes: 11 additions & 11 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use std::rc::Rc;
use std::vec::Vec;

#[derive(Debug)]
pub struct ArithInstructionIterator<'a> {
pub(crate) struct ArithInstructionIterator<'a> {
state_stack: Vec<TermIterState<'a>>,
}

pub type ArithCont = (Code, Option<ArithmeticTerm>);
pub(crate) type ArithCont = (Code, Option<ArithmeticTerm>);

impl<'a> ArithInstructionIterator<'a> {
fn push_subterm(&mut self, lvl: Level, term: &'a Term) {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<'a> ArithInstructionIterator<'a> {
}

#[derive(Debug)]
pub enum ArithTermRef<'a> {
pub(crate) enum ArithTermRef<'a> {
Constant(&'a Constant),
Op(ClauseName, usize), // name, arity.
Var(&'a Cell<VarReg>, Rc<Var>),
Expand Down Expand Up @@ -113,13 +113,13 @@ impl<'a> Iterator for ArithInstructionIterator<'a> {
}

#[derive(Debug)]
pub struct ArithmeticEvaluator<'a> {
pub(crate) struct ArithmeticEvaluator<'a> {
bindings: &'a AllocVarDict,
interm: Vec<ArithmeticTerm>,
interm_c: usize,
}

pub trait ArithmeticTermIter<'a> {
pub(crate) trait ArithmeticTermIter<'a> {
type Iter: Iterator<Item = Result<ArithTermRef<'a>, ArithmeticError>>;

fn iter(self) -> Result<Self::Iter, ArithmeticError>;
Expand All @@ -134,7 +134,7 @@ impl<'a> ArithmeticTermIter<'a> for &'a Term {
}

impl<'a> ArithmeticEvaluator<'a> {
pub fn new(bindings: &'a AllocVarDict, target_int: usize) -> Self {
pub(crate) fn new(bindings: &'a AllocVarDict, target_int: usize) -> Self {
ArithmeticEvaluator {
bindings,
interm: Vec::new(),
Expand Down Expand Up @@ -296,7 +296,7 @@ impl<'a> ArithmeticEvaluator<'a> {
Ok(())
}

pub fn eval<Iter>(&mut self, src: Iter) -> Result<ArithCont, ArithmeticError>
pub(crate) fn eval<Iter>(&mut self, src: Iter) -> Result<ArithCont, ArithmeticError>
where
Iter: ArithmeticTermIter<'a>,
{
Expand Down Expand Up @@ -329,7 +329,7 @@ impl<'a> ArithmeticEvaluator<'a> {
}

// integer division rounding function -- 9.1.3.1.
pub fn rnd_i<'a>(n: &'a Number) -> RefOrOwned<'a, Number> {
pub(crate) fn rnd_i<'a>(n: &'a Number) -> RefOrOwned<'a, Number> {
match n {
&Number::Integer(_) => RefOrOwned::Borrowed(n),
&Number::Float(OrderedFloat(f)) => RefOrOwned::Owned(Number::from(
Expand All @@ -347,7 +347,7 @@ pub fn rnd_i<'a>(n: &'a Number) -> RefOrOwned<'a, Number> {
}

// floating point rounding function -- 9.1.4.1.
pub fn rnd_f(n: &Number) -> f64 {
pub(crate) fn rnd_f(n: &Number) -> f64 {
match n {
&Number::Fixnum(n) => n as f64,
&Number::Integer(ref n) => n.to_f64(),
Expand All @@ -357,7 +357,7 @@ pub fn rnd_f(n: &Number) -> f64 {
}

// floating point result function -- 9.1.4.2.
pub fn result_f<Round>(n: &Number, round: Round) -> Result<f64, EvalError>
pub(crate) fn result_f<Round>(n: &Number, round: Round) -> Result<f64, EvalError>
where
Round: Fn(&Number) -> f64,
{
Expand Down Expand Up @@ -752,7 +752,7 @@ impl<'a> From<&'a Integer> for Number {
}

// Computes n ^ power. Ignores the sign of power.
pub fn binary_pow(mut n: Integer, power: &Integer) -> Integer {
pub(crate) fn binary_pow(mut n: Integer, power: &Integer) -> Integer {
let mut power = Integer::from(power.abs_ref());

if power == 0 {
Expand Down
11 changes: 11 additions & 0 deletions src/bin/scryer-prolog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
use nix::sys::signal;
use scryer_prolog::read::readline;
use scryer_prolog::*;

let handler = signal::SigHandler::Handler(handle_sigint);
unsafe { signal::signal(signal::Signal::SIGINT, handler) }.unwrap();

let mut wam = machine::Machine::new(readline::input_stream(), machine::Stream::stdout());
wam.run_top_level();
}
58 changes: 28 additions & 30 deletions src/clause_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ref_thread_local::{ref_thread_local, RefThreadLocal};
use std::collections::BTreeMap;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CompareNumberQT {
pub(crate) enum CompareNumberQT {
GreaterThan,
LessThan,
GreaterThanOrEqual,
Expand All @@ -33,7 +33,7 @@ impl CompareNumberQT {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareTermQT {
pub(crate) enum CompareTermQT {
LessThan,
LessThanOrEqual,
GreaterThanOrEqual,
Expand All @@ -52,14 +52,14 @@ impl CompareTermQT {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ArithmeticTerm {
pub(crate) enum ArithmeticTerm {
Reg(RegType),
Interm(usize),
Number(Number),
}

impl ArithmeticTerm {
pub fn interm_or(&self, interm: usize) -> usize {
pub(crate) fn interm_or(&self, interm: usize) -> usize {
if let &ArithmeticTerm::Interm(interm) = self {
interm
} else {
Expand All @@ -69,7 +69,7 @@ impl ArithmeticTerm {
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum InlinedClauseType {
pub(crate) enum InlinedClauseType {
CompareNumber(CompareNumberQT, ArithmeticTerm, ArithmeticTerm),
IsAtom(RegType),
IsAtomic(RegType),
Expand All @@ -83,11 +83,11 @@ pub enum InlinedClauseType {
}

ref_thread_local! {
pub static managed RANDOM_STATE: RandState<'static> = RandState::new();
pub(crate)static managed RANDOM_STATE: RandState<'static> = RandState::new();
}

ref_thread_local! {
pub static managed CLAUSE_TYPE_FORMS: BTreeMap<(&'static str, usize), ClauseType> = {
pub(crate)static managed CLAUSE_TYPE_FORMS: BTreeMap<(&'static str, usize), ClauseType> = {
let mut m = BTreeMap::new();

let r1 = temp_v!(1);
Expand Down Expand Up @@ -133,7 +133,7 @@ ref_thread_local! {
}

impl InlinedClauseType {
pub fn name(&self) -> &'static str {
pub(crate) fn name(&self) -> &'static str {
match self {
&InlinedClauseType::CompareNumber(qt, ..) => qt.name(),
&InlinedClauseType::IsAtom(..) => "atom",
Expand All @@ -150,7 +150,7 @@ impl InlinedClauseType {
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SystemClauseType {
pub(crate) enum SystemClauseType {
AtomChars,
AtomCodes,
AtomLength,
Expand Down Expand Up @@ -311,7 +311,7 @@ pub enum SystemClauseType {
}

impl SystemClauseType {
pub fn name(&self) -> ClauseName {
pub(crate) fn name(&self) -> ClauseName {
match self {
&SystemClauseType::AtomChars => clause_name!("$atom_chars"),
&SystemClauseType::AtomCodes => clause_name!("$atom_codes"),
Expand Down Expand Up @@ -596,7 +596,7 @@ impl SystemClauseType {
}
}

pub fn from(name: &str, arity: usize) -> Option<SystemClauseType> {
pub(crate) fn from(name: &str, arity: usize) -> Option<SystemClauseType> {
match (name, arity) {
("$abolish_clause", 3) => Some(SystemClauseType::REPL(REPLCodePtr::AbolishClause)),
("$add_dynamic_predicate", 4) => {
Expand All @@ -605,9 +605,9 @@ impl SystemClauseType {
("$add_multifile_predicate", 4) => {
Some(SystemClauseType::REPL(REPLCodePtr::AddMultifilePredicate))
}
("$add_discontiguous_predicate", 4) => {
Some(SystemClauseType::REPL(REPLCodePtr::AddDiscontiguousPredicate))
}
("$add_discontiguous_predicate", 4) => Some(SystemClauseType::REPL(
REPLCodePtr::AddDiscontiguousPredicate,
)),
("$add_goal_expansion_clause", 3) => {
Some(SystemClauseType::REPL(REPLCodePtr::AddGoalExpansionClause))
}
Expand Down Expand Up @@ -766,18 +766,16 @@ impl SystemClauseType {
("$asserta", 5) => Some(SystemClauseType::REPL(REPLCodePtr::Asserta)),
("$assertz", 5) => Some(SystemClauseType::REPL(REPLCodePtr::Assertz)),
("$retract_clause", 4) => Some(SystemClauseType::REPL(REPLCodePtr::Retract)),
("$is_consistent_with_term_queue", 4) => {
Some(SystemClauseType::REPL(REPLCodePtr::IsConsistentWithTermQueue))
}
("$flush_term_queue", 1) => {
Some(SystemClauseType::REPL(REPLCodePtr::FlushTermQueue))
}
("$is_consistent_with_term_queue", 4) => Some(SystemClauseType::REPL(
REPLCodePtr::IsConsistentWithTermQueue,
)),
("$flush_term_queue", 1) => Some(SystemClauseType::REPL(REPLCodePtr::FlushTermQueue)),
("$remove_module_exports", 2) => {
Some(SystemClauseType::REPL(REPLCodePtr::RemoveModuleExports))
}
("$add_non_counted_backtracking", 3) => {
Some(SystemClauseType::REPL(REPLCodePtr::AddNonCountedBacktracking))
}
("$add_non_counted_backtracking", 3) => Some(SystemClauseType::REPL(
REPLCodePtr::AddNonCountedBacktracking,
)),
("$variant", 2) => Some(SystemClauseType::Variant),
("$wam_instructions", 4) => Some(SystemClauseType::WAMInstructions),
("$write_term", 7) => Some(SystemClauseType::WriteTerm),
Expand Down Expand Up @@ -848,7 +846,7 @@ impl SystemClauseType {
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum BuiltInClauseType {
pub(crate) enum BuiltInClauseType {
AcyclicTerm,
Arg,
Compare,
Expand All @@ -866,7 +864,7 @@ pub enum BuiltInClauseType {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClauseType {
pub(crate) enum ClauseType {
BuiltIn(BuiltInClauseType),
CallN,
Inlined(InlinedClauseType),
Expand All @@ -876,7 +874,7 @@ pub enum ClauseType {
}

impl BuiltInClauseType {
pub fn name(&self) -> ClauseName {
pub(crate) fn name(&self) -> ClauseName {
match self {
&BuiltInClauseType::AcyclicTerm => clause_name!("acyclic_term"),
&BuiltInClauseType::Arg => clause_name!("arg"),
Expand All @@ -895,7 +893,7 @@ impl BuiltInClauseType {
}
}

pub fn arity(&self) -> usize {
pub(crate) fn arity(&self) -> usize {
match self {
&BuiltInClauseType::AcyclicTerm => 1,
&BuiltInClauseType::Arg => 3,
Expand All @@ -916,7 +914,7 @@ impl BuiltInClauseType {
}

impl ClauseType {
pub fn spec(&self) -> Option<SharedOpDesc> {
pub(crate) fn spec(&self) -> Option<SharedOpDesc> {
match self {
&ClauseType::Op(_, ref spec, _) => Some(spec.clone()),
&ClauseType::Inlined(InlinedClauseType::CompareNumber(..))
Expand All @@ -928,7 +926,7 @@ impl ClauseType {
}
}

pub fn name(&self) -> ClauseName {
pub(crate) fn name(&self) -> ClauseName {
match self {
&ClauseType::BuiltIn(ref built_in) => built_in.name(),
&ClauseType::CallN => clause_name!("$call"),
Expand All @@ -939,7 +937,7 @@ impl ClauseType {
}
}

pub fn from(name: ClauseName, arity: usize, spec: Option<SharedOpDesc>) -> Self {
pub(crate) fn from(name: ClauseName, arity: usize, spec: Option<SharedOpDesc>) -> Self {
CLAUSE_TYPE_FORMS
.borrow()
.get(&(name.as_str(), arity))
Expand Down
Loading

0 comments on commit 2f428b7

Please sign in to comment.