Indentation and funcdefs

This commit is contained in:
Dane Johnson 2022-02-01 10:48:04 -06:00
parent 356905316e
commit 7669be9c59

View File

@ -9,13 +9,19 @@ pub enum Expr<'a> {
Id(&'a str), Id(&'a str),
Num(f64), Num(f64),
Funcall(&'a str, Vec<Expr<'a>>), Funcall(&'a str, Vec<Expr<'a>>),
Funcdef(Option<&'a str>, Vec<Stmt<'a>>), Funcdef(Option<&'a str>, Fun<'a>),
Plus(Box<Expr<'a>>, Box<Expr<'a>>), Plus(Box<Expr<'a>>, Box<Expr<'a>>),
Minus(Box<Expr<'a>>, Box<Expr<'a>>), Minus(Box<Expr<'a>>, Box<Expr<'a>>),
Mult(Box<Expr<'a>>, Box<Expr<'a>>), Mult(Box<Expr<'a>>, Box<Expr<'a>>),
Div(Box<Expr<'a>>, Box<Expr<'a>>), Div(Box<Expr<'a>>, Box<Expr<'a>>),
} }
#[derive(Debug,PartialEq)]
pub enum Fun<'a> {
Oner(Box<Expr<'a>>),
Block(Vec<Stmt<'a>>),
}
peg::parser! { peg::parser! {
grammar deelang_parser() for str { grammar deelang_parser() for str {
pub rule program() -> Vec<Stmt<'input>> pub rule program() -> Vec<Stmt<'input>>
@ -31,8 +37,8 @@ peg::parser! {
e1:(@) "/" _ e2:@ { Expr::Div(Box::new(e1), Box::new(e2)) } e1:(@) "/" _ e2:@ { Expr::Div(Box::new(e1), Box::new(e2)) }
-- --
"(" _ e:expr() ")" _ { e } "(" _ e:expr() ")" _ { e }
f:funcall() _ { f } f:funcall() { f }
i:id()? "->" _ s:stmt() { Expr::Funcdef(i, vec![s]) } f:funcdef() { f }
i:id() _ { Expr::Id(i) } i:id() _ { Expr::Id(i) }
n:num() _ { Expr::Num(n) } n:num() _ { Expr::Num(n) }
} }
@ -43,6 +49,9 @@ peg::parser! {
= n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() } = n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() }
rule funcall() -> Expr<'input> rule funcall() -> Expr<'input>
= i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) } = 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)) }
rule letter() rule letter()
= ['A'..='Z'] / ['a'..='z'] = ['A'..='Z'] / ['a'..='z']
@ -50,6 +59,10 @@ peg::parser! {
= ['0'..='9'] = ['0'..='9']
rule stop() rule stop()
= __+ / eof() = __+ / eof()
rule indent()
= ">>>"
rule dedent()
= "<<<"
rule _ // Non-meaningful whitespace rule _ // Non-meaningful whitespace
= ['\t'|' ']* = ['\t'|' ']*
rule __ // End Of Statement (comment, newline, eof, TODO semicolon) rule __ // End Of Statement (comment, newline, eof, TODO semicolon)
@ -155,14 +168,52 @@ four <- (3 - 1) * 2";
#[test] #[test]
fn test_funcdef() { fn test_funcdef() {
let prgm = r"foo <- -> bar() let prgm = r"foo <- -> bar()
"; foo <- ->
>>>
bar()
baz()
<<<
foo <- x -> y -> x * y";
let expected = vec![ let expected = vec![
Stmt::Assignment("foo", Stmt::Assignment(
Expr::Funcdef( "foo",
None, Expr::Funcdef(
vec![ None,
Stmt::Funcall(Expr::Funcall("bar", vec![])), Fun::Oner(
]) Box::new(Expr::Funcall("bar", vec![])),
)
)
),
Stmt::Assignment(
"foo",
Expr::Funcdef(
None,
Fun::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")),
)
)
)
)
)
)
)
) )
]; ];