From 49badc37a41039e98cad7b93d7e9ba09747b0e91 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Tue, 1 Feb 2022 10:57:04 -0600 Subject: [PATCH] Blocks are a kind of expression now --- rust/src/parser.rs | 41 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/rust/src/parser.rs b/rust/src/parser.rs index 52b5ffd..06a48db 100644 --- a/rust/src/parser.rs +++ b/rust/src/parser.rs @@ -9,16 +9,11 @@ pub enum Expr<'a> { Id(&'a str), Num(f64), Funcall(&'a str, Vec>), - Funcdef(Option<&'a str>, Fun<'a>), + Funcdef(Option<&'a str>, Box>), Plus(Box>, Box>), Minus(Box>, Box>), Mult(Box>, Box>), Div(Box>, Box>), -} - -#[derive(Debug,PartialEq)] -pub enum Fun<'a> { - Oner(Box>), Block(Vec>), } @@ -42,6 +37,8 @@ peg::parser! { i:id() _ { Expr::Id(i) } n:num() _ { Expr::Num(n) } } + rule block() -> Expr<'input> + = stop() indent() __* s:stmt()+ dedent() { Expr::Block(s) } rule id() -> &'input str = i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i } @@ -50,8 +47,8 @@ peg::parser! { rule funcall() -> Expr<'input> = i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) } rule funcdef() -> Expr<'input> - = i:id()? "->" _ e:expr() { Expr::Funcdef(i, Fun::Oner(Box::new(e))) } / - i:id()? "->" _ stop() indent() __* s:stmt()+ dedent() { Expr::Funcdef(i, Fun::Block(s)) } + = i:id()? "->" _ e:expr() { Expr::Funcdef(i, Box::new(e)) } / + i:id()? "->" _ b:block() { Expr::Funcdef(i, Box::new(b)) } rule letter() = ['A'..='Z'] / ['a'..='z'] @@ -139,7 +136,7 @@ four <- (3 - 1) * 2"; Stmt::Assignment("four", Expr::Mult( Box::new(Expr::Minus( - Box::new(Expr::Num(3.0)), + Box::new(Expr::Num(3.0)), Box::new(Expr::Num(1.0)), )), Box::new(Expr::Num(2.0)), @@ -179,36 +176,30 @@ foo <- x -> y -> x * y"; "foo", Expr::Funcdef( None, - Fun::Oner( - Box::new(Expr::Funcall("bar", vec![])), - ) + Box::new(Expr::Funcall("bar", vec![])), ) ), Stmt::Assignment( "foo", Expr::Funcdef( None, - Fun::Block(vec![ + Box::new(Expr::Block(vec![ Stmt::Funcall(Expr::Funcall("bar", vec![])), Stmt::Funcall(Expr::Funcall("baz", vec![])), - ]) + ])) ) ), Stmt::Assignment( "foo", Expr::Funcdef( Some("x"), - Fun::Oner( - Box::new( - Expr::Funcdef( - Some("y"), - Fun::Oner( - Box::new( - Expr::Mult( - Box::new(Expr::Id("x")), - Box::new(Expr::Id("y")), - ) - ) + Box::new( + Expr::Funcdef( + Some("y"), + Box::new( + Expr::Mult( + Box::new(Expr::Id("x")), + Box::new(Expr::Id("y")), ) ) )