|
|
|
@ -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<Value, ExecError>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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<Command>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -84,7 +84,7 @@ impl Eval for Block {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Command {
|
|
|
|
|
pub struct Command {
|
|
|
|
|
name: Box<Element>,
|
|
|
|
|
args: Vec<Element>,
|
|
|
|
|
}
|
|
|
|
@ -123,11 +123,9 @@ impl TreeBuilder {
|
|
|
|
|
fn descend(&mut self, source: &mut parse2::Cursor) -> Result<Element, ParseError> {
|
|
|
|
|
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<Element, ParseError> {
|
|
|
|
|
builder.descend(&mut source)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn x(source: &str) -> Result<Element, ParseError> {
|
|
|
|
|
pub fn x(source: &str) -> Result<Element, ParseError> {
|
|
|
|
|
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(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|