diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index ce19654..457f50b 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -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 }) diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index aae22bf..243d758 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -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))]));