Blocks are a kind of expression now
This commit is contained in:
parent
7669be9c59
commit
49badc37a4
@ -9,16 +9,11 @@ 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>, Fun<'a>),
|
Funcdef(Option<&'a str>, Box<Expr<'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>>),
|
Block(Vec<Stmt<'a>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +37,8 @@ peg::parser! {
|
|||||||
i:id() _ { Expr::Id(i) }
|
i:id() _ { Expr::Id(i) }
|
||||||
n:num() _ { Expr::Num(n) }
|
n:num() _ { Expr::Num(n) }
|
||||||
}
|
}
|
||||||
|
rule block() -> Expr<'input>
|
||||||
|
= stop() indent() __* s:stmt()+ dedent() { Expr::Block(s) }
|
||||||
|
|
||||||
rule id() -> &'input str
|
rule id() -> &'input str
|
||||||
= i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i }
|
= i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i }
|
||||||
@ -50,8 +47,8 @@ peg::parser! {
|
|||||||
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>
|
rule funcdef() -> Expr<'input>
|
||||||
= i:id()? "->" _ e:expr() { Expr::Funcdef(i, Fun::Oner(Box::new(e))) } /
|
= i:id()? "->" _ e:expr() { Expr::Funcdef(i, Box::new(e)) } /
|
||||||
i:id()? "->" _ stop() indent() __* s:stmt()+ dedent() { Expr::Funcdef(i, Fun::Block(s)) }
|
i:id()? "->" _ b:block() { Expr::Funcdef(i, Box::new(b)) }
|
||||||
|
|
||||||
rule letter()
|
rule letter()
|
||||||
= ['A'..='Z'] / ['a'..='z']
|
= ['A'..='Z'] / ['a'..='z']
|
||||||
@ -139,7 +136,7 @@ four <- (3 - 1) * 2";
|
|||||||
Stmt::Assignment("four",
|
Stmt::Assignment("four",
|
||||||
Expr::Mult(
|
Expr::Mult(
|
||||||
Box::new(Expr::Minus(
|
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(1.0)),
|
||||||
)),
|
)),
|
||||||
Box::new(Expr::Num(2.0)),
|
Box::new(Expr::Num(2.0)),
|
||||||
@ -179,36 +176,30 @@ foo <- x -> y -> x * y";
|
|||||||
"foo",
|
"foo",
|
||||||
Expr::Funcdef(
|
Expr::Funcdef(
|
||||||
None,
|
None,
|
||||||
Fun::Oner(
|
Box::new(Expr::Funcall("bar", vec![])),
|
||||||
Box::new(Expr::Funcall("bar", vec![])),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
Stmt::Assignment(
|
Stmt::Assignment(
|
||||||
"foo",
|
"foo",
|
||||||
Expr::Funcdef(
|
Expr::Funcdef(
|
||||||
None,
|
None,
|
||||||
Fun::Block(vec![
|
Box::new(Expr::Block(vec![
|
||||||
Stmt::Funcall(Expr::Funcall("bar", vec![])),
|
Stmt::Funcall(Expr::Funcall("bar", vec![])),
|
||||||
Stmt::Funcall(Expr::Funcall("baz", vec![])),
|
Stmt::Funcall(Expr::Funcall("baz", vec![])),
|
||||||
])
|
]))
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
Stmt::Assignment(
|
Stmt::Assignment(
|
||||||
"foo",
|
"foo",
|
||||||
Expr::Funcdef(
|
Expr::Funcdef(
|
||||||
Some("x"),
|
Some("x"),
|
||||||
Fun::Oner(
|
Box::new(
|
||||||
Box::new(
|
Expr::Funcdef(
|
||||||
Expr::Funcdef(
|
Some("y"),
|
||||||
Some("y"),
|
Box::new(
|
||||||
Fun::Oner(
|
Expr::Mult(
|
||||||
Box::new(
|
Box::new(Expr::Id("x")),
|
||||||
Expr::Mult(
|
Box::new(Expr::Id("y")),
|
||||||
Box::new(Expr::Id("x")),
|
|
||||||
Box::new(Expr::Id("y")),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user