Docs for Machine and QueryState

This commit is contained in:
bakaq
2024-09-29 23:30:57 -03:00
parent c13fc2d1b6
commit cb040dafc1
2 changed files with 14 additions and 6 deletions

View File

@@ -14,6 +14,7 @@ use super::{
MachineConfig, PrologTerm, MachineConfig, PrologTerm,
}; };
/// An iterator though the leaf answers of a query.
pub struct QueryState<'a> { pub struct QueryState<'a> {
machine: &'a mut Machine, machine: &'a mut Machine,
term: TermWriteResult, term: TermWriteResult,
@@ -146,17 +147,20 @@ impl Iterator for QueryState<'_> {
} }
impl Machine { impl Machine {
/// Creates a new [`Machine`] configured for use as a library.
pub fn new_lib() -> Self { pub fn new_lib() -> Self {
Machine::new(MachineConfig::default().with_streams(StreamConfig::in_memory())) Machine::new(MachineConfig::default().with_streams(StreamConfig::in_memory()))
} }
pub fn load_module_string(&mut self, module_name: &str, program: String) { /// Loads a module into the [`Machine`] from a string.
let stream = Stream::from_owned_string(program, &mut self.machine_st.arena); pub fn load_module_string(&mut self, module_name: &str, program: impl Into<String>) {
let stream = Stream::from_owned_string(program.into(), &mut self.machine_st.arena);
self.load_file(module_name, stream); self.load_file(module_name, stream);
} }
pub fn consult_module_string(&mut self, module_name: &str, program: String) { /// Consults a module into the [`Machine`] from a string.
let stream = Stream::from_owned_string(program, &mut self.machine_st.arena); pub fn consult_module_string(&mut self, module_name: &str, program: impl Into<String>) {
let stream = Stream::from_owned_string(program.into(), &mut self.machine_st.arena);
self.machine_st.registers[1] = stream_as_cell!(stream); self.machine_st.registers[1] = stream_as_cell!(stream);
self.machine_st.registers[2] = atom_as_cell!(&atom_table::AtomTable::build_with( self.machine_st.registers[2] = atom_as_cell!(&atom_table::AtomTable::build_with(
&self.machine_st.atom_tbl, &self.machine_st.atom_tbl,
@@ -190,9 +194,10 @@ impl Machine {
self.machine_st.block = stub_b; self.machine_st.block = stub_b;
} }
pub fn run_query(&mut self, query: String) -> QueryState { /// Runs a query.
pub fn run_query(&mut self, query: impl Into<String>) -> QueryState {
let mut parser = Parser::new( let mut parser = Parser::new(
Stream::from_owned_string(query, &mut self.machine_st.arena), Stream::from_owned_string(query.into(), &mut self.machine_st.arena),
&mut self.machine_st, &mut self.machine_st,
); );
let op_dir = CompositeOpDir::new(&self.indices.op_dir, None); let op_dir = CompositeOpDir::new(&self.indices.op_dir, None);

View File

@@ -70,6 +70,7 @@ lazy_static! {
pub static ref INTERRUPT: AtomicBool = AtomicBool::new(false); pub static ref INTERRUPT: AtomicBool = AtomicBool::new(false);
} }
/// An instance of Scryer Prolog.
#[derive(Debug)] #[derive(Debug)]
pub struct Machine { pub struct Machine {
pub(super) machine_st: MachineState, pub(super) machine_st: MachineState,
@@ -262,6 +263,7 @@ impl Machine {
) )
} }
/// Gets the current inference count.
pub fn get_inference_count(&mut self) -> u64 { pub fn get_inference_count(&mut self) -> u64 {
self.machine_st self.machine_st
.cwil .cwil
@@ -480,6 +482,7 @@ impl Machine {
} }
} }
/// Creates a new [`Machine`] from a [`MachineConfig`].
#[allow(clippy::new_without_default)] #[allow(clippy::new_without_default)]
pub fn new(config: MachineConfig) -> Self { pub fn new(config: MachineConfig) -> Self {
let args = MachineArgs::new(); let args = MachineArgs::new();