From: bakaq Date: Mon, 30 Sep 2024 02:09:47 +0000 (-0300) Subject: More PrologTerm documentation X-Git-Tag: v0.10.0~92^2~21 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=0433706db68c54604f175a671ba203231f10140e;p=scryer-prolog.git More PrologTerm documentation --- diff --git a/src/machine/config.rs b/src/machine/config.rs index 79752f31..b968e624 100644 --- a/src/machine/config.rs +++ b/src/machine/config.rs @@ -55,7 +55,7 @@ impl MachineConfig { Default::default() } - /// Uses the given `crate::StreamConfig` in this configuration. + /// Uses the given `crate::StreamConfig` in this configuration. pub fn with_streams(mut self, streams: StreamConfig) -> Self { self.streams = streams; self diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index abfe87bf..4137a985 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -1,17 +1,17 @@ use std::collections::BTreeMap; -use crate::{atom_table, StreamConfig}; use crate::machine::machine_indices::VarKey; use crate::machine::mock_wam::CompositeOpDir; use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS}; use crate::parser::ast::{Var, VarPtr}; use crate::parser::parser::{Parser, Tokens}; use crate::read::{write_term_to_heap, TermWriteResult}; +use crate::{atom_table, StreamConfig}; use indexmap::IndexMap; use super::{ - streams::Stream, Atom, AtomCell, HeapCellValue, HeapCellValueTag, Machine, MachineConfig, - LeafAnswer, PrologTerm, + streams::Stream, Atom, AtomCell, HeapCellValue, HeapCellValueTag, LeafAnswer, Machine, + MachineConfig, PrologTerm, }; pub struct QueryState<'a> { @@ -138,7 +138,10 @@ impl Iterator for QueryState<'_> { // choice point, so we should break. self.machine.machine_st.backtrack(); - Some(Ok(LeafAnswer::LeafAnswer { bindings: bindings, residual_goals: vec![] })) + Some(Ok(LeafAnswer::LeafAnswer { + bindings: bindings, + residual_goals: vec![], + })) } } diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 93d15ee1..3688bc97 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -27,13 +27,23 @@ pub enum LeafAnswer { #[non_exhaustive] #[derive(Debug, Clone, PartialEq, Eq)] pub enum PrologTerm { + /// An arbitrary precision integer. Integer(Integer), + /// An arbitrary precision rational. Rational(Rational), + /// A float. Float(OrderedFloat), + /// A Prolog atom. Atom(String), + /// A Prolog string. + /// + /// In particular, this represents Prolog lists of characters. String(String), + /// A Prolog list. List(Vec), + /// A Prolog compound term. Compound(String, Vec), + /// A Prolog variable. Var(String), } @@ -91,12 +101,11 @@ impl PrologTerm { /// Creates a conjunction, giving `None` if empty. pub fn try_conjunction(value: impl IntoIterator) -> Option { let mut iter = value.into_iter(); - iter.next() - .map(|first| { - PrologTerm::try_conjunction(iter) - .map(|rest| PrologTerm::compound(",", [first.clone(), rest])) - .unwrap_or(first) - }) + iter.next().map(|first| { + PrologTerm::try_conjunction(iter) + .map(|rest| PrologTerm::compound(",", [first.clone(), rest])) + .unwrap_or(first) + }) } /// Creates a disjunction, giving the atom `false` if empty. @@ -107,12 +116,11 @@ impl PrologTerm { /// Creates a disjunction, giving `None` if empty. pub fn try_disjunction(value: impl IntoIterator) -> Option { let mut iter = value.into_iter(); - iter.next() - .map(|first| { - PrologTerm::try_disjunction(iter) - .map(|rest| PrologTerm::compound(";", [first.clone(), rest])) - .unwrap_or(first) - }) + iter.next().map(|first| { + PrologTerm::try_disjunction(iter) + .map(|rest| PrologTerm::compound(";", [first.clone(), rest])) + .unwrap_or(first) + }) } } @@ -122,12 +130,17 @@ impl From for PrologTerm { LeafAnswer::True => PrologTerm::atom("true"), LeafAnswer::False => PrologTerm::atom("false"), LeafAnswer::Exception(inner) => match inner.clone() { - PrologTerm::Compound(functor, args) if functor == "error" && args.len() == 2 => inner, + PrologTerm::Compound(functor, args) if functor == "error" && args.len() == 2 => { + inner + } _ => PrologTerm::compound("throw", [inner]), }, - LeafAnswer::LeafAnswer { bindings: _, residual_goals: _ } => { + LeafAnswer::LeafAnswer { + bindings: _, + residual_goals: _, + } => { todo!() - }, + } } } }