Default argument to function

This commit is contained in:
greg 2019-06-16 21:36:59 -07:00
parent 32fe7430a4
commit b6e3469573
2 changed files with 19 additions and 0 deletions

View File

@ -472,6 +472,10 @@ impl Parser {
_ => None
};
let default = match self.token_handler.peek_kind() {
Equals => {
self.token_handler.next();
Some(self.expression()?)
},
_ => None
};
Ok(FormalParam { name, anno, default })

View File

@ -225,6 +225,21 @@ x()
vec![exst!(s "x()")])))]));
}
#[test]
fn functions_with_default_args() {
parse_test! {
"fn func(x: Int, y: Int = 4) { }",
AST(vec![
Meta::new(Declaration(
FuncDecl(Signature { name: rc!(func), operator: false, type_anno: None, params: vec![
FormalParam { name: rc!(x), default: None, anno: Some(ty!("Int")) },
FormalParam { name: rc!(y), default: Some(ex!(s "4")), anno: Some(ty!("Int")) }
]}, vec![])
))
])
};
}
#[test]
fn parsing_bools() {
parse_test!("false", AST(vec![exst!(BoolLiteral(false))]));