Contine work on wide addresses

This commit is contained in:
Austen Adler 2023-04-18 17:51:17 -04:00
parent 9f03f43abf
commit d371a0786d

View File

@ -1,3 +1,6 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::rc::Weak;
use std::{fmt::Display, str::FromStr};
use nom::{
@ -22,10 +25,118 @@ pub struct Addresses<'a> {
addresses: Vec<Address<'a>>,
}
pub struct TreeNode<'a> {
parent: Option<Weak<RefCell<TreeNode<'a>>>>,
children: Vec<Rc<RefCell<TreeNode<'a>>>>,
token: Token<'a>,
}
impl<'a> Addresses<'a> {
// fn to_compact_tokens(&self) -> impl Iterator<Item=Token<'a>> {
// }
fn from_tokens(mut tokens: Vec<Token>) -> Result<Self, Error> {
// 123 and 456 a b c
// First, flip the order
tokens.reverse();
// c b a 456 and 123
// Next, build the tree
let mut depth = 0;
// let mut root_node = Rc::new(RefCell::new(TreeNode {
// parent: None,
// children: vec![],
// }));
let mut ret = vec![];
let mut last_node = None;
for token in tokens.into_iter() {
match token {
Token::Word(_) => {
// The number must be inserted at depth 3 only
if depth >= 3 {
panic!("{token:?} found at depth {depth}");
}
let parent = last_node.as_ref().map(|ln| Rc::downgrade(&ln));
let children = vec![];
let new_node = Rc::new(RefCell::new(TreeNode {
parent,
children,
token,
}));
if let Some(ln) = last_node.as_ref() {
// Now add the child
ln.borrow_mut().children.push(Rc::clone(&new_node));
} else {
// This is the first node
ret.push(Rc::clone(&new_node));
}
depth += 1;
last_node = Some(new_node);
}
Token::Number(_) => {
// The number must be inserted at depth 3 only
if depth != 3 {
panic!("{token:?} found at depth {depth}");
}
let parent = last_node.as_ref().map(|ln| Rc::downgrade(&ln));
let children = vec![];
let new_node = Rc::new(RefCell::new(TreeNode {
parent,
children,
token,
}));
if let Some(ln) = last_node.as_ref() {
// Now add the child
ln.borrow_mut().children.push(Rc::clone(&new_node));
} else {
// This is the first node
ret.push(Rc::clone(&new_node));
}
depth += 1;
last_node = Some(new_node);
}
Token::WideSeparator => {
// 123 and 999 a and 456 b c d
// Tree:
// d
// c
// a b
// 123 999 456
//
// last creaed: 123 last_created_depth= depth=3
// and
// last creaed: 999 last_created_depth=3 depth=3
// last creaed: a last_created_depth=3 depth=2
// and
// last_creaed: 456 last_created_depth=2 depth=3
// last_creaed: b last_created_depth=3 depth=2
// d c b 456 and a 999 and 123
// Tree:
// d Depth=0
// c Depth=1
// b Depth=2
// 456 Depth=0
}
}
}
todo![]
}
}
impl<'a> From<Vec<Address<'a>>> for Addresses<'a> {