delete the old parser

main
Jordan Orelli 11 months ago
parent aa6bcedcf0
commit f0dc5f2e64

@ -6,7 +6,6 @@ mod lex;
mod line; mod line;
mod log; mod log;
mod output; mod output;
mod parse;
mod parse2; mod parse2;
mod prompt; mod prompt;
mod shell; mod shell;

@ -1,112 +0,0 @@
use crate::error::ParseError;
use std::{cell::RefCell, fmt, rc::Rc};
#[derive(PartialEq, Clone)]
pub enum Element {
Empty,
Command(String),
Literal(String),
Glob(String),
}
impl fmt::Debug for Element {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
Element::Empty => write!(f, "()"),
Element::Command(cmd) => write!(f, "cmd<{}>", cmd),
Element::Literal(lit) => write!(f, "lit<{}>", lit),
Element::Glob(glob) => write!(f, "glob<{}>", glob),
}
}
}
pub struct Node {
pub elem: Element,
pub parent: Option<Rc<RefCell<Node>>>,
pub children: Vec<Rc<RefCell<Node>>>,
}
impl Node {
pub fn new(elem: Element) -> Self {
Self {
elem,
parent: None,
children: Vec::new(),
}
}
pub fn empty() -> Self {
Self::new(Element::Empty)
}
#[allow(dead_code)]
pub fn child_of(parent: Rc<RefCell<Self>>, elem: Element) -> Self {
Self {
elem,
parent: Some(parent),
children: Vec::new(),
}
}
}
impl fmt::Debug for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{:?}", self.elem)?;
if self.children.len() > 0 {
write!(f, "[")?;
for child in self.children.iter() {
write!(f, "{:?}", child.borrow())?;
}
write!(f, "]")?;
}
Ok(())
}
}
pub struct Tree {
target: Rc<RefCell<Node>>,
}
impl fmt::Debug for Tree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{:?}", self.target.borrow())
}
}
pub struct ChildIter {
/// pointer to the node in the tree whose children we are looking at.
parent: Rc<RefCell<Node>>,
idx: usize,
}
impl Iterator for ChildIter {
type Item = Tree;
fn next(&mut self) -> Option<Self::Item> {
if self.idx >= self.parent.borrow().children.len() {
None
} else {
let child = Rc::clone(&self.parent.borrow().children[self.idx]);
self.idx += 1;
Some(Tree { target: child })
}
}
}
impl From<Tree> for Node {
fn from(value: Tree) -> Self {
value.target.replace(Node::empty())
}
}
impl From<Node> for Tree {
fn from(value: Node) -> Self {
Self {
target: Rc::new(RefCell::new(value)),
}
}
}
pub fn parse<S: AsRef<str>>(text: S) -> Result<Tree, ParseError> {
todo!()
}

@ -4,7 +4,6 @@ use crate::{
line::Line, line::Line,
log::*, log::*,
output, output,
parse::{Element, Node},
}; };
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -112,28 +111,6 @@ impl Shell {
} }
} }
pub fn exec(&mut self, root: Node) -> Result<bool> {
match root.elem {
Element::Empty => Ok(true),
Element::Command(cmd) => {
let args: Vec<String> = root
.children
.iter()
.map(|node| match &node.borrow().elem {
Element::Literal(v) => v.clone(),
_ => todo!(),
})
.collect();
self.eval(
cmd.to_string(),
args.iter().map(|arg| arg.as_str()).collect(),
)
}
Element::Literal(_) => todo!(),
Element::Glob(_) => todo!(),
}
}
pub fn eval(&mut self, cmd: String, args: Vec<&str>) -> Result<bool> { pub fn eval(&mut self, cmd: String, args: Vec<&str>) -> Result<bool> {
match cmd.as_str() { match cmd.as_str() {
"pwd" => { "pwd" => {

Loading…
Cancel
Save