#![forbid(unsafe_code)]
use std::fmt;
use std::marker::PhantomData;
use delegate::delegate;
use itertools::Itertools;
use merc_utilities::MercError;
use crate::ATerm;
use crate::ATermArgs;
use crate::ATermIndex;
use crate::ATermRef;
use crate::SymbolRef;
use crate::Term;
use crate::TermIterator;
use crate::storage::THREAD_TERM_POOL;
pub fn is_list_term<'a, 'b, T: Term<'a, 'b>>(t: &'b T) -> bool {
THREAD_TERM_POOL.with(|tp| *tp.list_symbol() == t.get_head_symbol())
}
pub(crate) fn is_empty_list_term<'a, 'b, T: Term<'a, 'b>>(t: &'b T) -> bool {
THREAD_TERM_POOL.with(|tp| *tp.empty_list_symbol() == t.get_head_symbol())
}
pub struct ATermList<T> {
term: ATerm,
_marker: PhantomData<T>,
}
impl<T: From<ATerm>> ATermList<T> {
pub fn head(&self) -> T {
self.term.arg(0).protect().into()
}
pub fn to_vec(&self) -> Vec<T> {
self.iter().collect()
}
}
impl<T> ATermList<T> {
pub fn from_double_iter<I>(iter: I) -> Self
where
T: Into<ATerm>,
I: DoubleEndedIterator<Item = T>,
{
let mut list = Self::empty();
for item in iter.rev() {
list = list.cons(item);
}
list
}
pub fn try_from_double_iter<I>(iter: I) -> Result<Self, MercError>
where
T: Into<ATerm>,
I: DoubleEndedIterator<Item = Result<T, MercError>>,
{
let mut list = Self::empty();
for item in iter.rev() {
list = list.cons(item?);
}
Ok(list)
}
pub fn cons(&self, item: T) -> Self
where
T: Into<ATerm>,
{
ATermList {
term: THREAD_TERM_POOL
.with(|tp| ATerm::with_args(tp.list_symbol(), &[item.into().copy(), self.term.copy()]).protect()),
_marker: PhantomData,
}
}
pub fn empty() -> Self {
ATermList {
term: THREAD_TERM_POOL.with(|tp| ATerm::constant(tp.empty_list_symbol())),
_marker: PhantomData,
}
}
pub fn is_empty(&self) -> bool {
is_empty_list_term(&self.term)
}
pub fn tail(&self) -> ATermList<T> {
self.term.arg(1).into()
}
pub fn iter(&self) -> ATermListIter<T> {
ATermListIter { current: self.clone() }
}
}
impl<'a, 'b, T> Term<'a, 'b> for ATermList<T>
where
'b: 'a,
{
delegate! {
to self.term {
fn protect(&self) -> ATerm;
fn arg(&'b self, index: usize) -> ATermRef<'a>;
fn arguments(&'b self) -> ATermArgs<'a>;
fn copy(&'b self) -> ATermRef<'a>;
fn borrow(&self) -> ATermRef<'_>;
fn get_head_symbol(&'b self) -> SymbolRef<'a>;
fn iter(&'b self) -> TermIterator<'a>;
fn index(&self) -> usize;
fn shared(&self) -> &ATermIndex;
}
}
}
impl<T> Clone for ATermList<T> {
fn clone(&self) -> Self {
ATermList {
term: self.term.clone(),
_marker: PhantomData,
}
}
}
impl<T> From<ATermList<T>> for ATerm {
fn from(value: ATermList<T>) -> Self {
value.term
}
}
impl<T: From<ATerm>> Iterator for ATermListIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.current.is_empty() {
None
} else {
let head = self.current.head();
self.current = self.current.tail();
Some(head)
}
}
}
impl<T> From<ATerm> for ATermList<T> {
fn from(value: ATerm) -> Self {
debug_assert!(
is_list_term(&value) || is_empty_list_term(&value),
"Can only convert an aterm_list"
);
ATermList::<T> {
term: value,
_marker: PhantomData,
}
}
}
impl<'a, T> From<ATermRef<'a>> for ATermList<T> {
fn from(value: ATermRef<'a>) -> Self {
debug_assert!(
is_list_term(&value) || is_empty_list_term(&value),
"Can only convert an aterm_list"
);
ATermList::<T> {
term: value.protect(),
_marker: PhantomData,
}
}
}
impl<T: From<ATerm>> IntoIterator for ATermList<T> {
type IntoIter = ATermListIter<T>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T: From<ATerm>> IntoIterator for &ATermList<T> {
type IntoIter = ATermListIter<T>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T: From<ATerm> + fmt::Display> fmt::Display for ATermList<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}]", self.iter().format(","))
}
}
pub struct ATermListIter<T> {
current: ATermList<T>,
}
#[cfg(test)]
mod tests {
use crate::ATermInt;
use crate::ATermList;
#[test]
fn test_list_term() {
let list = ATermList::from_double_iter(vec![ATermInt::new(1), ATermInt::new(2), ATermInt::new(3)].into_iter());
assert_eq!(list.head().value(), 1);
assert_eq!(list.tail().head().value(), 2);
assert_eq!(list.tail().tail().head().value(), 3);
assert!(list.tail().tail().tail().is_empty());
}
}