mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
wip: still trying to write the iterator
This commit is contained in:
parent
140fd22223
commit
433e4df0f2
7 changed files with 114 additions and 66 deletions
|
|
@ -12,6 +12,19 @@ impl<T> Default for EdnItem<T> {
|
|||
Self::Nil
|
||||
}
|
||||
}
|
||||
impl<T: Debug> Debug for EdnItem<T> {
|
||||
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
|
||||
match self {
|
||||
Self::Nil => write!(f, "Nil"),
|
||||
Self::Num(u) => write!(f, "Num({u})"),
|
||||
Self::Sym(u) => write!(f, "Sym({u:?})"),
|
||||
Self::Key(u) => write!(f, "Key({u:?})"),
|
||||
Self::Exp(e) => write!(f, "Exp({})", itertools::join(
|
||||
e.iter().map(|i|format!("{:?}", i)), ","
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: AsRef<str> + PartialEq + Default + Clone + std::fmt::Debug> EdnItem<T> {
|
||||
pub fn to_ref (&self) -> EdnItem<&str> {
|
||||
match self {
|
||||
|
|
@ -25,40 +38,42 @@ impl<T: AsRef<str> + PartialEq + Default + Clone + std::fmt::Debug> EdnItem<T> {
|
|||
_ => todo!()
|
||||
}
|
||||
}
|
||||
pub fn symbols <'a> (&'a self) -> SymIterator<'a, T> {
|
||||
SymIterator::new(&self)
|
||||
pub fn symbols <'a> (&'a self) -> EdnIterator<'a, T> {
|
||||
EdnIterator::new(&self)
|
||||
}
|
||||
}
|
||||
enum SymIterator<'a, T> {
|
||||
enum EdnIterator<'a, T>{
|
||||
Nil,
|
||||
Sym(&'a T),
|
||||
Exp(&'a [EdnItem<T>])
|
||||
Exp(Vec<EdnIterator<'a, T>>)
|
||||
}
|
||||
impl<'a, T: AsRef<str>> SymIterator<'a, T> {
|
||||
impl<'a, T: AsRef<str>> EdnIterator<'a, T> {
|
||||
fn new (item: &'a EdnItem<T>) -> Self {
|
||||
use EdnItem::*;
|
||||
match item {
|
||||
Sym(t) => Self::Sym(t),
|
||||
Exp(i) => Self::Exp(i.as_slice()),
|
||||
Exp(i) => Self::Exp(i.iter().map(EdnIterator::new).collect()),
|
||||
_ => Self::Nil,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, T: AsRef<str>> Iterator for SymIterator<'a, T> {
|
||||
type Item = &'a str;
|
||||
impl<'a, T: AsRef<str>> Iterator for EdnIterator<'a, T> {
|
||||
type Item = &'a T;
|
||||
fn next (&mut self) -> Option<Self::Item> {
|
||||
use SymIterator::*;
|
||||
use EdnIterator::*;
|
||||
match self {
|
||||
Sym(t) => {
|
||||
let t = *t;
|
||||
*self = Nil;
|
||||
Some(t.as_ref())
|
||||
Some(t)
|
||||
},
|
||||
Exp([a, b @ ..]) => match a {
|
||||
EdnItem::Sym(t) => {
|
||||
*self = Exp(b);
|
||||
Some(t.as_ref())
|
||||
}
|
||||
EdnItem::Exp(_) => todo!(),
|
||||
Exp(v) => match v.as_mut_slice() {
|
||||
[a] => if let Some(next) = a.next() {
|
||||
Some(next)
|
||||
} else {
|
||||
*self = Exp(v.split_off(1));
|
||||
self.next()
|
||||
},
|
||||
_ => {
|
||||
*self = Nil;
|
||||
None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue