|
|
@ -5,7 +5,7 @@ use crate::{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use std::{collections::HashSet, process};
|
|
|
|
use std::{collections::HashSet, process};
|
|
|
|
|
|
|
|
|
|
|
|
struct State {}
|
|
|
|
pub struct State {}
|
|
|
|
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
impl State {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
pub fn new() -> Self {
|
|
|
@ -13,12 +13,12 @@ impl State {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
trait Eval {
|
|
|
|
pub trait Eval {
|
|
|
|
fn eval(&self, ctx: &mut State) -> Result<Value, ExecError>;
|
|
|
|
fn eval(&self, ctx: &mut State) -> Result<Value, ExecError>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Element {
|
|
|
|
pub enum Element {
|
|
|
|
Block(Block),
|
|
|
|
Block(Block),
|
|
|
|
Command(Command),
|
|
|
|
Command(Command),
|
|
|
|
Literal(Value),
|
|
|
|
Literal(Value),
|
|
|
@ -36,7 +36,7 @@ impl Eval for Element {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Value {
|
|
|
|
pub enum Value {
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
Text(String),
|
|
|
|
Text(String),
|
|
|
|
ExitStatus(process::ExitStatus),
|
|
|
|
ExitStatus(process::ExitStatus),
|
|
|
@ -61,7 +61,7 @@ impl Value {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Block {
|
|
|
|
pub struct Block {
|
|
|
|
commands: Vec<Command>,
|
|
|
|
commands: Vec<Command>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -84,7 +84,7 @@ impl Eval for Block {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Command {
|
|
|
|
pub struct Command {
|
|
|
|
name: Box<Element>,
|
|
|
|
name: Box<Element>,
|
|
|
|
args: Vec<Element>,
|
|
|
|
args: Vec<Element>,
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -123,11 +123,9 @@ impl TreeBuilder {
|
|
|
|
fn descend(&mut self, source: &mut parse2::Cursor) -> Result<Element, ParseError> {
|
|
|
|
fn descend(&mut self, source: &mut parse2::Cursor) -> Result<Element, ParseError> {
|
|
|
|
let e = match source.value() {
|
|
|
|
let e = match source.value() {
|
|
|
|
parse2::Value::Start => {
|
|
|
|
parse2::Value::Start => {
|
|
|
|
println!("start node");
|
|
|
|
|
|
|
|
let mut root = Block::new();
|
|
|
|
let mut root = Block::new();
|
|
|
|
let children = source.iter_children();
|
|
|
|
let children = source.iter_children();
|
|
|
|
for mut child in children {
|
|
|
|
for mut child in children {
|
|
|
|
println!("child");
|
|
|
|
|
|
|
|
let e = self.descend(&mut child)?;
|
|
|
|
let e = self.descend(&mut child)?;
|
|
|
|
match e {
|
|
|
|
match e {
|
|
|
|
Element::Command(cmd) => root.commands.push(cmd),
|
|
|
|
Element::Command(cmd) => root.commands.push(cmd),
|
|
|
@ -137,7 +135,6 @@ impl TreeBuilder {
|
|
|
|
Element::Block(root)
|
|
|
|
Element::Block(root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parse2::Value::Statement => {
|
|
|
|
parse2::Value::Statement => {
|
|
|
|
println!("statement node");
|
|
|
|
|
|
|
|
let mut children = source.iter_children();
|
|
|
|
let mut children = source.iter_children();
|
|
|
|
let mut first = children.next().unwrap();
|
|
|
|
let mut first = children.next().unwrap();
|
|
|
|
let name = self.descend(&mut first)?;
|
|
|
|
let name = self.descend(&mut first)?;
|
|
|
@ -152,14 +149,12 @@ impl TreeBuilder {
|
|
|
|
Element::Command(cmd)
|
|
|
|
Element::Command(cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parse2::Value::Terminal(Token::Word(word)) => {
|
|
|
|
parse2::Value::Terminal(Token::Word(word)) => {
|
|
|
|
println!("terminal word node");
|
|
|
|
|
|
|
|
Element::Literal(Value::Text(word.to_string()))
|
|
|
|
Element::Literal(Value::Text(word.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parse2::Value::Terminal(Token::Glob(_)) => {
|
|
|
|
parse2::Value::Terminal(Token::Glob(_)) => {
|
|
|
|
todo!()
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
println!("visited: {:?}", source.target.id);
|
|
|
|
|
|
|
|
self.visited.insert(source.target.id);
|
|
|
|
self.visited.insert(source.target.id);
|
|
|
|
Ok(e)
|
|
|
|
Ok(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -171,11 +166,10 @@ fn build(mut source: parse2::Cursor) -> Result<Element, ParseError> {
|
|
|
|
builder.descend(&mut source)
|
|
|
|
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 tokens = Lexer::new(source);
|
|
|
|
let parser = parse2::Parser::new(tokens);
|
|
|
|
let parser = parse2::Parser::new(tokens);
|
|
|
|
let mut parse_tree = parser.parse()?;
|
|
|
|
let mut parse_tree = parser.parse()?;
|
|
|
|
println!("parse tree: {parse_tree:?}");
|
|
|
|
|
|
|
|
let mut builder = TreeBuilder::new();
|
|
|
|
let mut builder = TreeBuilder::new();
|
|
|
|
builder.descend(&mut parse_tree)
|
|
|
|
builder.descend(&mut parse_tree)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -187,9 +181,10 @@ mod test {
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn hi() -> Result<(), ParseError> {
|
|
|
|
fn hi() -> Result<(), ParseError> {
|
|
|
|
let e = x("ls one two three ; ls four")?;
|
|
|
|
let e = x("ls one two three")?;
|
|
|
|
print!("{:?}", e);
|
|
|
|
print!("{:?}", e);
|
|
|
|
Ok(())
|
|
|
|
todo!()
|
|
|
|
|
|
|
|
//Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|