From aa6bcedcf0ffc4cd20b47a23d8421f0f45a18b95 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 31 Jan 2024 12:36:22 -0600 Subject: [PATCH] the new parser is doing parsing now --- src/log.rs | 2 +- src/main.rs | 20 +++++++++++++++++--- src/parse2.rs | 11 +---------- src/syntax.rs | 25 ++++++++++--------------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/log.rs b/src/log.rs index e5aa230..604ac0c 100644 --- a/src/log.rs +++ b/src/log.rs @@ -1,5 +1,5 @@ use crate::error::Error; -pub use log::{debug, info, set_logger, set_max_level, warn, LevelFilter}; +pub use log::{debug, error, info, set_logger, set_max_level, warn, LevelFilter}; use std::{ fs::File, diff --git a/src/main.rs b/src/main.rs index 099ad01..359df32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ mod syntax; use crate::log::*; use prompt::Prompt; use shell::Shell; +use syntax::Eval; use std::io::Write; @@ -55,9 +56,22 @@ fn main() -> Result<()> { shell.output.newline()?; let s = shell.line.pop(); info!("◇ {}", s); - let tree = parse::parse(&s)?; - debug!(" {:?}", tree); - shell.exec(tree.into())?; + // let tree = parse::parse(&s)?; + match syntax::x(&s) { + Ok(tree) => { + debug!(" {:?}", tree); + let mut state = syntax::State::new(); + if let Err(e) = tree.eval(&mut state) { + error!("{e:?}"); + println!("Error: {e:?}"); + } + } + Err(e) => { + error!("{e:?}"); + println!("Error: {e:?}"); + } + } + // shell.exec(tree.into())?; // Some commands don't leave the terminal in a clean state, so we use reset // to ensure that our input and output modes are what we expect them to be. shell.reset()?; diff --git a/src/parse2.rs b/src/parse2.rs index 950be60..265e5eb 100644 --- a/src/parse2.rs +++ b/src/parse2.rs @@ -69,10 +69,8 @@ impl Iterator for ChildIter { type Item = Cursor; fn next(&mut self) -> Option { - println!("next"); let children = self.target.children.borrow(); let v = children.get(self.idx)?; - println!("next has: {v:?}"); self.idx += 1; Some(Cursor { target: Rc::clone(v), @@ -176,7 +174,6 @@ impl<'text> Parser<'text> { } fn step(&mut self) -> Result { - println!("parser step..."); match self.cursor.value() { Value::Start => self.step_start(), Value::Statement => self.step_statement(), @@ -186,30 +183,24 @@ impl<'text> Parser<'text> { fn step_start(&mut self) -> Result { assert!(matches!(self.cursor.value(), Value::Start)); - println!("step start node"); match self.source.peek()? { Some(Token::Word(_)) => { self.cursor.push(Value::Statement)?; let token = self.source.next().unwrap()?; self.cursor.push(Value::Terminal(token))?; self.cursor.up()?; - println!("start node returns true..."); Ok(true) } Some(Token::Glob(_)) => { let token = self.source.next().unwrap()?; Err(ParseError::UnexpectedToken(token)) } - None => { - println!("start node has ... nothin peeked in source"); - Ok(false) - } + None => Ok(false), } } fn step_statement(&mut self) -> Result { assert!(matches!(self.cursor.value(), Value::Statement)); - println!("step statement node"); match self.source.peek()? { Some(Token::Word(_) | Token::Glob(_)) => { let token = self.source.next().unwrap()?; diff --git a/src/syntax.rs b/src/syntax.rs index 47f3c24..f7e3238 100644 --- a/src/syntax.rs +++ b/src/syntax.rs @@ -5,7 +5,7 @@ use crate::{ }; use std::{collections::HashSet, process}; -struct State {} +pub struct State {} impl State { pub fn new() -> Self { @@ -13,12 +13,12 @@ impl State { } } -trait Eval { +pub trait Eval { fn eval(&self, ctx: &mut State) -> Result; } #[derive(Debug)] -enum Element { +pub enum Element { Block(Block), Command(Command), Literal(Value), @@ -36,7 +36,7 @@ impl Eval for Element { } #[derive(Debug, Clone)] -enum Value { +pub enum Value { None, Text(String), ExitStatus(process::ExitStatus), @@ -61,7 +61,7 @@ impl Value { } #[derive(Debug)] -struct Block { +pub struct Block { commands: Vec, } @@ -84,7 +84,7 @@ impl Eval for Block { } #[derive(Debug)] -struct Command { +pub struct Command { name: Box, args: Vec, } @@ -123,11 +123,9 @@ impl TreeBuilder { fn descend(&mut self, source: &mut parse2::Cursor) -> Result { let e = match source.value() { parse2::Value::Start => { - println!("start node"); let mut root = Block::new(); let children = source.iter_children(); for mut child in children { - println!("child"); let e = self.descend(&mut child)?; match e { Element::Command(cmd) => root.commands.push(cmd), @@ -137,7 +135,6 @@ impl TreeBuilder { Element::Block(root) } parse2::Value::Statement => { - println!("statement node"); let mut children = source.iter_children(); let mut first = children.next().unwrap(); let name = self.descend(&mut first)?; @@ -152,14 +149,12 @@ impl TreeBuilder { Element::Command(cmd) } parse2::Value::Terminal(Token::Word(word)) => { - println!("terminal word node"); Element::Literal(Value::Text(word.to_string())) } parse2::Value::Terminal(Token::Glob(_)) => { todo!() } }; - println!("visited: {:?}", source.target.id); self.visited.insert(source.target.id); Ok(e) } @@ -171,11 +166,10 @@ fn build(mut source: parse2::Cursor) -> Result { builder.descend(&mut source) } -fn x(source: &str) -> Result { +pub fn x(source: &str) -> Result { let tokens = Lexer::new(source); let parser = parse2::Parser::new(tokens); let mut parse_tree = parser.parse()?; - println!("parse tree: {parse_tree:?}"); let mut builder = TreeBuilder::new(); builder.descend(&mut parse_tree) } @@ -187,9 +181,10 @@ mod test { #[test] fn hi() -> Result<(), ParseError> { - let e = x("ls one two three ; ls four")?; + let e = x("ls one two three")?; print!("{:?}", e); - Ok(()) + todo!() + //Ok(()) } }