More PrologTerm documentation
This commit is contained in:
@@ -1,17 +1,17 @@
|
|||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use crate::{atom_table, StreamConfig};
|
|
||||||
use crate::machine::machine_indices::VarKey;
|
use crate::machine::machine_indices::VarKey;
|
||||||
use crate::machine::mock_wam::CompositeOpDir;
|
use crate::machine::mock_wam::CompositeOpDir;
|
||||||
use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS};
|
use crate::machine::{BREAK_FROM_DISPATCH_LOOP_LOC, LIB_QUERY_SUCCESS};
|
||||||
use crate::parser::ast::{Var, VarPtr};
|
use crate::parser::ast::{Var, VarPtr};
|
||||||
use crate::parser::parser::{Parser, Tokens};
|
use crate::parser::parser::{Parser, Tokens};
|
||||||
use crate::read::{write_term_to_heap, TermWriteResult};
|
use crate::read::{write_term_to_heap, TermWriteResult};
|
||||||
|
use crate::{atom_table, StreamConfig};
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
streams::Stream, Atom, AtomCell, HeapCellValue, HeapCellValueTag, Machine, MachineConfig,
|
streams::Stream, Atom, AtomCell, HeapCellValue, HeapCellValueTag, LeafAnswer, Machine,
|
||||||
LeafAnswer, PrologTerm,
|
MachineConfig, PrologTerm,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct QueryState<'a> {
|
pub struct QueryState<'a> {
|
||||||
@@ -138,7 +138,10 @@ impl Iterator for QueryState<'_> {
|
|||||||
// choice point, so we should break.
|
// choice point, so we should break.
|
||||||
self.machine.machine_st.backtrack();
|
self.machine.machine_st.backtrack();
|
||||||
|
|
||||||
Some(Ok(LeafAnswer::LeafAnswer { bindings: bindings, residual_goals: vec![] }))
|
Some(Ok(LeafAnswer::LeafAnswer {
|
||||||
|
bindings: bindings,
|
||||||
|
residual_goals: vec![],
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,13 +27,23 @@ pub enum LeafAnswer {
|
|||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum PrologTerm {
|
pub enum PrologTerm {
|
||||||
|
/// An arbitrary precision integer.
|
||||||
Integer(Integer),
|
Integer(Integer),
|
||||||
|
/// An arbitrary precision rational.
|
||||||
Rational(Rational),
|
Rational(Rational),
|
||||||
|
/// A float.
|
||||||
Float(OrderedFloat<f64>),
|
Float(OrderedFloat<f64>),
|
||||||
|
/// A Prolog atom.
|
||||||
Atom(String),
|
Atom(String),
|
||||||
|
/// A Prolog string.
|
||||||
|
///
|
||||||
|
/// In particular, this represents Prolog lists of characters.
|
||||||
String(String),
|
String(String),
|
||||||
|
/// A Prolog list.
|
||||||
List(Vec<PrologTerm>),
|
List(Vec<PrologTerm>),
|
||||||
|
/// A Prolog compound term.
|
||||||
Compound(String, Vec<PrologTerm>),
|
Compound(String, Vec<PrologTerm>),
|
||||||
|
/// A Prolog variable.
|
||||||
Var(String),
|
Var(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,8 +101,7 @@ impl PrologTerm {
|
|||||||
/// Creates a conjunction, giving `None` if empty.
|
/// Creates a conjunction, giving `None` if empty.
|
||||||
pub fn try_conjunction(value: impl IntoIterator<Item = PrologTerm>) -> Option<Self> {
|
pub fn try_conjunction(value: impl IntoIterator<Item = PrologTerm>) -> Option<Self> {
|
||||||
let mut iter = value.into_iter();
|
let mut iter = value.into_iter();
|
||||||
iter.next()
|
iter.next().map(|first| {
|
||||||
.map(|first| {
|
|
||||||
PrologTerm::try_conjunction(iter)
|
PrologTerm::try_conjunction(iter)
|
||||||
.map(|rest| PrologTerm::compound(",", [first.clone(), rest]))
|
.map(|rest| PrologTerm::compound(",", [first.clone(), rest]))
|
||||||
.unwrap_or(first)
|
.unwrap_or(first)
|
||||||
@@ -107,8 +116,7 @@ impl PrologTerm {
|
|||||||
/// Creates a disjunction, giving `None` if empty.
|
/// Creates a disjunction, giving `None` if empty.
|
||||||
pub fn try_disjunction(value: impl IntoIterator<Item = PrologTerm>) -> Option<Self> {
|
pub fn try_disjunction(value: impl IntoIterator<Item = PrologTerm>) -> Option<Self> {
|
||||||
let mut iter = value.into_iter();
|
let mut iter = value.into_iter();
|
||||||
iter.next()
|
iter.next().map(|first| {
|
||||||
.map(|first| {
|
|
||||||
PrologTerm::try_disjunction(iter)
|
PrologTerm::try_disjunction(iter)
|
||||||
.map(|rest| PrologTerm::compound(";", [first.clone(), rest]))
|
.map(|rest| PrologTerm::compound(";", [first.clone(), rest]))
|
||||||
.unwrap_or(first)
|
.unwrap_or(first)
|
||||||
@@ -122,12 +130,17 @@ impl From<LeafAnswer> for PrologTerm {
|
|||||||
LeafAnswer::True => PrologTerm::atom("true"),
|
LeafAnswer::True => PrologTerm::atom("true"),
|
||||||
LeafAnswer::False => PrologTerm::atom("false"),
|
LeafAnswer::False => PrologTerm::atom("false"),
|
||||||
LeafAnswer::Exception(inner) => match inner.clone() {
|
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]),
|
_ => PrologTerm::compound("throw", [inner]),
|
||||||
},
|
},
|
||||||
LeafAnswer::LeafAnswer { bindings: _, residual_goals: _ } => {
|
LeafAnswer::LeafAnswer {
|
||||||
|
bindings: _,
|
||||||
|
residual_goals: _,
|
||||||
|
} => {
|
||||||
todo!()
|
todo!()
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user