Default argument to function
This commit is contained in:
parent
32fe7430a4
commit
b6e3469573
@ -472,6 +472,10 @@ impl Parser {
|
|||||||
_ => None
|
_ => None
|
||||||
};
|
};
|
||||||
let default = match self.token_handler.peek_kind() {
|
let default = match self.token_handler.peek_kind() {
|
||||||
|
Equals => {
|
||||||
|
self.token_handler.next();
|
||||||
|
Some(self.expression()?)
|
||||||
|
},
|
||||||
_ => None
|
_ => None
|
||||||
};
|
};
|
||||||
Ok(FormalParam { name, anno, default })
|
Ok(FormalParam { name, anno, default })
|
||||||
|
@ -225,6 +225,21 @@ x()
|
|||||||
vec![exst!(s "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]
|
#[test]
|
||||||
fn parsing_bools() {
|
fn parsing_bools() {
|
||||||
parse_test!("false", AST(vec![exst!(BoolLiteral(false))]));
|
parse_test!("false", AST(vec![exst!(BoolLiteral(false))]));
|
||||||
|
Loading…
Reference in New Issue
Block a user