still working on function parser
This commit is contained in:
parent
3200a352d3
commit
a4b7b56d67
1 changed files with 13 additions and 10 deletions
|
|
@ -126,12 +126,17 @@ where
|
||||||
|
|
||||||
let return_type_parser = just(Token::Colon).ignore_then(type_parser.clone()).or_not();
|
let return_type_parser = just(Token::Colon).ignore_then(type_parser.clone()).or_not();
|
||||||
|
|
||||||
|
let return_expr = just(Token::Return)
|
||||||
|
.ignore_then(expr.clone())
|
||||||
|
.map(|expr| (expr));
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------------
|
||||||
// Function Parser
|
// Function Parser
|
||||||
//---------------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------------
|
||||||
let fun = just(Token::Fun)
|
let fun = just(Token::Fun)
|
||||||
.ignore_then(ident) // function name
|
.ignore_then(ident) // function name
|
||||||
.then(
|
.then(
|
||||||
|
// arguments
|
||||||
ident
|
ident
|
||||||
.then_ignore(just(Token::Colon))
|
.then_ignore(just(Token::Colon))
|
||||||
.then(type_parser.clone())
|
.then(type_parser.clone())
|
||||||
|
|
@ -141,16 +146,18 @@ where
|
||||||
.delimited_by(just(Token::LParen), just(Token::RParen))
|
.delimited_by(just(Token::LParen), just(Token::RParen))
|
||||||
.or_not(),
|
.or_not(),
|
||||||
)
|
)
|
||||||
.then(return_type_parser.clone())
|
.then(return_type_parser.clone()) // return type
|
||||||
.then_ignore(just(Token::LBrace))
|
.then_ignore(just(Token::LBrace)) // {
|
||||||
.then_ignore(just(Token::NewLine).repeated())
|
.then_ignore(just(Token::NewLine).repeated().or_not())
|
||||||
|
.then_ignore(return_expr.or_not())
|
||||||
.then(
|
.then(
|
||||||
expr.clone()
|
expr.clone()
|
||||||
.then_ignore(just(Token::NewLine))
|
.then_ignore(just(Token::NewLine))
|
||||||
.repeated()
|
.repeated()
|
||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
)
|
)
|
||||||
.then_ignore(just(Token::RBrace))
|
.then_ignore(just(Token::NewLine).repeated().or_not())
|
||||||
|
.then_ignore(just(Token::RBrace)) // }
|
||||||
.map(|(((name, params), return_ty), stmts)| {
|
.map(|(((name, params), return_ty), stmts)| {
|
||||||
Expr::FunctionExpr(Function {
|
Expr::FunctionExpr(Function {
|
||||||
name: name,
|
name: name,
|
||||||
|
|
@ -279,7 +286,7 @@ mod tests {
|
||||||
// tests for return expr in functions
|
// tests for return expr in functions
|
||||||
let fun_that_returns_int = parse(
|
let fun_that_returns_int = parse(
|
||||||
r"fun returnsInt(): int {
|
r"fun returnsInt(): int {
|
||||||
-> 12
|
-> 12
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
@ -287,7 +294,7 @@ mod tests {
|
||||||
fun_that_returns_int.clone().unwrap(),
|
fun_that_returns_int.clone().unwrap(),
|
||||||
vec![Expr::FunctionExpr(Function {
|
vec![Expr::FunctionExpr(Function {
|
||||||
name: String::from("returnsInt"),
|
name: String::from("returnsInt"),
|
||||||
params: None,
|
params: Some([].to_vec()),
|
||||||
return_type: Some(Type::Integer),
|
return_type: Some(Type::Integer),
|
||||||
body: Some(vec![Expr::IntLiteral(12)]),
|
body: Some(vec![Expr::IntLiteral(12)]),
|
||||||
body_expr: Some(Box::new(Expr::ReturnExpr)),
|
body_expr: Some(Box::new(Expr::ReturnExpr)),
|
||||||
|
|
@ -295,7 +302,3 @@ mod tests {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
var x = 10\nvar y = 5\n{\n var z = 7\n}\n10 + 10\n10 - 5\n5 * 5\n10 / 2
|
|
||||||
*/
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue