One line functions
This commit is contained in:
parent
3b3501cb57
commit
2e04bcdfc1
@ -1,26 +1,21 @@
|
|||||||
#[derive(Debug,PartialEq)]
|
#[derive(Debug,PartialEq)]
|
||||||
pub enum Stmt<'a> {
|
pub enum Stmt<'a> {
|
||||||
Assignment(&'a str, Expr<'a>),
|
Assignment(&'a str, Expr<'a>),
|
||||||
Funcall(Funcall<'a>),
|
Funcall(Expr<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,PartialEq)]
|
#[derive(Debug,PartialEq)]
|
||||||
pub enum Expr<'a> {
|
pub enum Expr<'a> {
|
||||||
Id(&'a str),
|
Id(&'a str),
|
||||||
Num(f64),
|
Num(f64),
|
||||||
Funcall(Funcall<'a>),
|
Funcall(&'a str, Vec<Expr<'a>>),
|
||||||
|
Funcdef(Option<&'a str>, Vec<Stmt<'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 struct Funcall<'a> {
|
|
||||||
id: &'a str,
|
|
||||||
args: Vec<Expr<'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>>
|
||||||
@ -28,7 +23,7 @@ peg::parser! {
|
|||||||
pub rule stmt() -> Stmt<'input>
|
pub rule stmt() -> Stmt<'input>
|
||||||
= i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) } /
|
= i:id() "<-" _ e:expr() stop() { Stmt::Assignment(i, e) } /
|
||||||
f:funcall() stop() { Stmt::Funcall(f) }
|
f:funcall() stop() { Stmt::Funcall(f) }
|
||||||
rule expr() -> Expr<'input> = precedence!{
|
rule expr() -> Expr<'input> = 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)) }
|
||||||
--
|
--
|
||||||
@ -36,7 +31,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() _ { Expr::Funcall(f) }
|
f:funcall() _ { f }
|
||||||
|
i:id()? "->" _ s:stmt() { Expr::Funcdef(i, vec![s]) }
|
||||||
i:id() _ { Expr::Id(i) }
|
i:id() _ { Expr::Id(i) }
|
||||||
n:num() _ { Expr::Num(n) }
|
n:num() _ { Expr::Num(n) }
|
||||||
}
|
}
|
||||||
@ -45,8 +41,8 @@ peg::parser! {
|
|||||||
= i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i }
|
= i:$(letter() (letter() / digit() / ['?'|'.'|'-'])*) _ { i }
|
||||||
rule num() -> f64
|
rule num() -> f64
|
||||||
= n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() }
|
= n:$(digit()+ "."? digit()* / "." digit()+) _ { n.parse().unwrap() }
|
||||||
rule funcall() -> Funcall<'input>
|
rule funcall() -> Expr<'input>
|
||||||
= i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Funcall{id: i, args: e} }
|
= i:id() "(" _ e:(expr() ** ("," _)) ")" _ { Expr::Funcall(i, e) }
|
||||||
|
|
||||||
rule letter()
|
rule letter()
|
||||||
= ['A'..='Z'] / ['a'..='z']
|
= ['A'..='Z'] / ['a'..='z']
|
||||||
@ -84,11 +80,10 @@ apple <- 1 ## This is too
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_funcall() {
|
fn test_funcall() {
|
||||||
let expected = vec![
|
let expected = vec![
|
||||||
Stmt::Funcall(Funcall{id: "pear", args: vec![]}),
|
Stmt::Funcall(Expr::Funcall("pear", vec![])),
|
||||||
Stmt::Funcall(Funcall {
|
Stmt::Funcall(Expr::Funcall("pear",
|
||||||
id: "pear",
|
vec![Expr::Id("x"), Expr::Id("y")],
|
||||||
args: vec![Expr::Id("x"), Expr::Id("y")],
|
))
|
||||||
})
|
|
||||||
];
|
];
|
||||||
let prgm = r"pear()
|
let prgm = r"pear()
|
||||||
pear(x, y)";
|
pear(x, y)";
|
||||||
@ -103,12 +98,11 @@ apple <- pear(x, y)";
|
|||||||
Stmt::Assignment("apple",
|
Stmt::Assignment("apple",
|
||||||
Expr::Num(1.0)),
|
Expr::Num(1.0)),
|
||||||
Stmt::Assignment("apple",
|
Stmt::Assignment("apple",
|
||||||
Expr::Funcall(Funcall {
|
Expr::Funcall("pear",
|
||||||
id: "pear",
|
vec![
|
||||||
args: vec![
|
|
||||||
Expr::Id("x"),
|
Expr::Id("x"),
|
||||||
Expr::Id("y"),
|
Expr::Id("y"),
|
||||||
]})),
|
])),
|
||||||
];
|
];
|
||||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||||
}
|
}
|
||||||
@ -140,4 +134,37 @@ four <- (3 - 1) * 2";
|
|||||||
];
|
];
|
||||||
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_compound_expression() {
|
||||||
|
let prgm = "apple <- pear(x, y) + z";
|
||||||
|
let expected = vec![
|
||||||
|
Stmt::Assignment("apple",
|
||||||
|
Expr::Plus(
|
||||||
|
Box::new(Expr::Funcall(
|
||||||
|
"pear",
|
||||||
|
vec![Expr::Id("x"), Expr::Id("y")],
|
||||||
|
)),
|
||||||
|
Box::new(Expr::Id("z"))
|
||||||
|
)
|
||||||
|
),
|
||||||
|
];
|
||||||
|
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_funcdef() {
|
||||||
|
let prgm = r"foo <- -> bar()";
|
||||||
|
let expected = vec![
|
||||||
|
Stmt::Assignment("foo",
|
||||||
|
Expr::Funcdef(
|
||||||
|
None,
|
||||||
|
vec![
|
||||||
|
Stmt::Funcall(Expr::Funcall("bar", vec![])),
|
||||||
|
])
|
||||||
|
)
|
||||||
|
];
|
||||||
|
|
||||||
|
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user