Allow a bare expression that will be evaluated and echo'd to stdin

This commit is contained in:
Dane Johnson 2022-02-14 15:37:46 -06:00
parent 15055b9ece
commit 688b48ef42
2 changed files with 4 additions and 2 deletions

View File

@ -10,7 +10,7 @@ fn repl() {
let mut line = String::new(); let mut line = String::new();
io::stdin().read_line(&mut line).unwrap(); io::stdin().read_line(&mut line).unwrap();
let tree = parser::parse_stmt(&line); let tree = parser::parse_stmt(&line);
println!("{:?}", tree); println!("{:#?}", tree);
} }
} }

View File

@ -3,6 +3,7 @@ pub enum Stmt {
Assignment(String, Expr), Assignment(String, Expr),
Funcall(Expr), Funcall(Expr),
Conditional(Vec<Expr>, Option<Expr>), Conditional(Vec<Expr>, Option<Expr>),
ReplPrint(Expr),
} }
#[derive(Debug,PartialEq,Clone)] #[derive(Debug,PartialEq,Clone)]
@ -34,7 +35,8 @@ peg::parser! {
pub rule stmt() -> Stmt pub rule stmt() -> Stmt
= a:assignment() { a } / = a:assignment() { a } /
f:funcall() stop() { Stmt::Funcall(f) } / f:funcall() stop() { Stmt::Funcall(f) } /
c:conditional() { c } c:conditional() { c } /
e:expr() stop() { Stmt::ReplPrint(e) }
rule expr() -> Expr = precedence! { rule expr() -> Expr = precedence! {
e1:(@) "+" _ e2:@ { Expr::Plus(Box::new(e1), Box::new(e2)) } e1:(@) "+" _ e2:@ { Expr::Plus(Box::new(e1), Box::new(e2)) }
e1:(@) "-" _ e2:@ { Expr::Minus(Box::new(e1), Box::new(e2)) } e1:(@) "-" _ e2:@ { Expr::Minus(Box::new(e1), Box::new(e2)) }